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 ≤ 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

     
    题解:
    反素数+线段树(删除编号为k的元素)
    反素数:对于任何正整数x,其约数的个数记做g(x).例如g(1)=1,g(6)=4.如果某个正整数x满足:对于任意i(0<i<x),都有g(i)<g(x),则称x为反素数(百度词条)·
    tr数组记下每个结点所代表区间的长度,然后每次查找当前序列排名为k的元素在原序列中的编号
     1 /*
     2   挑战2-线段树
     3   反素数+线段树(删除编号为k的元素)
     4   by-solution
     5 */
     6 #include<cstdio>
     7 #include<cstdlib>
     8 #include<cstring>
     9 #include<iostream>
    10 #include<cmath>
    11 #include<algorithm>
    12 #define ll long long
    13 #define ls x<<1
    14 #define rs x<<1|1
    15 using namespace std;
    16 
    17 const int N = 500010;
    18 
    19 int n,k,ans,tr[N*4];
    20 int a[40]={1,2,4,6,12,24,36,48,60,120,180,240,360,720,840,1260,1680,2520,5040,7560,10080,15120,20160,25200,27720,45360,50400,55440,83160,110880,166320,221760,277200,332640,498960,500001},b[40] = {1,2,3,4,6,8,9,10,12,16,18,20,24,30,32,36,40,48,60,64,72,80,84,90,96,100,108,120,128,144,160,168,180,192,200,1314521};
    21 
    22 struct Node {
    23   int v;
    24   char s[11];
    25 }q[N];
    26 
    27 int gi() {
    28   int x=0,o=1; char ch=getchar();
    29   while(ch!='-' && (ch<'0'||ch>'9')) ch=getchar();
    30   if(ch=='-') ch=getchar(),o=-1;
    31   while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
    32   return o*x;
    33 }
    34 
    35 void build(int x, int l, int r) {
    36   tr[x]=r-l+1;
    37   if(l==r) return;
    38   int mid=(l+r)>>1;
    39   build(ls,l,mid),build(rs,mid+1,r);
    40 }
    41 
    42 int query(int x, int l, int r, int k) {
    43   tr[x]--;
    44   if(l==r) return l;
    45   int mid=(l+r)>>1;
    46   if(k<=tr[ls]) return query(ls,l,mid,k);
    47   else return query(rs,mid+1,r,k-tr[ls]);
    48 }
    49 
    50 int main() {//poj2886
    51   while(~scanf("%d%d", &n, &k)) {
    52     int cnt=0,p,pos,i,n1=n;
    53     for(i=1; i<=n; i++) {
    54       scanf("%s%d", q[i].s, &q[i].v);
    55     }
    56     build(1,1,n);
    57     while(a[cnt]<=n) cnt++;
    58     ans=b[cnt-1],p=a[cnt-1];
    59     while(p--) {
    60       pos=query(1,1,n1,k),n--;
    61       if(!n) break;
    62       if(q[pos].v>=0) k=(k-1+q[pos].v-1)%n+1;//下一个要ti的人当前的编号,很妙~
    63       else k=((k-1+q[pos].v)%n+n)%n+1;//mod +mod mod,转圈圈的题算完答案先mod一发再说~
    64     }//第一个k-1意思是从当前第一个开始数,第二个-1意思是当前的数已经被删除,如果要往右数应从上一个数开始
    65     printf("%s %d
    ", q[pos].s,ans);
    66   }
    67   return 0;
    68 }
     
  • 相关阅读:
    loadrunner数据库MySQL参数化列表乱码问题
    [MySQL]导入导出
    [MySQL]命令行工具和基本操作
    [MySQL]安装和启动
    win7下loadrunner创建mysql数据库参数化问题解决
    Win7-64bit系统下安装mysql的ODBC驱动
    loadrunner个版本历程
    性能分析与调优的原理
    性能分析与调优的原理
    loadrunner解决“服务器正在运行中”方法
  • 原文地址:https://www.cnblogs.com/HLXZZ/p/7471448.html
Copyright © 2011-2022 走看看