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

首頁 > 學院 > 開發設計 > 正文

poj 2886 Who Gets the Most Candies?

2019-11-11 00:48: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;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 正阳县| 桑植县| 金坛市| 商丘市| 廉江市| 和龙市| 罗田县| 顺平县| 威海市| 北流市| 三穗县| 酉阳| 彩票| 甘泉县| 玉田县| 桃园市| 喜德县| 洛宁县| 循化| 昭平县| 家居| 广丰县| 仲巴县| 北宁市| 毕节市| 嘉义市| 沁源县| 黄浦区| 新建县| 灵宝市| 吉林省| 舒城县| 和林格尔县| 理塘县| 贵州省| 南宫市| 张家港市| 普洱| 南昌县| 金门县| 桂林市|