POJ Who Gets the Most Candies? 2886(线段树)
来源:互联网 发布:淘宝买违禁品怎么查到 编辑:程序博客网 时间:2024/06/05 17:12
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
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
题意:(copy)N个人围成一圈第一个人跳出圈后会告诉你下一个谁跳出来跳出来的人(如果他手上拿的数为正数,从他左边数A个,反之,从他右边数A个) 跳出来的人所得到的糖果数量和他跳出的顺序有关 所得的糖果数为 (假设他是第k个跳出的) 则他得到的糖数为k能被多少个数正数 比如说 k = 6 ; 6 = 1*2*3*6 所以他得到的糖数为4;
分析:第一印象是约瑟夫环,可是n太大了,用线段树找出第几个出来的编号,首先要找出第几个出来最大
#include <stdio.h>#include <string.h>#include <algorithm>#include <stdlib.h>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define maxx 510000struct{ char name[15]; int h;}q[maxx];struct node{ int l,r,sum;//sum代表区间还剩多少人}tree[maxx<<2];int v[maxx],id,ma;void build(int l,int r,int rt){ tree[rt].sum=(r-l+1); tree[rt].l=l; tree[rt].r=r; if(l==r) return ; int m=(l+r)>>1; build(lson); build(rson);}int gengxin(int k,int rt){ tree[rt].sum--; //每删除一个就更新区间 if(tree[rt].l==tree[rt].r) return tree[rt].l; if(tree[rt<<1].sum>=k)//如果左孩子剩的人数大于k return gengxin(k,rt<<1); else return gengxin(k-tree[rt<<1].sum,rt<<1|1);}void zhao(int n)//找第几个最大{ int i,j; memset(v,0,sizeof(v)); for(i=1;i<=n;i++) { v[i]++; for(j=2*i;j<=n;j+=i) v[j]++; } ma=1; for(i=2;i<=n;i++) { if(v[i]>ma) { ma=v[i]; id=i; } }}int main(){ int n,m,i,j,k; while(~scanf("%d%d",&n,&k)) { zhao(n); for(i=1;i<=n;i++) { scanf("%s%d",q[i].name,&q[i].h); } build(1,n,1); m=id-1;//找前id个就行 int pos=gengxin(k,1);//先找出来第一个 int mod=tree[1].sum; while(m--) { if(q[pos].h>0)//k代表删除后这个人排第几 { k=(k-1+q[pos].h-1+mod)%mod+1;//k-1是防止k%mod==0的情况,而q[pos].h-1是 当这个本身删掉, } //他的下一个,就是第k个,只需要在这一个的基础上移动q[pos].h-1个 else { k=((k-1+q[pos].h)%mod+mod)%mod+1; } pos=gengxin(k,1); mod=tree[1].sum; } //printf("%d\n",pos); printf("%s %d\n",q[pos].name,ma); } return 0;}
- POJ 2886 Who Gets the Most Candies? 线段树
- poj 2886 Who Gets the Most Candies?(线段树#3)
- POJ 2886 Who Gets the Most Candies? 线段树
- POJ 2886 Who Gets the Most Candies? (线段树)
- POJ 2886 Who Gets the Most Candies?(线段树)
- POJ 2886 Who Gets the Most Candies?(线段树)
- POJ 2886 Who Gets the Most Candies? (线段树)
- Who Gets the Most Candies? - POJ 2886 线段树
- poj 2886 Who Gets the Most Candies?(线段树)
- POJ 2886 Who Gets the Most Candies?(线段树)
- POJ - 2886 Who Gets the Most Candies?(线段树)
- POJ - 2886 Who Gets the Most Candies?(线段树)
- POJ 2886 Who Gets the Most Candies?(线段树)
- poj 2886-Who Gets the Most Candies?(线段树)
- POJ Who Gets the Most Candies? 2886(线段树)
- poj 2886 Who Gets the Most Candies?(线段树)
- Who Gets the Most Candies?+POJ+线段树+反素数
- Who Gets the Most Candies? 线段树
- 清除浮动的几种方法
- JVM学习05——内存分配与回收
- 隶属度函数
- /bin,/sbin,/usr/bin,/usr/sbin区别
- css 选择器
- POJ Who Gets the Most Candies? 2886(线段树)
- ArcGis积累
- jquery的$().each,$.each的区别
- 机器学习 -- 模型集成与调优
- 安装ArcGIS提示localhost是无效的主机名解决方法
- 求二叉树中的第一条最长路径长度,并输出最长路径上的节点
- 【u034】追查坏奶牛
- DS单链表--类实现
- VLOOKUP函数语法解析