首页 > 学院 > 开发设计 > 正文

poj 2886 Who Gets the Most Candies?

2019-11-10 21:24:00
字体:
来源:转载
供稿:网友

Who Gets the Most Candies?
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 1

Sample 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;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表