zoukankan      html  css  js  c++  java
  • 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 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 2
    Tom 2
    Jack 4
    Mary -1
    Sam 1

    Sample Output

    Sam 3

    #include<cstdio>
    #include<cstring>
    #include<map>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    const int MAXN=5e5+5;
    struct node
    {
        int l,r,num;
    }tree[MAXN<<2];
    char s[MAXN][11];
    int p[MAXN],add[MAXN],n,k;
    void table()
    {
        for(int i=1;i<MAXN;i++)p[i]=1;
        for(int i=2;i<MAXN;i++)
            for(int j=i;j<MAXN;j+=i)
                p[j]++;
        int maxv=0;
        for(int i=2;i<MAXN;i++)
        {
            if(p[i]>maxv)maxv=p[i];
            p[i]=maxv;
        }
    }
    void pushup(int o)
    {
        tree[o].num=tree[o<<1].num+tree[o<<1|1].num;
    }
    void build(int l,int r,int o)
    {
        tree[o].l=l,tree[o].r=r;
        if(l==r){tree[o].num=1;return;}
        int mid=(l+r)>>1;
        build(l,mid,o<<1);
        build(mid+1,r,o<<1|1);
        pushup(o);
    }
    int update(int ad,int o)
    {
        int res;
        if(tree[o].l==tree[o].r){tree[o].num=0;return tree[o].l;}
        if(ad>tree[o<<1].num)res=update(ad=ad-tree[o<<1].num,o<<1|1);
        else res=update(ad,o<<1);
        pushup(o);
        return res;
    }
    int query(int L,int R,int o)
    {
        if(tree[o].l==L&&tree[o].r==R)return tree[o].num;
        int mid=(tree[o].l+tree[o].r)>>1;
        if(mid>=R)return query(L,R,o<<1);
        else if(mid<L)return query(L,R,o<<1|1);
        else return query(L,mid,o<<1)+query(mid+1,R,o<<1|1);
    }
    int main()
    {
        table();
        while(scanf("%d%d",&n,&k)!=EOF)
        {
            for(int i=1;i<=n;i++)
                scanf("%s%d",s[i],&add[i]);
            build(1,n,1);
            int num=k,pos=update(num,1);
            for(int i=2;i<=n;i++)
            {
                if(add[pos]>=0)
                {
                    num=add[pos]+query(1,pos,1);
                    num%=(n-i+1);
                    num=(num-1+n-i+1)%(n-i+1)+1;
                    pos=update(num,1);
                }
                else
                {
                    num=-add[pos]+query(pos,n,1);
                    num%=(n-i+1);
                    num=(n-i+1)-num+1;
                    num=(num-1)%(n-i+1)+1;
                    pos=update(num,1);
                }
                if(p[i]==p[n])break;
            }
            printf("%s %d
    ",s[pos],p[n]);
        }
        return 0;
    }
  • 相关阅读:
    Scala中的构造器和高阶函数
    Scala中的元组
    Scala中的Map
    Scala中的If判断&While&For循环
    [改善Java代码]asList方法产生的List对象不可更改
    [改善Java代码]避开基本类型数组转换列表陷阱
    [改善Java代码]枚举和注解结合使用威力更大
    [改善Java代码]枚举项的数量限制在64个以内
    [改善Java代码]用枚举实现工厂方法模式更简洁
    [改善Java代码]在switch的default代码块中增加AssertionError错误
  • 原文地址:https://www.cnblogs.com/homura/p/6727800.html
Copyright © 2011-2022 走看看