博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj2886线段树(单点修改,区间查询)
阅读量:4617 次
发布时间:2019-06-09

本文共 3842 字,大约阅读时间需要 12 分钟。

Who Gets the Most Candies?
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 11955   Accepted: 3734
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 2Tom 2Jack 4Mary -1Sam 1

Sample Output

Sam 3 像是约瑟夫问题,用线段树表示区间中有多少可用
1 #include 
2 char name[500005][10]; 3 int ex[500005], sum[2000005]; 4 int prim[25] = {
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}; 5 void build(int o, int l, int r) { 6 if (l == r) { 7 sum[o] = 1; 8 return; 9 }10 int mid = l + r >> 1;11 build(o << 1, l, mid);12 build(o << 1 | 1, mid + 1, r);13 sum[o] = sum[o << 1] + sum[o << 1 | 1];14 }15 int update(int o, int l, int r, int pos) {16 int ans(0);17 if (l == r) {18 sum[o] -= 1;19 ans = l;20 return ans;21 }22 int mid = l + r >> 1;23 if (sum[o << 1] >= pos) ans = update(o << 1, l, mid, pos);24 else ans = update(o << 1 | 1, mid + 1, r, pos - sum[o << 1]);25 sum[o] = sum[o << 1] + sum[o << 1 | 1];26 return ans;27 }28 int query(int o, int l, int r, int ql, int qr) {29 int ans(0);30 if (ql <= l && r <= qr) {31 return sum[o];32 }33 int mid = l + r >> 1;34 if (ql <= mid) ans += query(o << 1, l, mid, ql, qr);35 if (qr > mid) ans += query(o << 1 | 1, mid + 1, r, ql, qr);36 return ans;37 }38 int solve(int x) {39 int sum = 1;40 for (int i = 0; i < 25; i++) {41 int ans = 0;42 while (x % prim[i] == 0) {43 ans++;44 x /= prim[i];45 }46 sum *= ans + 1;47 }48 return sum;49 }50 int main() {51 int n, k, ans, key;52 while (~scanf("%d%d", &n, &k)) {53 for (int i = 1; i <= n; i++) scanf("%s %d", name[i], &ex[i]);54 int m = n;55 build(1, 1, n);56 int pos = update(1, 1, n, k);57 ans = solve(1); key = 1;58 m--;59 for (int i = 2; i <= n; i++) {60 int id = ((query(1, 1, n, 1, (pos - 1 == 0 ? 1 : pos - 1)) + (ex[pos] < 0 ? ex[pos] + 1 : ex[pos])) % m + m) % m;61 if (!id) id = m;62 m--;63 pos = update(1, 1, n, id);64 int p = solve(i);65 if (p > ans) {66 key = pos;67 ans = p;68 }69 }70 printf("%s %d\n", name[key], ans);71 }72 return 0;73 }

 

 

转载于:https://www.cnblogs.com/y7070/p/4750540.html

你可能感兴趣的文章
python random
查看>>
esp32-智能语音-cli(调试交互命令)
查看>>
netty与MQ使用心得
查看>>
关于dl dt dd 文字过长换行在移动端显示对齐的探讨总结
查看>>
swoolefy PHP的异步、并行、高性能网络通信引擎内置了Http/WebSocket服务器端/客户端...
查看>>
Python学习笔记
查看>>
unshift()与shift()
查看>>
使用 NPOI 、aspose实现execl模板公式计算
查看>>
行为型模式:中介者模式
查看>>
How to Notify Command to evaluate in mvvmlight
查看>>
33. Search in Rotated Sorted Array
查看>>
461. Hamming Distance
查看>>
Python垃圾回收机制详解
查看>>
个人介绍
查看>>
mui搜索框 搜索点击事件
查看>>
最简单的三层实例【插入据
查看>>
设计模式学习笔记——Prototype原型模式
查看>>
pom.xml里有红叉报错的解决办法
查看>>
Perl last和next的用法区别
查看>>
Selenium 管理 Cookies
查看>>