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

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§ candies where F§ 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 2
Tom 2
Jack 4
Mary -1
Sam 1
Sample Output
Sam 3

题意:
从第k个人开始出队,出队那个人的权值为正,则往顺时针数第v个人出队。否则逆时针第(-v)个人出队。
思路:
维护线段树sum值,寻找出队的那个节点。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>

using namespace std;

const int maxn = 5e5 + 7;

int f[maxn];

struct Node
{
    char name[20];
    int val;
    int id;
}nodes[maxn];

struct Tree
{
    int l,r,sum;
}t[maxn << 2];

void pushup(int i)
{
    t[i].sum = t[i * 2].sum + t[i * 2 + 1].sum;
}

void build(int i,int l,int r)
{
    t[i].l = l;t[i].r = r;
    if(l == r)
    {
        t[i].sum = 1;
        return;
    }
    int m = (l + r) >> 1;
    build(i * 2,l,m);
    build(i * 2 + 1,m + 1,r);
    pushup(i);
}

int update(int i,int x)
{
    if(t[i].l == t[i].r)
    {
        t[i].sum = 0;
        return t[i].l;
    }
    int m = (t[i].l + t[i].r) >> 1;
    int num = 0;
    if(t[i * 2].sum >= x)
    {
        num = update(i * 2,x);
    }
    else
    {
        num = update(i * 2 + 1,x - t[i * 2].sum);
    }
    pushup(i);
    return num;
}

void init()
{
    for(int i = 1;i <= 500000;i++)
    {
        for(int j = 1;j * i <= 500000;j++)
        {
            f[i * j]++;
        }
    }
}

int main()
{
    int n,k;
    init();
    while(~scanf("%d%d",&n,&k) && n && k)
    {
        build(1,1,n);
        for(int i = 1;i <= n;i++)
        {
            scanf("%s%d",nodes[i].name,&nodes[i].val);
            nodes[i].id = i;
        }
        int now = k;
        int cnt = n;
        
        int ans = 0;
        char ans_name[20];
        for(int i = 1;i <= n;i++)
        {
            int num = update(1,now);
//            printf("%d ",num);
            int v = nodes[num].val;
            int tmp = f[i];
            cnt--;
            if(tmp > ans)
            {
                ans = tmp;
                strcpy(ans_name,nodes[num].name);
            }
            if(i == n)break;
            int l = now - 1;
            int r = cnt - now + 1;
//            printf("%d %d\n",l,r);
            if(v > 0)//顺时针
            {
                v = v % cnt;
                if(v == 0)v = cnt;

                if(v <= r)
                {
                    now = v + l;
                }
                else
                {
                    now = v - r;
                }
            }
            else if(v < 0)//逆时针
            {
                v = (-v) % cnt;
                if(v == 0)v = cnt;

                if(v <= l)
                {
                    now = l - v + 1;
                }
                else
                {
                    now = cnt - (v - l) + 1;
                }
            }
        }
        printf("%s %d\n",ans_name,ans);
    }
    return 0;
}

发布了628 篇原创文章 · 获赞 17 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/104062440