poj 2886 Who Gets the Most Candies? //线段树+约瑟夫环....

Who Gets the Most Candies?

Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 16809 Accepted: 5293
Case Time Limit: 2000MS

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

题意:n个人成约瑟夫环状,每个人有一个数字,一开始出去一个人,他的数字是x,右数的第x再出去,以此类推,每个出去的人可以得到一个F(p), F(p)=p的因子数,求游戏过程最大的F(p)。

1.预处理每个数的F(p)。

类似埃筛nlg可以求出(调和级数极限是ln(n+1)+0.5...)。(如果是单个数求因子数,为n^1.5)。

2.线段树维护一段区间有几个人。

3.约瑟夫环求出去的是第几个人。

(线段树好写,约瑟夫环是真的写跪了QAQ)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include<vector>
using namespace std;
#define LL long long
const int max_n = 1e6+6;
char name[max_n][15];int a[max_n];
int tree[4*max_n];
vector<int>p,val;
int vis[max_n];
int now;
void init(){
    for(int i=1;i<=6e5;i++){
        for(int j=i;j<=6e5;j=j+i){
            vis[j]++;
        }
    }
    p.push_back(1);val.push_back(1);
    for(int i=1;i<=1e6;i++){
        if(vis[i]>val[val.size()-1]){
            val.push_back(vis[i]);p.push_back(i);
        }
    }
    /*for(int i=0;i<p.size();i++){
        printf("%d %d\n",p[i],val[i]);
    }*/
}
void build(int l,int r,int root){
    if(l==r) {tree[root]=1;return;}
    int m=(l+r)>>1;
    build(l,m,root<<1);build(m+1,r,root<<1|1);
    tree[root]=tree[root<<1]+tree[root<<1|1];
}
void update(int l,int r,int root,int k,int v){
    if(l==r) {tree[root]=v;now=l;return;}
    int mid=(l+r)>>1;
    if(tree[root<<1]>=k) update(l,mid,root<<1,k,v);
    else update(mid+1,r,root<<1|1,k-tree[root<<1],v);
    tree[root]=tree[root<<1]+tree[root<<1|1];
}

int query(int l,int r,int root,int k){
    if(l==r)return l;
    int mid=(l+r)>>1;
    if(tree[root<<1]>=k) return query(l,mid,root<<1,k);
    return query(mid+1,r,root<<1|1,k-tree[root<<1]);
}
int main(){
    int n,k;
    init();
    while(scanf("%d%d",&n,&k)!=EOF){
        for(int i=1;i<=n;i++)scanf("%s%d",name[i],&a[i]);
        build(1,n,1);
        int pos=upper_bound(p.begin(),p.end(),n)-p.begin()-1;
        int turn=p[pos];
        for(int i=1;i<turn;i++){
            update(1,n,1,k,0);int ji;
            if(a[now]<0) ji=1;else ji=n-i;//左移右移区分开
            a[now]=a[now]%(n-i);//无用的循环
            if(a[now]==0) a[now]=ji;
            if(a[now]<0)a[now]=a[now]+(n-i+1);//循环当前的一圈
            k--;//前移一位
            k=(a[now]+k)%(n-i);
            if(k==0)k=n-i;//循环
        }
        int ans=query(1,n,1,k);
        printf("%s %d\n",name[ans],val[pos]);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值