POJ 2886——Who Gets the Most Candies? (线段树单点更新+因子数打表)

Who Gets the Most Candies?

Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 16050 Accepted: 5076
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

 

第一次写博客,也不知道会怎样(有误望指出qwq)...主要是为了理清思路,顺便分享出来。

首先看懂题意:n个人顺时针坐成一个圈,每个人带有一个非0的val值,从第k个人开始出队,如果val【k】>0,则向左数val【k】个数,否则向右走val【k】个...直到全部出队。其中,第i个出队的人有i的因子数个糖果(比如i==4,则有1,2,4三个),问:谁会获得最多糖果?

看了网上很多题解,反素数?什么玩意啊(反正不是很理解@-@)

我的做法是首先对于n个人,先打表求出所有可能的糖果数,然后求出f[i]的最大值,然后模拟i次得到第i的出队的人是谁就行。

那么问题来了,怎么模拟呢,因为原题意是一个顺时针的环,那么这个问题就很像约瑟夫问题了。首先,把环展成一个1--n的坐标轴,每次模拟第k个人出队,顺便得到下一次出队人的位置,val【k】>0则向坐标轴的右边走,反之则向左边走。模拟的过程需用到线段树,具体看代码咯~
 

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;

const int maxn = 5e5+5;
int tree[maxn<<2];//tree[i]结点表示区间还有多少人,初始时有r-l+1个

struct children
{
    char name[12];
    int val;
}c[maxn];

void push_up(int rt)
{
    tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}

void build(int l,int r,int rt)//建树
{
    if(l==r)
    {
        tree[rt]=1;
        return;
    }
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    push_up(rt);
}

int update(int k,int l,int r,int rt)//在(l,r)中踢出第k个人,k是每次剩余人数中的相对位置
{
    tree[rt]--;//每次有人出队更新,路过的结点都要-1
    if(l==r)
    {
        tree[rt]=0;
        return l;//走到叶子结点了则表明该位置是下一个要出队的
    }
    int mid=(l+r)>>1;
    if(k<=tree[rt<<1]) return update(k,lson);//要在(l,r)区间踢出第k个人,若p小于等于tree[rt<<1],则在左半区间踢第p个人
    else               return update(k-tree[rt<<1],rson);//否则在右半区间踢第(p-tree[rt<<1])个人(tree[rt<<1]为左半区间人数)
}

int ans[maxn];
int Findid(int n)
{
    int i,j;
    for(i=1;i<=n;i++)//打表
    {
        ans[i]++;
        for(j=i+i;j<=n;j+=i)
        {
            ans[j]++;
        }
    }
    int m=ans[1],id=1;
    for(i=2;i<=n;i++)
    {
        if(m<ans[i])
        {
            m=ans[i];
            id=i;
        }
    }
    return id;
}

int main()
{
    int n,m,k,i,j,id,pos,mod;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        for(i=1;i<=n;i++) scanf("%s%d",c[i].name,&c[i].val);
        memset(ans,0,sizeof(ans));
        id=Findid(n);
        build(1,n,1);

        mod=n;
        c[0].val=0;
        pos=0;
        m=id;

        while(m--)//模拟id次
        {
            if(c[pos].val>0) k=(k+c[pos].val-2)%mod+1;          //先-1再+1是为了防止取模为0的情况
            else             k=((k+c[pos].val-1)%mod+mod)%mod+1;
            pos=update(k,1,n,1);
            mod--;
        }
        printf("%s %d\n",c[pos].name,ans[id]);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值