zoukankan      html  css  js  c++  java
  • POJ 2886 Who Gets the Most Candies?

    Who Gets the Most Candies?

    Time Limit: 5000ms
    Memory Limit: 131072KB
    This problem will be judged on PKU. Original ID: 2886
    64-bit integer IO format: %lld      Java class name: Main

    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) andK (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

    Source

     
    解题:线段树嘛,注意问题啊。。。注意那个什么屁转向啊,搞得我晕头转向。。。也可以用树状数组进行解题啊,二分后即可获得下标
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 using namespace std;
     5 const int maxn = 500010;
     6 int tree[maxn<<2],val[maxn],ans[maxn],n,k;
     7 char name[maxn][16];
     8 void pushup(int v) {
     9     tree[v] = tree[v<<1] + tree[v<<1|1];
    10 }
    11 void build(int L,int R,int v) {
    12     if(L == R) {
    13         tree[v] = 1;
    14         return;
    15     }
    16     int mid = (L + R)>>1;
    17     build(L,mid,v<<1);
    18     build(mid+1,R,v<<1|1);
    19     pushup(v);
    20 }
    21 int query(int L,int R,int lt,int rt,int v) {
    22     if(lt <= L && rt >= R) return tree[v];
    23     int mid = (L + R)>>1,ans = 0;
    24     if(lt <= mid) ans += query(L,mid,lt,rt,v<<1);
    25     if(rt > mid) ans += query(mid,R,lt,rt,v<<1|1);
    26     return ans;
    27 }
    28 int update(int L,int R,int p,int v) {
    29     if(L == R) {
    30         tree[v]--;
    31         return L;
    32     }
    33     int mid = (L + R)>>1,o;
    34     if(tree[v<<1] >= p) o = update(L,mid,p,v<<1);
    35     else o = update(mid+1,R,p-tree[v<<1],v<<1|1);
    36     pushup(v);
    37     return o;
    38 }
    39 void preSolve() {
    40     for(int i = 1; i < maxn; ++i) {
    41         ans[i]++;
    42         for(int j = i<<1; j < maxn; j += i)
    43             ans[j]++;
    44     }
    45 }
    46 int main() {
    47     preSolve();
    48     while(~scanf("%d %d",&n,&k)) {
    49         for(int i = 1; i <= n; ++i)
    50             scanf("%s %d",name[i],val+i);
    51         build(1,n,1);
    52         int ret = 0,id = 0,pos = k;
    53         for(int i = 1; i <= n; ++i) {
    54             int o = update(1,n,k,1);
    55             if(ans[i] > ret) {
    56                 ret = ans[i];
    57                 id = o;
    58             }
    59             if(i == n) break;
    60             if(val[o] > 0)
    61                 k = ((val[o] + k - 1)%tree[1] + tree[1])%tree[1];
    62             else if(val[o] < 0)
    63                 k = ((k + val[o])%tree[1] + tree[1])%tree[1];
    64             if(k == 0) k = tree[1];
    65         }
    66         printf("%s %d
    ",name[id],ret);
    67     }
    68     return 0;
    69 }
    View Code
  • 相关阅读:
    20180827 360笔试客观题
    20180821 hikvision笔试
    20180820 美团一面
    20180811 网易
    20180811 多益网络
    20180810 多益网络模拟笔试
    hbase --知识点总结
    flume知识点总结
    oracle --hint总结
    查看oracle的执行计划
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4444072.html
Copyright © 2011-2022 走看看