| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 14121 | Accepted: 4461 | |
| Case Time Limit: 2000MS | ||
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 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?
Input
There are several test cases in the input. Each test case starts with two integers N (0 < N ≤ 500,000) and K (1 ≤ K ≤ N) on the first line. The next N lines contains the names of the children (consisting of at most 10 letters) and the integers (non-zero with magnitudes within 108) on their cards in increasing order of the children’s numbers, a name and an integer separated by a single space in a line with no leading or trailing spaces.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 2Tom 2Jack 4Mary -1Sam 1Sample Output
Sam 3題意:類似約瑟夫環那樣,每個人有個值,是如果他跳出來之后,從他這開始算下一個該跳出來的人的序號。只不過每個人跳出來的時候會得到一些糖果,而得到的糖果的數目等于f(p),是指第p次跳出的人能夠獲得p的因數的個數的糖果。
思路:先找出第幾次跳出能夠獲得最大的糖果數id。
然后模擬n次,第n次跳出的人的名字輸出,然后輸出最大的糖果數就行了。
終點就是用線段樹維護這個約瑟夫環。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN=5e5+7;int n,k,id;struct node{ int l,r; int sum;//當前區間還有多少個人}tree[MAXN<<2];struct node1{ char name[20]; int val;}people[MAXN];int ans[MAXN];void get_ans()//每個數的因數的個數{ int i,j; for(i=1;i<=500000;++i) { ans[i]++; for(j=i*2;j<=500000;j+=i)ans[j]++; }}void get_id(){ int MAX=1; for(int i=2;i<=n;++i) { if(ans[i]>MAX) { MAX=ans[i]; id=i; } }}void build_tree(int i,int l,int r){ tree[i].l=l; tree[i].r=r; tree[i].sum=r-l+1; if(l==r)return; int mid=(l+r)>>1; build_tree(i<<1,l,mid); build_tree(i<<1|1,mid+1,r);}int del(int i,int key)//線段樹維護刪除人{ tree[i].sum--; if(tree[i].l==tree[i].r)return tree[i].l; if(tree[i<<1].sum>=key)return del(i<<1,key); else return del(i<<1|1,key-tree[i<<1].sum);}int main(){ int i; get_ans(); while(~scanf("%d%d",&n,&k)) { get_id(); for(i=1;i<=n;++i) { scanf("%s %d",people[i].name,&people[i].val); } build_tree(1,1,n); int mod=tree[1].sum; n=id; int pos=0; people[0].val=0; while(n--) { if(people[pos].val>0) { k=((k-1+people[pos].val-1)%mod+mod)%mod+1; } else k=((k-1+people[pos].val)%mod+mod)%mod+1; pos=del(1,k); mod--; } PRintf("%s %d/n",people[pos].name,ans[id]); } return 0;}
新聞熱點
疑難解答