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 }
     
  • 相关阅读:
    前端脚手架的那些事儿
    CSS重置默认样式reset.css代码模板
    Web 3.0 前瞻:基于区块链的下一代浏览器
    关键词定位是网站推广的基础
    6年架构师针对web前端小白,作出的职业规划建议
    和程序员约会的优点和缺点
    如何在软件开发中避免出现漏洞
    Linux下修改时区
    前端需要掌握的Nginx知识
    Nginx入门指南
  • 原文地址:https://www.cnblogs.com/HLXZZ/p/7471448.html
Copyright © 2011-2022 走看看