Who Gets the Most Candies? - POJ 2886 线段树

来源:互联网 发布:昆仑万维 知乎 编辑:程序博客网 时间:2024/06/05 16:46

Who Gets the Most Candies?
Time Limit: 5000MS Memory Limit: 131072KTotal Submissions: 10217 Accepted: 3169Case 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 1

Sample Output

Sam 3

题意:N个小孩围成一圈,他们被顺时针编号为 1 到 N。每个小孩手中有一个卡片,上面有一个非 0 的数字,游戏从第 K 个小孩开始,他告诉其他小孩他卡片上的数字并离开这个圈,他卡片上的数字 A 表明了下一个离开的小孩,如果 A 是大于 0 的,则下个离开的是左手边第 A 个,如果是小于 0 的,则是右手边的第 A 个小孩。游戏将直到所有小孩都离开,在游戏中,第 p 个离开的小孩将得到 F(p) 个糖果,F(p) 是 p 的约数的个数,问谁将得到最多的糖果。输出最幸运的小孩的名字和他可以得到的糖果。注意左手边指的的一维中下面的,不要弄反。

思路:每次记录其中有多少孩子已经离开了,然后查找下一个应该是原来几号位置的小孩。

AC代码如下:

#include<cstdio>#include<cstring>using namespace std;struct node{    int num,l,r;}tree[2000010];int f[500010],f2[500010],jump[500010];char s[500010][15];void build(int o,int l,int r){    tree[o].l=l;    tree[o].r=r;    tree[o].num=r-l+1;    if(l==r)      return;    int mi=(l+r)/2;    build(o*2,l,mi);    build(o*2+1,mi+1,r);}void update(int o,int pos){    if(tree[o].l==pos && tree[o].r==pos)    {        tree[o].num=0;        return;    }    int mi=(tree[o].l+tree[o].r)/2;    if(pos<=mi)      update(o*2,pos);    else      update(o*2+1,pos);    tree[o].num=tree[o*2].num+tree[o*2+1].num;}int query1(int o,int l,int r){    if(tree[o].l==l && tree[o].r==r)      return tree[o].num;    int mi=(tree[o].l+tree[o].r)/2;    if(r<=mi)      return query1(o*2,l,r);    if(l>mi)      return query1(o*2+1,l,r);    return query1(o*2,l,mi)+query1(o*2+1,mi+1,r);}int query2(int o,int num){    if(tree[o].l==tree[o].r && num==1)      return tree[o].l;    if(tree[o*2].num>=num)      return query2(o*2,num);    else      return query2(o*2+1,num-tree[o*2].num);}int main(){    int n,i,j,k,ret,q,h,pos;    for(i=1;i<=500000;i++)       for(j=i;j<=500000;j+=i)          f[j]++;    f2[1]=1;    for(i=2;i<=500000;i++)       if(f[i]<=f[f2[i-1]])         f2[i]=f2[i-1];       else         f2[i]=i;    while(~scanf("%d%d",&n,&k))    {        for(i=1;i<=n;i++)           scanf("%s%d",s[i],&jump[i]);        build(1,1,n);        pos=k;        for(i=1;i<f2[n];i++)        {            update(1,pos);            q=query1(1,1,pos);            if(jump[pos]>0)              k=(q+jump[pos])%(n-i);            else              k=((q+jump[pos]+1)%(n-i)+n-i)%(n-i);            if(k==0)              k=n-i;            pos=query2(1,k);        }        printf("%s %d\n",s[pos],f[f2[n]]);    }}



0 0
原创粉丝点击