• POJ 2886 Who Gets the Most Candies?


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

    Source

     
    出了很多数据都正确,不由得怀疑理解错题了,结果真错了,他那第p个孩子中的p不是原来的编号,而是一次站出的顺序
    #include <iostream>
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #define N 500010
    #define M 15
    using namespace std;
    bool ch[N];
    int a[N],b[N],sum[N];
    struct num
    {
        char s1[M];
        int val;
    }in[N];
    struct tree
    {
        int l,r,sum;
    }d[N*4];
    int n,m,res,ans;
    int main()
    {
        //freopen("data.in","r",stdin);
        void pre_build(int k,int l,int r);
        void get(int k,int l,int r);
        void deal(int k,int tar);
        memset(ch,true,sizeof(ch));
        for(int i=2;i<=N-10;i++)
        {
            if(ch[i])
            {
                for(int j=2;i*j<=N-10;j++)
                {
                    ch[i*j]=false;
                }
            }
        }
        int Top=0;
        for(int i=2;i<=N-10;i++)
        {
            if(ch[i])
            {
                a[Top++]=i;
            }
        }
        sum[1]=1;
        memset(b,0,sizeof(b));
        for(int i=2;i<=N-10;i++)
        {
            int co=i,x;
            for(int j=0;j<=Top-1;j++)
            {
                if(ch[co])
                {
                    b[j]++;
                    x=j;
                    break;
                }
                while(co%a[j]==0)
                {
                    b[j]++;
                    x=j;
                    co=co/a[j];
                }
                if(co==1)
                {
                    break;
                }
            }
            int s =1;
            for(int j=0;j<=x;j++)
            {
                s=s*(b[j]+1);
                b[j]=0;
            }
            sum[i]=s;
        }
        while(scanf("%d %d",&n,&m)!=EOF)
        {
            for(int i=1;i<=n;i++)
            {
                scanf("%s %d",in[i].s1,&in[i].val);
            }
            int Max,pt;
            pre_build(1,1,n);
            Max=sum[1];
            pt=m;
            for(int i=1;i<=n;i++)
            {
                if(i==1)
                {
                    int s=m%n;
                    if(s==0)
                    {
                        s=n;
                    }
                    deal(1,s);
                    if(Max<sum[i])
                    {
                        Max=sum[i];
                        pt=res;
                    }
                    continue;
                }
                ans=0;
                get(1,1,res);
                int uv;
                if(in[res].val<0)
                {
                    uv = (in[res].val*-1)%(n-i+1);
                    if(uv==0)
                    {
                        uv=n-i+1;
                    }
                    uv = (n-i+1)-uv+1+ans;
                    uv = uv%(n-i+1);
                }else
                {
                    uv = (in[res].val)%(n-i+1);
                    uv = uv+ans;
                    uv = uv%(n-i+1);
                }
                if(uv==0)
                {
                    uv = n-i+1;
                }
                deal(1,uv);
                if(Max<sum[i])
                {
                    Max=sum[i];
                    pt=res;
                }
            }
            printf("%s %d
    ",in[pt].s1,Max);
        }
        return 0;
    }
    void pushup(int k)
    {
        d[k].sum = d[k*2].sum+d[k*2+1].sum;
    }
    void pre_build(int k,int l,int r)
    {
        d[k].l = l;
        d[k].r = r;
        if(l==r)
        {
            d[k].sum=1;
            return ;
        }
        int mid=(l+r)/2;
        pre_build(k*2,l,mid);
        pre_build(k*2+1,mid+1,r);
        pushup(k);
    }
    void get(int k,int l,int r)
    {
        if(d[k].l==l&&d[k].r==r)
        {
            ans+=d[k].sum;
            return ;
        }
        int mid=(d[k].l+d[k].r)/2;
        if(mid>=r)
        {
            get(k*2,l,r);
        }else if(mid<l)
        {
            get(k*2+1,l,r);
        }else
        {
            get(k*2,l,mid);
            get(k*2+1,mid+1,r);
        }
    }
    void deal(int k,int tar)
    {
        if(d[k].l==d[k].r)
        {
            res=d[k].l;
            d[k].sum=0;
            return ;
        }
        if(d[k*2].sum<tar)
        {
            deal(k*2+1,tar-d[k*2].sum);
        }else
        {
            deal(k*2,tar);
        }
        pushup(k);
    }
    
    

  • 相关阅读:
    Leetcode Binary Tree Paths
    Leetcode Lowest Common Ancestor of a Binary Tree
    Leetcode Lowest Common Ancestor of a Binary Search Tree
    Leetcode Path Sum
    Leetcode Symmetric Tree
    Leetcode Invert Binary Tree
    Leetcode Same Tree
    Leetcode Maximum Depth of Binary Tree
    Python Json&Pickle&模块
    Python Shelve模块
  • 原文地址:https://www.cnblogs.com/pangblog/p/3290222.html
Copyright © 2020-2023  润新知