POJ 2886 Who Gets the Most Candies? 数据结构+线段树+约瑟夫环+反素数

POJ 2886 Who Gets the Most Candies?
Time Limit:5000MS    Memory Limit:131072KB    64bit IO Format:%lld & %llu

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


解题思路:
n个小朋友做成一圈,每个人手上有一个数字,从第k个小朋友开始,这个小朋友出环获得糖果,然后按照他手上
的数字x决定下一个出环的小朋友,这个数字如果是正数,那就顺时针决定下x个小朋友,负数就是逆时针,让你
求出所有的小朋友出列之后谁获得的糖果最多,获得糖果的数量等于出环次序的因子的数量
如第六个出环的会获得4个糖果,因为6有(1,2,3,6)四个因子。
1,这题首先要能想到用线段树来做,如果你做过同样类型的题目你会知道要用线段树来维护当前区间存在多少个
小朋友,然后线段树要做的就是动态的维护这个值,然后能在根据要求让某个编号的小朋友出环
2,手动模拟一下这个过程就好理解了,然后我要传给线段树的就是现在要剔除第几个小朋友,线段树完成剔除并
告诉我它剔除的小朋友的编号。
3,那么其他的问题来了,如何决定每次出环的是第几个小朋友,这里涉及一个“约瑟夫环问题”,具体问题的
解释可以看网上其他的资料,那么解决的公式是:if(arry[is]>0)k = (k-1+arry[is]-1)%sum+1 ;
else k = ((k-1+arry[is])%sum+sum)%sum+1 ; 就在我的代码里这句。
4,求得一个数的因子数目的个数,这里用的是反素数表,并记录每一个反素数的因子的个数,
定义:【对于任何正整数x,其约数的个数记做g(x).例如g(1)=1,g(6)=4.
如果某个正整数x满足:对于任意i(0<i<x),都有g(i)<g(x),则称x为反素数】
仔细思考一下确实是这个反素数记录了每一个1到n之间的拥有最多因子的那个最小的数,正好用在本题上。
5,这个题就这三个重点【线段树】、【反素数】、【约瑟夫环】

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
///rt<<1|1 ;相当于rt*2+1
const int maxn = 500005;
int sum[maxn<<2];
int ans[maxn<<2];
int n,k;
int idx ;
///反素数表
int a[38]= {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};
///对应因子个数
int b[38]= {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};
int getid(int x) {
    //printf("s = %d\n",(int)sizeof(a));
//    int temp =lower_bound(a,a+37,x)-a;
//    idx = a[temp] ;
//    //printf("idx = %d\n",idx);
//    return b[temp];
    int i=0 ;
    while (a[i] <= n)i++;
        idx=a[i-1];
        return b[i-1];

}
int is ;
void PushUP(int rt) {///更新父节点的值
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void build(int l,int r,int rt) {///建树范围(1,n),根节点为1。
    if (l == r) {
        sum[rt] = 1 ;
        return ;
    }
    int m = (l + r) >> 1;
    build(lson);
    build(rson);
    PushUP(rt);
}
void update(int p,int add,int l,int r,int rt) {///第p个节点增加add,范围(1,n),根节点为1。
    if (l == r) {
        sum[rt] += add;
        is = l ;
        return ;
    }
    int m = (l + r) >> 1;
    if (p<=sum[rt<<1]) update(p , add , lson);///这里是最重要的
    else update(p-sum[rt<<1] , add, rson);
    PushUP(rt);
}
char name[maxn][20] ;
int arry[maxn] ;
int main() {
    //freopen("in.txt","r",stdin) ;
    while(~scanf("%d%d",&n,&k)) {
        for(int i=1; i<=n; i++) {
            scanf("%s%d",name[i],&arry[i]);
        }
        int s = getid(n) ;
        //printf("第%d个人出来最多为%d\n",idx,s);
        build(1,n,1) ;///建树
        ///初始每个人都有
        int sum = n ;///总人数
        for(int i=0;i<idx-1;i++){///算出下一个人的位置在第几个人的位置
            sum-- ;
            update(k,-1,1,n,1) ;///在p个人的位置减一
            if(arry[is]>0){
                k = (k-1+arry[is]-1)%sum+1 ;
            }else{
                k = ((k-1+arry[is])%sum+sum)%sum+1 ;
            }
        }
        update(k,-1,1,n,1) ;
        printf("%s %d\n",name[is],s) ;
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值