国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

poj 2886 Who Gets the Most Candies?

2019-11-10 22:49:33
字體:
供稿:網(wǎng)友

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

題意:類似約瑟夫環(huán)那樣,每個(gè)人有個(gè)值,是如果他跳出來之后,從他這開始算下一個(gè)該跳出來的人的序號(hào)。只不過每個(gè)人跳出來的時(shí)候會(huì)得到一些糖果,而得到的糖果的數(shù)目等于f(p),是指第p次跳出的人能夠獲得p的因數(shù)的個(gè)數(shù)的糖果。

思路:先找出第幾次跳出能夠獲得最大的糖果數(shù)id。

然后模擬n次,第n次跳出的人的名字輸出,然后輸出最大的糖果數(shù)就行了。

終點(diǎn)就是用線段樹維護(hù)這個(gè)約瑟夫環(huá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;//當(dāng)前區(qū)間還有多少個(gè)人}tree[MAXN<<2];struct node1{    char name[20];    int val;}people[MAXN];int ans[MAXN];void get_ans()//每個(gè)數(shù)的因數(shù)的個(gè)數(shù){    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)//線段樹維護(hù)刪除人{(lán)    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;}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阳江市| 毕节市| 宁波市| 广安市| 兖州市| 东阿县| 武胜县| 温宿县| 昌乐县| 阳江市| 永福县| 彭山县| 滨州市| 塘沽区| 广州市| 赤峰市| 宁南县| 永定县| 肃宁县| 南川市| 阿克| 青田县| 利川市| 南康市| 池州市| 黄梅县| 香格里拉县| 密云县| 时尚| 祁门县| 阿拉善盟| 信阳市| 新乡县| 晋江市| 离岛区| 鄂伦春自治旗| 大化| 天台县| 东山县| 湘潭市| 巴东县|