poj2886

Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 8527   Accepted: 2581
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 ≤ 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
线段树,每段区间维护未jump out的人数;当第k个人离开之后,如何找下一个要离开的位置。因为模运算的结果是[0,...n -1]([1,2,...,k-1,k,k+1,...,n]与[0,1,...,n-1]
一一对应),所以先k减1,如果第k个人的cards大于0,也就是说d第k个人离开之后,应从左手边开始数,当第k个人离开之后[1,2,...,k-1,k,k+1,...,n]就变成了[1,2,...,k-1,k+1,...,n],
也就是第k+1(如果有的话)就变成了第k个,...( 依次下去),所以k需要再减1。模运算完之后需要再加上1。
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <math.h>
 5 #define N 500005
 6 #define mid(x, y) ((x + y) >> 1)
 7 #define pp(x) (x << 1)
 8 using namespace std;
 9 int segtree[N << 2];
10 char name[N][11];
11 int p[N], pos[N];
12 void build(int l, int r, int p)
13 {
14     segtree[p] = r - l + 1;
15     if (l < r)
16     {
17         build(l, mid(l, r), pp(p));
18         build(mid(l, r) + 1, r, pp(p) + 1);
19     }
20 }
21 int update(int l, int r, int p, int v)
22 {
23     if (l >= r)
24     {
25         segtree[p] = 0;
26         return l;
27     }
28     int ans;
29     if (segtree[pp(p)] >= v)
30         ans = update(l, mid(l, r), pp(p), v);
31     else
32         ans = update(mid(l, r) + 1, r, pp(p) + 1, v - segtree[pp(p)]);
33     segtree[p] = segtree[pp(p)] + segtree[pp(p) + 1];
34     return ans;
35 }
36 int main()
37 {
38     int n, k, i, j, t, maxi, count, ans;
39     n = int(sqrt(1.0 * N) + 0.5);
40     memset(p, 0, sizeof(p));
41     for (i = 1; i <= n; i++)
42     {
43         for (j = i + 1; i * j <= N; j++)
44             p[i * j] += 2;
45         p[i * i]++;
46     }
47     while (~scanf("%d%d", &n, &k))
48     {
49         build(1, n, 1);
50         for (i = 1; i <= n; i++)
51             scanf("%s%d", name[i], &pos[i]);
52         int &num = segtree[1];
53         count = 0;//记录出来的人数
54         maxi = 0;
55         while (num)
56         {
57             t = update(1, n, 1, k);//得到第k个人的实际位置
58             count++;
59             if (maxi < p[count])//找到更符合条件的替换
60             {
61                 maxi = p[count];
62                 ans = t;
63             }
64             if (num)
65             {
66                 i = 1;
67                 if (pos[t] > 0)
68                     i = 2;
69                 k = ((k - i + pos[t]) % num + num) % num + 1;
70             }
71         }
72         printf("%s %d
", name[ans], maxi);
73     }
74     return 0;
75 }
原文地址:https://www.cnblogs.com/jecyhw/p/3450915.html