Who Gets the Most Candies?
Description N children are sitting in a circle to play a game. The children are numbered from 1 to N in clockwise order. Each of them has a card with a non-zero integer on it in his/her hand. The game starts from the The game lasts until all children have jumped out of the circle. During the game, the Input There are several test cases in the input. Each test case starts with two integers Output Output one line for each test case containing the name of the luckiest child and the number of candies he/she gets. If ties occur, always choose the child who jumps out of the circle first. Sample Input 4 2 Tom 2 Jack 4 Mary -1 Sam 1 Sample Output Sam 3 Source POJ Monthly--2006.07.30, Sempr |
[Submit] [Go Back] [Status] [Discuss]
题意:N个人围成一圈第一个人跳出圈后会告诉你下一个谁跳出来跳出来的人(如果他手上拿的数为正数,从他左边数A个,反之,从他右边数A个) 跳出来的人所得到的糖果数量和他跳出的顺序有关 所得的糖果数为 (假设他是第k个跳出的) 则他得到的糖数为k能被多少个数正数 比如说 k = 6 ; 6 = 1*2*3*6 所以他得到的糖数为4;
思路:线段数 先算出N个人中,是第几个人(id)跳出来得到的糖果最多。然后模拟id遍 长到第id个人的name
线段树的结点中保存该区间内还剩多少人,每次update 删除一个人。
题目思路转:http://www.cnblogs.com/jackge/archive/2013/04/23/3038576.html
ac代码
#include<stdio.h> #include<string.h> int n,k; struct s { char name[15]; int val; }b[500100]; struct tree { int l,r,sum; }node[500100<<2]; int ans[500100],id,maxn; void solve() { memset(ans,0,sizeof(ans)); int i,j; for(i=1;i<=n;i++) { ans[i]++; for(j=2*i;j<=n;j+=i) { ans[j]++; } } maxn=ans[1],id=1; for(i=2;i<=n;i++) { if(maxn<ans[i]) { maxn=ans[i]; id=i; } } } void build_tr(int l,int r,int tr) { node[tr].l=l; node[tr].r=r; node[tr].sum=r-l+1; if(l==r) return; int mid=(l+r)>>1; build_tr(l,mid,tr<<1); build_tr(mid+1,r,tr<<1|1); } int query(int key,int tr) { node[tr].sum--; if(node[tr].l==node[tr].r) { return node[tr].l; } if(key<=node[tr<<1].sum) query(key,tr<<1); else query(key-node[tr<<1].sum,tr<<1|1); } int main() { //int n,k; while(scanf("%d%d",&n,&k)!=EOF) { int i; for(i=1;i<=n;i++) { scanf("%s%d",b[i].name,&b[i].val); } build_tr(1,n,1); solve(); int p=id,idx; for(i=0;i<p;i++) { n--; idx=query(k,1);//原始位置 if(n==0) break; if(b[idx].val>0) { k=(k-1+b[idx].val-1)%n+1;//下一次掉出的人的编号 } else k=((k-1+b[idx].val)%n+n)%n+1; } printf("%s %d\n",b[idx].name,maxn); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。