[原创]POJ 2886 Who Gets the Most Candies? [线段树-单点更新]【数据结构】
[原创]POJ 2886 Who Gets the Most Candies? [线段树-单点更新]【数据结构】
2016-11-15 20:30:35 Tabris_ 阅读数:198
博客爬取于2020-06-14 22:42:35
以下为正文
版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/53176786
题目链接:http://poj.org/problem?id=2886
------------------------------------.
Who Gets the Most Candies?
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 13899 Accepted: 4397
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
-----------------------------------.
题目大意:
就是有n个人围城一圈,第一次第k个人离开,接下来离开的人是上一次离开的人的位置相对手上的数字相对位置上的人。
如果正的就是顺时针,负的就是逆时针
然后输出的是获得糖果最多的人。这个人获得的糖果数是第p个离开。是p的因子个数。
解题思路:
首先对于n个人来说,获得的最多的糖果个数和其是第几个离开的已经是确定的了,就是一个反素数,这个只要打个表就行了。
那么我们的任务就只剩下了 怎么找第K个离开的人。
每一次我们能够知道的离开的编号是哪一个,所以我们只要一个个的找就行了,但是直接暴力的找的话复杂度是,n又很大 一定会超时。
所以我们采取的就是用线段树维护的方法,每次离开的人放到树上,然后找下一个人,这样的话复杂度就降到了.
维护的方法就是在树上的节点存储这个区间中剩下的没有离开的人的个数,然后更新的时候减掉1就行了.
这个与POJ2828一样 详情戳这里
附本题代码
----------------------------.
1 | # include <stdio.h> |