HDU 2886 线段树单点修改
来源:互联网 发布:pic单片机中trisa 编辑:程序博客网 时间:2024/05/12 21:26
传送门:点击打开链接
Time Limit: 5000MS
Memory Limit: 131072KTotal Submissions: 10852 Accepted: 3371Case Time Limit: 2000MSDescription
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
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
Source
题意:有n个熊孩子站成一圈玩游戏,顺时针编号从1~n。他们每个人有一个数字(不为0),当第i个人出圈的时候,如果他的数为正数x,则下一个出圈的是从这个人开始(这个人不算)顺时针数的第xi个人。为负数则是逆时针。第i个出圈的熊孩子获得i的因子的个数个糖果。问获得糖果最多的最先出圈的是谁。
思路:用线段树记录区间内剩余的人数。譬如查找第x个人,如果左区间的人数小于等于x,就递归到左区间,否则就递归到右区间寻找第x-(左区间人数)个人。返回其最初的编号。用k记录实际是第几个人出圈,当这个人手里为正数的时候,他出圈了,那么后面人的时间编号都会减1,这里要十分地注意!
代码:
#include<cstdio>#include<cstring>int sum[2000005];char name[500005][12];int num[500005];int tmp[500005];int ans;void init(int n=500000){ memset(tmp,0,sizeof(tmp)); for(int i=1;i<=n;i++) for(int j=i;j<=n;j+=i) tmp[j]++;}void init2(int n){ ans=0; for(int i=1;i<=n;i++) if(ans<tmp[i]) ans=tmp[i];}void build(int id,int L,int R){ sum[id]=R-L+1; if(L==R) return; else { int mid=(L+R)>>1; build(id<<1,L,mid); build(id<<1|1,mid+1,R); }}int que(int id,int L,int R,int c){ if(L==R) { sum[id]--; return L; } else { sum[id]--; int mid=(L+R)>>1; if(c<=sum[id<<1]) return que(id<<1,L,mid,c); else return que(id<<1|1,mid+1,R,c-sum[id<<1]); }}int main(){ init(); int n,k; while(~scanf("%d %d",&n,&k)) { init2(n); for(int i=1;i<=n;i++) { scanf("%s %d",name[i],&num[i]); } build(1,1,n); int pos=k; for(int i=1;i<=n;i++) { pos=que(1,1,n,k); if(tmp[i]==ans) { printf("%s %d\n",name[pos],ans); break; } if(num[pos]>0) { k=(k-1+num[pos]-1)%(n-i)+1; } else { k=((k-1+num[pos])%(n-i)+(n-i))%(n-i)+1; } } } return 0;}
- HDU 2886 线段树单点修改
- hdu 1166 线段树(sum+单点修改)
- hdu 1754 线段树(Max+单点修改)
- 线段树(单点修改)
- HDU 1166(zkw线段树-单点修改)
- hdu 1754 线段树单点修改+区间查询
- hdu 4819 二维线段树,单点修改区间查询
- HDU - 1166 敌兵布阵(线段树,单点修改)
- HDU - 1754 I Hate It(线段树,单点修改)
- HDU 3308 线段树区间合并 + 单点修改
- HDU 1754 I hate it 线段树 单点修改、最大值
- 【模板】线段树单点修改
- hdu 1166 线段树 单点修改 + 询问区间求和 (线段树模板)
- 线段树 单点修改,区间修改
- Minimum Inversion Number 线段树+单点修改
- [CodeVS1080] 线段树练习(单点修改+求和)
- 【数据结构】【线段树】单点修改区间查询
- 线段树(单点修改,区间查询)
- flex 封装mx:list 成两列显示组件
- socket 获取外部地址
- vs2010转移ipch和sdf的位置
- linux中配置Java环境
- 知道仓储货架的构造和材料
- HDU 2886 线段树单点修改
- Excel Sheet Column Number
- Markdown语法
- GCC 编译错误 relocation truncated to fit: R_X86_64_32S against `.bss'
- acm311完全背包
- 听说北京有个兄弟连!
- maven nexus 在服务器端通过web界面添加上传第三方jar包
- 知道仓储管理四步如何做
- MYSQL数据库学习笔记