HDU 1443 Joseph 约瑟夫环 暴力
2015-12-22 13:10
381 查看
题意:有k个好人在队伍前面,k个坏人在队伍后面。每次去掉一个在第m个位置的人,问使所有坏人都比好人先去掉的最小的m是多少?
思路:因为k比较小,直接模拟这个过程。要注意一个情况,每次找第mm个数的要找在现在这种情况的第m0m_0个数。假设已经找到cntcnt个数,则m0=(m−1)%(2∗k−cnt)+1m_0 = (m-1) \%(2 * k - cnt) + 1。mm和m0m_0是等价的,m0m_0比mm在很多情况下小很多。用mm可能就超时了。
http://acm.hdu.edu.cn/showproblem.php?pid=1443/********************************************* Problem : HDU 1443 Author : NMfloat InkTime (c) NM . All Rights Reserved . ********************************************/ #include <map> #include <set> #include <queue> #include <stack> #include <cmath> #include <ctime> #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> #define rep(i,a,b) for(int i = (a) ; i <= (b) ; i ++) #define rrep(i,a,b) for(int i = (b) ; i >= (a) ; i --) #define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next) #define cls(a,x) memset(a,x,sizeof(a)) #define eps 1e-8 using namespace std; const int MOD = 1e9+7; const int INF = 0x3f3f3f3f; const int MAXN = 1e5+5; const int MAXE = 2e5+5; typedef long long LL; typedef unsigned long long ULL; int T,n,m,k; int fx[] = {0,1,-1,0,0}; int fy[] = {0,0,0,-1,1}; int vis[30]; int ans[14]; void init() { rep(i,1,13) { rep(j,i+1,INF) { cls(vis,0); int pos = (j - 1 ) % (2 * i) + 1; if(pos <= i) continue; vis[pos] = 1; int cnt = 1;//被删除的个数; int flag = 0; while(cnt < i) { int limit = (j - 1) % ( 2 * i - cnt ) + 1; rep(ia,1,limit) { ++pos; pos = (pos - 1) % (2 * i) + 1; while(vis[pos]) { ++ pos; pos = (pos - 1) % (2 * i) + 1; } } if(pos <= i) {flag = 1 ; break;} vis[pos] = 1; cnt ++; } if(flag) continue ; else { ans[i] = j; //printf("%d %d\n",i,ans[i]); break; } } } } void input() { } void solve() { printf("%d\n",ans ); } int main(void) { //freopen("a.in","r",stdin); init(); while(scanf("%d",&n),n) { input(); solve(); } return 0; }
相关文章推荐
- 路由的距离向量算法等(也不知道该起什么名字好)
- setValuesForKeysWithDictionary 和 setValue:forKey 和setValue:forUndefinedKey之间的关系
- iOS 中正则表达式的一些语法应用
- Nexus添加新Repository后Maven仍找不到jar包的解决方案
- 关于bootsreap自动提示typeahead的bug
- 重要的4种函数
- 函数的调用
- 解决MySQL显示中文的问题
- 【HTML 未完】tabindex 属性
- iOS8推出了几个新的“controller”
- Spring面试问答Top 25
- 磁盘阵列命令行基本操作
- git 回滚
- python实现决策树ID3算法
- cpu占用率曲线 直线 sin() 图像
- iOS开发之设计模式(二)
- ios tableview section标题不滑动问题
- 自己的代码的风格——关键字的定义(变量的定义)匈牙利命名法
- HDU 4876 ZCC loves cards(暴力剪枝)
- mongodb在windows系统的安装