首页 > 代码库 > POJ 2886 Who Gets the Most Candies? (线段树)

POJ 2886 Who Gets the Most Candies? (线段树)

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 theK-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. LetA denote the integer. If A is positive, the next child will be theA-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, thep-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 integersN (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


只说一下模拟过程的思路,减少人数的过程中把一个圈看成 所有的位置都有人,理解为相对位置,实际上过程中有的位置是空的。这样做的目的是,方便用线段树查询该点的真是位置。比如现在在3这个位置,注意这里一圈的位置都看做有人,卡片上是4,那么应该找到第7个位置上,但是第三个位置是要退出,会影响到后面人的相对位置,因此,只能找到第六个位置,好了,现在回到真实的位置,也就是存在空位的圈,目的是查找第6个是 人存在的位置,然后按照那个人手中的卡片,迭代整个过程。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <ctype.h>
#define lson o << 1, l, m
#define rson o << 1|1, m+1, r
using namespace std;
typedef long long LL;
const int MAX = 0x3f3f3f3f;
const int maxn = 500050;
struct C{
    int v;
    char name[11];
} a[maxn];
int n, k, ans[maxn], id, res[maxn<<2], pos;
void Ini(){
    for(int i=1;i<=n;i++){
        ans[i]++;
        for(int j=2*i;j<=n;j+=i)
            ans[j]++;
    }
    int _max=ans[1];
    id=1;
    for(int i=2;i<=n;i++)   //找出第几个人跳出获得的糖最多
        if(ans[i]>_max){
            _max=ans[i];
            id=i;
        }
}
void up(int o) {
    res[o] = res[o<<1] + res[o<<1|1];
}
void build(int o, int l, int r) {
    if(l == r) {
        res[o] = 1;
        return;
    }
    int m = (l+r) >> 1;
    build(lson);
    build(rson);
    up(o);
}
int update(int p, int o, int l, int r) {
    if(l == r) {
        res[o] = 0;
        return l;
    }
    int m = (l+r) >> 1, tmp;
    if(p <= res[o<<1]) tmp = update(p, lson);
    else tmp = update(p-res[o<<1], rson);
    up(o);
    return tmp;
}
int main()
{

    while(~scanf("%d%d", &n, &k)) {
        Ini();
        int cnt = n, i, cc = id;
        for(i = 1; i <= n; i++) scanf("%s%d", a[i].name, &a[i].v);
        build(1, 1, n);
        int pos ,next = k;
        pos = update(next, 1, 1, n);
        cnt = res[1];
        cc--;
        while(cc--) {
            if(a[pos].v > 0) next = ( (next-1+a[pos].v-1)%cnt + cnt )%cnt + 1;
            else next = ( (next-1+a[pos].v)%cnt + cnt )%cnt + 1;
            pos = update(next, 1, 1, n);
            cnt = res[1];
        }
        printf("%s %d\n", a[pos].name, ans[id]);
    }

    return 0;
}