码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-08-04 17:57:27      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   os   io   for   2014   art   

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


只说一下模拟过程的思路,减少人数的过程中把一个圈看成 所有的位置都有人,理解为相对位置,实际上过程中有的位置是空的。这样做的目的是,方便用线段树查询该点的真是位置。比如现在在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;
}



POJ 2886 Who Gets the Most Candies? (线段树),布布扣,bubuko.com

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

标签:des   style   blog   os   io   for   2014   art   

原文地址:http://blog.csdn.net/u013923947/article/details/38369931

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!