首页 > 其他分享 >poj 2886 Who Gets the Most Candies? (线段树单点更新应用)

poj 2886 Who Gets the Most Candies? (线段树单点更新应用)

时间:2023-07-26 17:32:36浏览次数:36  
标签:2886 小孩 Candies Who int cnt2 child return children


                        poj 2886 Who Gets the Most Candies?


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 theK-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. LetA denote the integer. If A is positive, the next child will be theA-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, thep-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 integersN (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 个小孩围成一圈,他们被顺时针编号为 1 到 N。每个小孩手中有一个卡片,上面有一个非 0 的数字,游戏从第 K 个小孩开始,他告诉其他小孩他卡片上的数字并离开这个圈,他卡片上的数字 A 表明了下一个离开的小孩,如果 A 是大于 0 的,则下个离开的是左手边第 A 个,如果是小于 0 的,则是右手边的第 -A 个小孩。游戏将直到所有小孩都离开,在游戏中,第 p 个离开的小孩将得到 F(p) 个糖果,F(p) 是 p 的约数的个数,问谁将得到最多的糖果。输出最幸运的小孩的名字和他可以得到的糖果。

解题思路:建树:节点存的是他所包含的叶结点个数。

                    更新树:用于删除叶结点。

                    反素数:打表。



#include<stdio.h>
#include<string.h>
int antiprime[]={//反素数
	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,
	554400
};

int factor[]={//反素数约数个数
	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,216
};
const int N = 500005 * 4;
int S[N], V[N], L[N], R[N];
void build(int u, int l, int r) {
	L[u] = l;
	R[u] = r;
	S[u] = r - l + 1;
	if (l == r) {
		return;
	}
	int mid = (l + r) / 2;
	build(u * 2, l, mid);
	build(u * 2 + 1, mid + 1, r);
}
int modify(int u, int x) {
	S[u]--;
	if (L[u] == R[u]) {
		return L[u];
	}
	if (S[u * 2] >= x) {
		return modify(u * 2, x);      
	}
	else {
		return modify(u * 2 + 1, x - S[u * 2]);     //如果所查结点不在左子树中,除掉在左子树种的部分,其余的部分在右子树中查找
	}
}
char name[N][50];
int num[N];
int main() {
	int n, k;
	while (scanf("%d%d", &n, &k) == 2) {
		for (int i = 1; i <= n; i++) {
			scanf("%s%d", name[i], &num[i]);
		}
		build(1, 1, n);
		int cnt = 0;
		while (antiprime[cnt] <= n) cnt++;
		cnt--;
		int cnt2 = 0;  
		num[cnt2] = 0;  
		for(int i = 0; i < antiprime[cnt]; i++)  //第antiprime[cnt]个小朋友,有最多的糖果
		{  
			if(num[cnt2] > 0)  
			{  
				k = ((k + num[cnt2] - 2) % S[1] + S[1]) % S[1] + 1;     
                  //"-2“一是因为考虑k + num[cnt2] == 0的情况,一是因为当前节点要被删除,向右移动要少记一位
                  //第一个% S[1] 是当k + num[cnt2] - 2大于目前小朋友数量时,把它除回来
                  //后面的  +S[1]) % S[1]是考虑负数的情况,最后+1归位(之前为了0减掉的1)  
                         }  
			else  
			{  
				k = ((k + num[cnt2] - 1) % S[1] + S[1]) % S[1] + 1;  
			}  
			cnt2 = modify(1, k);  
		}  
		printf("%s %d\n", name[cnt2], factor[cnt]);  
	}
	return 0 ;
}






标签:2886,小孩,Candies,Who,int,cnt2,child,return,children
From: https://blog.51cto.com/u_16206136/6858301

相关文章

  • UnKnowhost mysql
    解决"Unknownhostmysql"的步骤在解决"Unknownhostmysql"的问题之前,我们首先要了解这个错误的含义。当我们在连接MySQL数据库时,如果遇到"Unknownhostmysql"错误,意味着我们的程序无法找到MySQL数据库的主机。此错误通常是由以下几种原因引起的:数据库主机名错误:可......
  • (GCC) gcc编译选项 -Wl, -start-group,whole-archive,-Wl, Bstatic
    1.start-group编译选项假设程序x依赖三个静态库:libX1.a、libX2.a和libX3.a,而libX2.a又依赖libX1.a,libX3.a依赖libX2.a和libX1.a,正常情况下的CMakeLists.txt格式如下target_link_libraries(xlibX1.alibX2.alibX3.a)但也可以偷懒,不关心静态库的顺序问题,ld......
  • whoami
    whoami打印当前有效的用户ID对应的名称概要whoami[OPTION]...主要用途打印当前有效的用户ID对应的名称。选项--help显示帮助信息并退出。--version显示版本信息并退出。返回值返回0表示成功,返回非0值表示失败。例子[root@localhost~]#whoamiroot......
  • who
    who显示当前所有登陆用户的信息。概要who[OPTION]...[file][ami]主要用途当没有给出非选项参数时,按以下字段顺序为每个当前用户打印信息:登录用户名称,终端信息,登录时间,远程主机或Xdisplay。当用户执行whoami时,只显示运行该命令的用户的信息。选项-a,--all......
  • P2886题解
    题目大意给定起点\(S\)和终点\(T\),求从起点到终点恰好经过\(N\)(\(N\)给定)条边的最短路径。solution发现本题点数巨多,但是边数小到可怜,我们可以只保留了一部分点,先判断连通性,不连通直接输出即可。当\(S\)和\(T\)连通时,我们设\(G^k\)为经过\(k\)条边后最短路的邻......
  • EveryWhereIsSparserThanWhole(Construction)
    [ARC161D]EverywhereisSparserthanWhole(Construction)构造题,重在思路,代码不难。考虑有一个性质,既然部分比整体更稀疏,那么需要每个点的入度都\(>d\),因为这样删去之后\(\div(n-1)\)才会减小。形式化的说,需要满足\[记cnt=\min(度_i(1\lei\len))\\d>\dfrac{nd-cnt}{n......
  • PANDACU: Second Hand Brand Bags Used Handbag Brand Purse Shoulder Bags Wholesale
    PANDACUisatrustedwholesalesupplierspecializinginsecond-handbrandbags,includingusedhandbagsandbrandpurses.Theyofferawiderangeofoptionsforretailers,resellers,anddistributorsseekinghigh-qualityshoulderbagsatwholesaleprices.......
  • PANDACU: second hand luxury bag and wallet bags designer used leather branded ba
    PANDACUisareputablewholesalesupplierspecializinginsecond-handluxurybagsandwallets.Theyofferawideselectionofdesignerusedleatherbags,includingbrandedoptions.Withafocusonprovidinghigh-qualityproducts,PANDACUcaterstoretaile......
  • dangdang_newhotsales_recent7days
    童书文学成功/励志管理传记社会科学科普读物经济投资理财......
  • POJ2886线段树 Joseph游戏(单点更新)
    题目:WhoGetstheMostCandies? 题意:1.n个人进行Joseph游戏,游戏共p轮(p为思路:用相对坐标来处理,例如这一轮出局的是p,下一个要+m,则p出局时p+1就变成了p(因为是相对坐标),则下一个人应该是p+m-1,再注意循环和m的正负的处理,不要忘了取模。2.求解原始序号时维护一棵线段树,类似上一题插队......