zoukankan      html  css  js  c++  java
  • Who Gets the Most Candies? 线段树的建立更新和反素数

    Problem 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. IfA 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
     **************************************************************************************************************************************************************************************************************
    线段树:
    先把总体建成一个树,然后把已选到的更新树,即从树中删去
    ***************************************************************************************************************************************************************************************************************
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 
     5 using namespace std;
     6 
     7 #define L(rt) (rt<<1)
     8 #define R(rt) (rt<<1|1)
     9 
    10 const int maxn=500010;
    11 
    12 int n,id;
    13 
    14 struct node{
    15     int l,r,sum;    //sum 表 该区间剩余人数
    16 }tree[maxn*3];
    17 
    18 struct date{
    19     int val;
    20     char name[15];
    21 }boy[maxn];
    22 
    23 int ans[maxn]; //ans[i]保存第i个人跳出能得到的糖果数量
    24 
    25 void build(int l,int r,int rt){
    26     tree[rt].l=l;
    27     tree[rt].r=r;
    28     tree[rt].sum=r-l+1;
    29     if(l==r)
    30         return ;
    31     int mid=(l+r)>>1;
    32     build(l,mid,L(rt));
    33     build(mid+1,r,R(rt));
    34 }
    35 
    36 int update(int key,int rt){
    37     tree[rt].sum--;
    38     if(tree[rt].l==tree[rt].r)
    39         return tree[rt].l;
    40     if(tree[L(rt)].sum>=key)
    41         update(key,L(rt));
    42     else
    43         update(key-tree[L(rt)].sum,R(rt));
    44 }
    45 
    46 void Solve(){   //计算ans
    47     memset(ans,0,sizeof(ans));
    48     for(int i=1;i<=n;i++){
    49         ans[i]++;
    50         for(int j=2*i;j<=n;j+=i)
    51             ans[j]++;
    52     }
    53     int max1=ans[1];
    54     id=1;
    55     for(int i=2;i<=n;i++)   //找出第几个人跳出获得的糖最多
    56         if(ans[i]>max1){
    57             max1=ans[i];
    58             id=i;
    59         }
    60 }
    61 
    62 int main(){
    63 
    64     //freopen("input.txt","r",stdin);
    65 
    66     int i,k,mod;
    67     while(~scanf("%d%d",&n,&k)){
    68         Solve();
    69         for(i=1;i<=n;i++)
    70             scanf("%s%d",boy[i].name,&boy[i].val);
    71         build(1,n,1);
    72         mod=tree[1].sum;
    73         int pos=0;
    74         boy[0].val=0;
    75         n=id;
    76         while(n--){
    77             if(boy[pos].val>0)  //k表剩余的人中从左起第k中出队
    78                 k=((k-1+boy[pos].val-1)%mod+mod)%mod+1;
    79             else
    80                 k=((k-1+boy[pos].val)%mod+mod)%mod+1;
    81             pos=update(k,1);
    82             mod=tree[1].sum;
    83         }
    84         printf("%s %d
    ",boy[pos].name,ans[id]);
    85     }
    86     return 0;
    87 }
    View Code
  • 相关阅读:
    强人工智能基本问题:全局控制与自组织
    程序员,为未来准备好了吗?
    强人工智能基本问题:自上而下,还是自下而上。
    强人工智能基本问题:神经网络分层还是不分层
    什么阻碍了强人工智能的发展
    人类和强人工智能的关系(人类会被灭掉吗?)
    为什么需要强人工智能
    神经网络和机器学习、强人工智能
    重新开张!
    Xcode文件乱序
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3485847.html
Copyright © 2011-2022 走看看