POJ 题目2513 Who Gets the Most Candies?(线段树)


Who Gets the Most Candies?

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

Source

POJ Monthly--2006.07.30, Sempr

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page
  Go Back  To
top

题意:N个人围成一圈第一个人跳出圈后会告诉你下一个谁跳出来跳出来的人(如果他手上拿的数为正数,从他左边数A个,反之,从他右边数A个) 跳出来的人所得到的糖果数量和他跳出的顺序有关 所得的糖果数为 (假设他是第k个跳出的) 则他得到的糖数为k能被多少个数正数 比如说 k = 6 ;  6 = 1*2*3*6 所以他得到的糖数为4;

思路:线段数 先算出N个人中,是第几个人(id)跳出来得到的糖果最多。然后模拟id遍 长到第id个人的name

线段树的结点中保存该区间内还剩多少人,每次update 删除一个人。

题目思路转:http://www.cnblogs.com/jackge/archive/2013/04/23/3038576.html

ac代码

#include<stdio.h>
#include<string.h>
int n,k;
struct s
{
	char name[15];
	int val;
}b[500100];
struct tree
{
	int l,r,sum;
}node[500100<<2];
int ans[500100],id,maxn;
void solve()
{
	memset(ans,0,sizeof(ans));
	int i,j;
	for(i=1;i<=n;i++)
	{
		ans[i]++;
		for(j=2*i;j<=n;j+=i)
		{
			ans[j]++;
		}
	}
	maxn=ans[1],id=1;
	for(i=2;i<=n;i++)
	{
		if(maxn<ans[i])
		{
			maxn=ans[i];
			id=i;
		}
	}
}
void build_tr(int l,int r,int tr)
{
	node[tr].l=l;
	node[tr].r=r;
	node[tr].sum=r-l+1;
	if(l==r)
		return;
	int mid=(l+r)>>1;
	build_tr(l,mid,tr<<1);
	build_tr(mid+1,r,tr<<1|1);
}
int query(int key,int tr)
{
	node[tr].sum--;
	if(node[tr].l==node[tr].r)
	{
		return node[tr].l;
	}
	if(key<=node[tr<<1].sum)
		query(key,tr<<1);
	else
		query(key-node[tr<<1].sum,tr<<1|1);
}
int main()
{
	//int n,k;
	while(scanf("%d%d",&n,&k)!=EOF)
	{
		int i;
		for(i=1;i<=n;i++)
		{
			scanf("%s%d",b[i].name,&b[i].val);
		}
		build_tr(1,n,1);
		solve();
		int p=id,idx;
		for(i=0;i<p;i++)
		{
			n--;

			idx=query(k,1);//原始位置
			if(n==0)
				break;
			if(b[idx].val>0)
			{
				k=(k-1+b[idx].val-1)%n+1;//下一次掉出的人的编号
			}
			else
				k=((k-1+b[idx].val)%n+n)%n+1;
		}
		printf("%s %d\n",b[idx].name,maxn);
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-25 00:51:42

POJ 题目2513 Who Gets the Most Candies?(线段树)的相关文章

poj 2886 Who Gets the Most Candies?(线段树+约瑟夫环+反素数)

Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 9934   Accepted: 3050 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 o

POJ 3468 A Simple Problem with Integers(线段树)

题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 56005   Accepted: 16903 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with

【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)

[POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11294   Accepted: 3091 Description Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I still remembe

poj 3468 A Simple Problem with Integers (线段树 成段更新 加值 求和)

题目链接 题意: 只有这两种操作 C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000."Q a b" means querying the sum of Aa, Aa+1, ... , Ab. 分析:自己写的有点麻烦了,写的时候手残+脑残,改了好久. val+lazy*(r-l+1)表示和,如果lazy==0表示当前区间加的值不统一. 1 #include <iostream

POJ 2763 Housewife Wind LCA转RMQ+时间戳+线段树成段更新

题目来源:POJ 2763 Housewife Wind 题意:给你一棵树 2种操作0 x 求当前点到x的最短路 然后当前的位置为x; 1 i x 将第i条边的权值置为x 思路:树上两点u, v距离为d[u]+d[v]-2*d[LCA(u,v)] 现在d数组是变化的 对应每一条边的变化 他修改的是一个区间 用时间戳处理每个点管辖的区域 然后用线段树修改 线段树的叶子节点村的是根到每一个点的距离 求最近公共祖先没差别 只是堕落用线段树维护d数组 各种错误 4个小时 伤不起 #include <cs

Poj2886Who Gets the Most Candies?线段树

约瑟夫环用线段数搞,一脸搞不出来的样子.反素数,太神了,先打表,然后就可以 O(1)找到因子数最多的.ps:哎.这题也是看着题解撸的. #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cs

【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记

A Simple Problem with Integers Time Limit:5000MS   Memory Limit:131072K Case Time Limit:2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each

poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 58269   Accepted: 17753 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

POJ 3468 A Simple Problem with Integers(线段树 区间更新)

Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval. In