HDU- Who Gets the Most Candies?

本文介绍了一种算法竞赛题目“谁得糖果最多”的解决方案。该问题涉及多名儿童围成一圈进行游戏,通过特定规则决定每轮出局的儿童,并根据其出局顺序分配糖果。文章详细阐述了使用线段树数据结构进行高效模拟的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                 Who Gets the Most Candies?

Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 262144/131072K (Java/Other)
Total Submission(s) : 15   Accepted Submission(s) : 7
Problem 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个人围成一圈第一个人跳出圈后会告诉你下一个谁跳出来跳出来的人
(如果他手上拿的数为正数,从他左边数A个,反之,从他右边数A个)
跳出来的人所得到的糖果数量和他跳出的顺序有关 所得的糖果数为 (假设他是第k个跳出的) 则他得到的糖数为k能被多少个数正数
 比如说 k = 6 ;  6 = 1*2*3*6 所以他得到的糖数为4;

思路:线段数 先算出N个人中,是第几个人(id)跳出来得到的糖果最多。然后模拟id遍 长到第id个人的name
线段树的结点中保存该区间内还剩多少人,每次update 删除一个人。*/
#include<stdio.h>
#include<string.h>
#include<math.h>
#define maxn 500004
int n,id;
struct node
{
    int left,right;
    int num;
};
node tree[3*maxn];
struct data
{
    int val;
    char name[15];
} boy[maxn];

int ans[maxn];
void build(int left,int right,int i)
{
    tree[i].left =left;
    tree[i].right =right;
    tree[i].num =right-left+1;
    if(tree[i].left ==tree[i].right )
        return ;
    build(left,(left+right)/2,2*i);
    build((left+right)/2+1,right,2*i+1);
}
int insert(int i,int x)
{
    tree[i].num--;
    if(tree[i].left==tree[i].right)
        return tree[i].left;
    if(x<=tree[i].num)
      return insert(2*i,x);
    else
      return insert(2*i+1,x-tree[2*i].num);
}
void count_ans()//这个函数最重要。
{
    int i,max,j;
    memset (ans,0,sizeof(ans));  //计算ans
    for (i = 1; i <= n; i ++)
    {
        ans[i] ++;
        for (j=2*i;j<= n;j+= i)
            ans[j] ++;
    }
     max = ans[1];
    id = 1;
    for (i = 2; i <= n; i ++) //找出第几个人跳出获得的糖最多
        if (ans[i] > max)
        {
            max = ans[i];
            id = i;
        }
}
int main ()
{
    int i,k,mod;
    while (~scanf ("%d %d",&n,&k))
    {
        count_ans();
        for (i = 1; i <= n; i ++)
            scanf ("%s %d",boy[i].name,&boy[i].val);
         build(1,n,1);
        mod =tree[1].num;
        int pos = 0;
        boy[0].val = 0;
        n = id;
        while (n --)
        {
            if (boy[pos].val > 0)  //k表剩余的人中从左起第k中出队(PS:k的求法是看别人的)
                k = ((k + boy[pos].val - 2)%mod + mod)%mod + 1;
            else
                k = ((k + boy[pos].val - 1)%mod + mod)%mod + 1;
            pos = insert(1,k);
            mod = tree[1].num;
        }
        printf ("%s %d\n",boy[pos].name,ans[id]);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/cancangood/p/3423904.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值