G POJ 2886 Who Gets the Most Candies?
来源:互联网 发布:三菱l系列编程手册 编辑:程序博客网 时间:2024/06/04 18:08
题意为每个人手中有一张记录有一个非零整数的卡片,然后开始先将一个人踢出队列,然后按照这个人的数字来继续踢人,正数为左,负数为右
最后叫你求哪个人出队列时得到的candies最多,每个人得到的candies的数量等于这个人出队列时的次序的约数个数
这道的难点就是快速处理处下一个要被踢掉的人的位置
所以只要先处理处这人的相对位置,再用线段树查询该人的绝对位置
/*===============*\| *** *** *** *** || * ** * * * || *** *** * *** || ID: ZERO || LANG: C++ |\*===============*/#include <vector>#include <map>#include <set>#include <deque>#include <stack>#include <bitset>#include <algorithm>#include <sstream>#include <iostream>#include <cstdio>#include <queue>#include <cmath>#include <cstdlib>#include <cstring>#include <ctime>#include <string>#include <cassert>using namespace std;#define mod (int)(1e9+7)#define lson l , m , rt << 1#define rson m + 1 , r , rt << 1 | 1#define ls rt << 1#define rs rt << 1 | 1#define maxn 500001#define inf 1000000009int min(int x,int y){return x<y?x:y;}int sum[maxn<<2];void PushUP(int rt) { //向上更新 sum[rt]=sum[ls]+sum[rs];//求和}void build(int l,int r,int rt) {//建树 if (l == r) { sum[rt]=1; return ; } int m = (l + r) >> 1; build(lson); build(rson); PushUP(rt);}void update(int p,int l,int r,int rt) {//更新 if (l==r) { sum[rt]=0; return ; } int m=(l+r) >> 1;//middle if (p<=m) update(p , lson); else update(p , rson); PushUP(rt);}int query(int L,int R,int l,int r,int rt) {//询问 if (L <= l && r <= R) { return sum[rt]; } int m=(l+r) >> 1;//middle int ret = 0; if (L <= m) ret += query(L,R,lson); if (R > m) ret += query(L,R,rson); return ret;}int num[maxn];struct po{ char name[20]; int v;}a[maxn];void pre(){//处理出每个数的约数个数 int i,j,n=500000; for(i=1;i<=n;i++) num[i]=1; for(i=2;i<=n;i++){ for(j=i;j<=n;j+=i) num[j]++; }}int find(int s,int l,int r,int rt){//找出相对位置的绝对位置 if(l==r){ return l; } int m=(l+r)/2; if(sum[ls]>=s) return find(s,lson); return find(s-sum[ls],rson);}int main(){ int n,m,i; pre(); while(~scanf("%d%d",&n,&m)){ for(i=1;i<=n;i++){ scanf("%s%d",&a[i].name,&a[i].v); } int ma=0,tag; for(i=1;i<=n;i++){//直接处理出第几个出来的为最大 if(num[i]>ma) ma=num[i],tag=i; } int now=0; build(1,n,1); for(i=1;i<=tag;i++){//只循环到找到之前找出的最大 int ans,aa,tt,zero; if(i==1){ update(m,1,n,1);//删除第一个数 now=m; } else{ tt=a[now].v; if(tt<0){ tt=-tt; tt%=(n-i+1);//取余不解释。。 if(tt==0) tt=n-i+1; aa=query(1,now,1,n,1); if(aa<tt) { zero=aa+n-i+2-tt; } else zero= aa-tt+1; } else{ tt%=(n-i+1);//取余不解释。。 if(tt==0) tt=n-i+1; aa=query(now,n,1,n,1); if(aa<tt) zero=-aa+tt; else zero=n-i+1-aa+tt; } //zero为相对的位置 ans=find(zero,1,n,1);//找到这个数的绝对位置 update(ans,1,n,1);//踢出这个数 now=ans; } } printf("%s %d\n",a[now].name,ma); } return 0;}
- G 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?
- 转Android 安全攻防(三): SEAndroid Zygote执行用户态许可检查控制
- 在Windows下编译FFmpeg详细说明
- PL/SQL:学会使用DBMS_JOB包
- 【转】c++ web编程:写出你的CGI程序(英文)
- Android 开发源码分享
- G POJ 2886 Who Gets the Most Candies?
- Eclipse报错java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener解决方案
- html跳转的几种方法
- Java中的正则表达式的匹配和分组
- lcd驱动解析(一) .
- hdu 2045 LELE的EPG难题
- windows 下apache多站点支持 [PHP]
- LINQ to SQL查询和SQL命令
- CentOS之SSH安装与配置