poj 2886 Who Gets the Most Candies?(线段树单点更新+反素数)真难。。。

1、http://poj.org/problem?id=2886

2、题目大意:

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

题目:

Who Gets the Most Candies?
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 8763   Accepted: 2666
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 ≤ KN) 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 2
Tom 2
Jack 4
Mary -1
Sam 1

Sample Output

Sam 3

Source

POJ Monthly--2006.07.30, Sempr

 

3、AC代码:

#include<stdio.h>
#include<algorithm>
using namespace std;
#define N 500005
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int num[N],sum[N*4];
char str[N][15];
int f[N],cur_i,next_i,cur_n,ans,ans1,cnt,cur;
void pushUp(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        sum[rt]=1;
        return ;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    pushUp(rt);
}
void update(int p,int l,int r,int rt)
{
    if(l==r)
    {
        sum[rt]--;
        next_i=num[l];
        cur_n--;
        if(ans<f[cnt])
        {
            ans=f[cnt];
            ans1=l;
        }
        return ;
    }
    int m=(l+r)>>1;
    if(p<=sum[rt<<1])
    update(p,lson);
    else
    update(p-sum[rt<<1],rson);
    pushUp(rt);
}
int main()
{
    int n,k;
    for(int i=1; i<=N; i++)
    {
        for(int j=1; i*j<=N; j++)
        {
            f[i*j]++;
        }
    }
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        build(1,n,1);
        for(int i=1; i<=n; i++)
        {
            scanf("%s%d",str[i],&num[i]);
        }
        cur_n=n;
        cur_i=n;
        next_i=k;
        cnt=1;
        ans=0;
        ans1=1;
        for(int i=1; i<=n; i++)
        {
            if(next_i>=0)
                cur_i--;
            cur=(cur_i+next_i%cur_n+cur_n)%cur_n;
            update(cur+1,1,n,1);
            cur_i=cur;
            cnt++;
        }
        printf("%s %d\n",str[ans1],ans);
    }
    return 0;
}


 

你可能感兴趣的:(poj 2886 Who Gets the Most Candies?(线段树单点更新+反素数)真难。。。)