HDU- Who Gets the Most Candies?
Who Gets the Most Candies?
Time Limit : 10000/5000ms (Java/Other) Memory Limit : 262144/131072K (Java/Other)
Total Submission(s) : 15 Accepted Submission(s) : 7
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 K-th child, who tells all the others the integer on his card and jumps out of the circle. The integer on his card tells the next child to jump out. Let A denote the integer. If A is positive, the next child will be the A-th child to the left. If A is negative, the next child will be the (−A)-th child to the right.
The game lasts until all children have jumped out of the circle. During the game, the p-th child jumping out will get F(p) candies where F(p) is the number of positive integers that perfectly divide p. Who gets the most candies?
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.
/*题意:N个人围成一圈第一个人跳出圈后会告诉你下一个谁跳出来跳出来的人 (如果他手上拿的数为正数,从他左边数A个,反之,从他右边数A个) 跳出来的人所得到的糖果数量和他跳出的顺序有关 所得的糖果数为 (假设他是第k个跳出的) 则他得到的糖数为k能被多少个数正数 比如说 k = 6 ; 6 = 1*2*3*6 所以他得到的糖数为4; 思路:线段数 先算出N个人中,是第几个人(id)跳出来得到的糖果最多。然后模拟id遍 长到第id个人的name 线段树的结点中保存该区间内还剩多少人,每次update 删除一个人。*/ #include<stdio.h> #include<string.h> #include<math.h> #define maxn 500004 int n,id; struct node { int left,right; int num; }; node tree[3*maxn]; struct data { int val; char name[15]; } boy[maxn]; int ans[maxn]; void build(int left,int right,int i) { tree[i].left =left; tree[i].right =right; tree[i].num =right-left+1; if(tree[i].left ==tree[i].right ) return ; build(left,(left+right)/2,2*i); build((left+right)/2+1,right,2*i+1); } int insert(int i,int x) { tree[i].num--; if(tree[i].left==tree[i].right) return tree[i].left; if(x<=tree[i].num) return insert(2*i,x); else return insert(2*i+1,x-tree[2*i].num); } void count_ans()//这个函数最重要。 { int i,max,j; memset (ans,0,sizeof(ans)); //计算ans for (i = 1; i <= n; i ++) { ans[i] ++; for (j=2*i;j<= n;j+= i) ans[j] ++; } max = ans[1]; id = 1; for (i = 2; i <= n; i ++) //找出第几个人跳出获得的糖最多 if (ans[i] > max) { max = ans[i]; id = i; } } int main () { int i,k,mod; while (~scanf ("%d %d",&n,&k)) { count_ans(); for (i = 1; i <= n; i ++) scanf ("%s %d",boy[i].name,&boy[i].val); build(1,n,1); mod =tree[1].num; int pos = 0; boy[0].val = 0; n = id; while (n --) { if (boy[pos].val > 0) //k表剩余的人中从左起第k中出队(PS:k的求法是看别人的) k = ((k + boy[pos].val - 2)%mod + mod)%mod + 1; else k = ((k + boy[pos].val - 1)%mod + mod)%mod + 1; pos = insert(1,k); mod = tree[1].num; } printf ("%s %d\n",boy[pos].name,ans[id]); } return 0; }