poj2886 Who Gets the Most Candies? 线段树

Who Gets the Most Candies?
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 10876   Accepted: 3383
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

  N个人顺时针站成一圈,从第K个人开始,他先离开,每个离开的人拿的号码m如果是正数,下一个离开的人就是他左边第m个人,否则就是他右边第m个人。第n个离开的人会得到f[n]个糖果,f[n]就是n约数的个数,输出得到糖果最多的人的名字和得到的糖果个数。

  网上有人打反素数表做的,会快一点,对于每个N直接暴力找那个最大的位置也可以,复杂度O(nlgn)。用线段树模拟离开的过程,sum记录子树还剩几个人没走。假设把人编号1-N,这里重点就在于找出下一个人在剩下的人里排第几,然后更新线段树,同时得到下一个人的编号。这里和约瑟夫环类似,假设当前的人编号为ans,在还剩下的人中排第now个,那么他离开后下一个人排的位置,左边的话就是now=(now-1+a[ans].val-1)%M+1,右边的话是now=((now-1+a[ans].val)%M+M)%M+1。这里搞清楚就容易了。

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> pii;

const int MAXN=500010;
const int MAXNODE=4*MAXN;
const int LOGMAXN=50;

int N,K,MAX,cnt[MAXN],sum[MAXNODE];

struct st{
    char name[20];
    int val;
}a[MAXN];

int cal(int n){
    memset(cnt,0,sizeof(cnt));
    for(int i=1;i<=n;i++)
        for(int j=i;j<=n;j+=i) cnt[j]++;
    MAX=-1;
    int id;
    for(int i=1;i<=n;i++) if(cnt[i]>MAX){
        MAX=cnt[i];
        id=i;
    }
    return id;
}

void build(int o,int L,int R){
    if(L>=R){
        sum[o]=1;
        return;
    }
    int mid=L+(R-L)/2;
    build(o<<1,L,mid);
    build(o<<1|1,mid+1,R);
    sum[o]=sum[o<<1]+sum[o<<1|1];
}

int update(int o,int L,int R,int pos){
    sum[o]--;
    if(L>=R) return L;
    int mid=L+(R-L)/2;
    if(pos<=sum[o<<1]) return update(o<<1,L,mid,pos);
    else return update(o<<1|1,mid+1,R,pos-sum[o<<1]);
}

int main(){
    freopen("in.txt","r",stdin);
    while(scanf("%d%d",&N,&K)!=EOF){
        for(int i=1;i<=N;i++) scanf("%s%d",a[i].name,&a[i].val);
        int id=cal(N),now=K,ans;
        build(1,1,N);
        for(int i=0;i<id;i++){
            ans=update(1,1,N,now);
            int M=N-i-1;
            if(!M) break;
            if(a[ans].val>=0) now=(now-1+a[ans].val-1)%M+1;
            else now=((now-1+a[ans].val)%M+M)%M+1;
        }
        printf("%s %d\n",a[ans].name,MAX);
    }
    return 0;
}


你可能感兴趣的:(poj2886 Who Gets the Most Candies? 线段树)