URAL 1521 War Games 2 树状数组解决约瑟夫环,输出离队顺序
2015-02-12 10:32
429 查看
1521. War Games 2
Time limit: 1.0 secondMemory limit: 64 MB
Background
During the latest war games (this story is fully described in the problem "War games") the Minister of Defense of theSoviet Federation comrade Ivanov had a good chance to make sure personally, that an alertness of the Soviet Army under his command is just brilliant. But there was a thing, that continued to worry him. Being an outstanding commander, he realized, that only
physical conditions of the soldiers were demonstrated. So the time came to organize one more war games and examine their mental capacity.
General Rascal was appointed to be responsible for the war games again. The general donated the allocated funds to the poor and went to bed free-hearted. In his dream, the tactics manual appeared to
him and described a scheme, that allows to organize the war games absolutely free of charge.
Problem
In accordance with this scheme, the war games are divided into N phases; and N soldiers, successively numbered from 1 to N, are marching round a circleone after another, i.e. the first follows the second, the second follows the third, ..., the (N-1)-th follows the N-th, and the N-th follows the first. At each phase, a single soldier leaves the circle and
goes to clean the WC, while the others continue to march. At some phase, the circle is left by a soldier, who is marching Kpositions before the one, who left the circle at the previous phase. A soldier, whose number is K,
leaves the circle at the first phase.
Surely, Mr. Rascal cherished no hope about his soldiers' abilities to determine an order of leaving the circle. "These fools can not even paint the grass properly", - he sniffed scornfully and went
to sergeant Filcher for an assistance.
Input
The only line contains the integer numbers N (1 ≤ N ≤ 100000) and K (1 ≤ K ≤ N).
Output
You should output the numbers of soldiers as they leave the circle. The numbers should be separated by single spaces.
Sample
input | output |
---|---|
5 3 | 3 1 5 2 4 |
做法:因为要输出在原串中的位置,所以用了树状数组加二分来找。树状数组开始每个点都初始化为1,然后离队的改为减一。对于sum(x),x是原来环的 标号,sum(x) 就表示 当前 减少了人数的环中 对应的 是第几个。
//树状数组 单点更新 区间查询 #include <stdio.h> #include <string.h> const int MAX_N=100000+5;//开到区间大小就够了 int bit[MAX_N],N; // int sum(int i) { int s=0; while(i>0) { s+=bit[i]; i-=i&(-i); //感觉从底层向上加。 加减二进制末尾的1 } return s; } void add(int i,int x) { while(i<=N) { bit[i]+=x; i+=i&(-i); } } int find(int x,int n) { int l=1,r=n; while(l<=r) { int mid=(l+r)/2; int tem=sum(mid); if(tem<x) l=mid+1; else r=mid-1; } printf("%d ",l); return l; } int main() { int n,k; scanf("%d%d",&n,&k); N=n; memset(bit,0,sizeof(bit)); for(int i=1;i<=n;i++) add(i,1); int cur = 1; int res = n; for(int i=1;i<=n;i++) { cur=(cur+k-1)%res; if(cur==0) cur=res; int tem=find(cur,n); add(tem,-1); res--; } return 0; }
相关文章推荐
- SBT解决约瑟夫环问题(URAL1521)
- ural 1521. War Games 2 约瑟夫环 SBT实现
- C语言用数组1. 简单约瑟夫环问题: N个人,编号从1~N围成一圈,输入一个数T,从1号开始报数,报到T的人出圈;下一人又从1开始报数,下一个报到T的人出圈,输出出圈顺序。 考虑问实现约瑟夫环问题
- 使用数组解决约瑟夫环问题
- 对一维顺序数组螺旋输出到二维数组
- PHP 通过数组判断数组顺序输出是否是二叉排序树的后序遍历结果
- 数组解决杨辉三角问题(输出10行)
- 利用数组列表求两个数组对应下标之和,在第三个数组输出《我的方法不太正确,求更准确的解决方法》
- 从键盘输入20个整数将奇数&偶数分别存在不同的数组中并按先奇数后偶数的顺序输出这两个数组中的数据
- hdu 2688_Rotate(树状数组,求顺序对数)
- 数组解决约瑟夫环问题
- 给定一个数组,要求把数组内元素的顺序随机打乱,然后输出,主要是要保证效率。
- 用模板函数解决未知类型未知维数的数组的输出
- php解决json_encode输出GB2312中文问题 (数组)
- 讲一个字符串数组的顺序翻转后输出
- 数组解决约瑟夫环问题
- 用函数+指针数组对输入的3个字符串按由小到大的顺序输出
- 用树状数组解决区间查询问题
- ural 1028【树状数组】
- 用树状数组解决求统计问题:hdu2492