POJ 2886 Who Gets the Most Candies?
来源:互联网 发布:当今网络世界强国 编辑:程序博客网 时间:2024/05/22 00:36
POJ 2886 Who Gets the Most Candies?
线段树基本应用
传送门:HustOJ
题意
约瑟夫问题。一群人标号1~N。给你k,第一次k号出来。每个人还都有一个数字,表示他出去以后下一个出去人的是谁。数字正表示向号大的方向数,负表示向号小的方向数。
第x个出去的人(x从1开始数)获得的糖果数是x的约数个数。问谁获得的糖果数最多。
思路
线段树维护约瑟夫环,像上一题一样。每个叶子节点设置为1,维护一个区间和。线段树作用在于,某个人gg后可以在logn时间维护新的排名。
关键还是在于change函数:
if(pos<=stree[rt<<1].sum) res=change(pos, lson);else res=change(pos-stree[rt<<1].sum, rson);
如果左儿子的剩余个数少于要求的名次,那么查询右儿子中第k减左儿子数
的名次。
每次一个人gg后,通过他的数字、他的标号来计算出下一个人的编号,这个编号是当前轮的编号。通过这个在线段树查询,查询这个人的实际编号。
代码
#include <cstdio>#include <cstdlib>#include <iostream>#include <algorithm>#include <string>#include <cstring>#include <vector>#include <cmath>#include <queue>#include <stack>#include <set>#include <map>#include <iomanip>#define _ ios_base::sync_with_stdio(0),cin.tie(0)#define M(a,b) memset(a,b,sizeof(a))#define N n#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;const int MAXN=500007;const int oo=0x3f3f3f3f;typedef long long LL;const LL loo=4223372036854775807ll;typedef long double LB;const LL mod=1e9+7;const double eps=1e-6;struct SegmentTree{ int sum;}stree[4*MAXN];void pushup(int rt){ stree[rt].sum=stree[rt<<1].sum+stree[rt<<1|1].sum;}void build(int l, int r, int rt){ if(l==r) { stree[rt].sum=1; return; } int m=(l+r)>>1; build(lson); build(rson); pushup(rt);}int change(int pos, int l, int r, int rt){ if(l==r) { stree[rt].sum=0; return l; } int m=(l+r)>>1; int res=0; if(pos<=stree[rt<<1].sum) res=change(pos, lson); else res=change(pos-stree[rt<<1].sum, rson); pushup(rt); return res;}char name[MAXN][15];int ne[MAXN];int zs[MAXN];int main(int argc, char *argv[]){ for(int i=1;i<sqrt(1.0*MAXN);i++) { for(int j=i+1;i*j<MAXN;j++) zs[i*j]+=2; zs[i*i]++; } int n, k; while(scanf("%d%d", &n, &k)==2) { for(int i=1;i<=N;i++) { scanf("%s %d", name[i], &(ne[i])); } build(1, n, 1); int m=n; int maxp=0,maxc=0; int ou=1; while(m) { int tmp=change(k, 1, n, 1);m--; if(maxc<zs[ou]) { maxp=tmp; maxc=zs[ou]; } ou++; if(m==0) break; if(ne[tmp]>0) { k=(k-2+ne[tmp])%m+1; } else { k=((k-1+ne[tmp])%m+m)%m+1; } } cout<<name[maxp]<<' '<<maxc<<endl; } return 0;}
阅读全文
0 0
- 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 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 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?
- 函数
- css的margin、padding和布局定位
- 关键字static
- HashMap 不会重复的Arrays
- Dubbo 简 单 应 用
- POJ 2886 Who Gets the Most Candies?
- 使用Nodejs进行反向代理
- 双钥密钥密码算法RSA解析与短密钥实现
- LCA之ST算法模板
- POJ-2965(DFS路径输出)
- poi中getPhysicalNumberOfRows()和getLastRowNum()区别
- stm32中中断源名和中断处理函数名所在位置
- Linux IO模式及 select、poll、epoll详解
- hibernateUtil工具类的封装