POJ - 2886 Who Gets the Most Candies?(线段树)
来源:互联网 发布:开源网络管理软件 编辑:程序博客网 时间:2024/06/05 23:01
既然是做线段树专题,这个题思路很容易有,用线段树维护一个区间和,表示这个区间内还有多少个人,更新是单点增减。
自己做的时候不知道反素数的概念所以就打个表先预处理一下。
参考题解
if(p[pos].v > 0) k = ((k + p[pos].v - 2) % tree[1].cnt + tree[1].cnt) % tree[1].cnt + 1;else k = ((k + p[pos].v - 1) % tree[1].cnt + tree[1].cnt) % tree[1].cnt + 1;
从这篇题解里,利用取余运算,避免了各种讨论,我应该好好学习。
#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>using namespace std;#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1const int maxn=500000+100;int sum[maxn<<2], cnt[maxn], A[maxn], id[maxn];char name[maxn][12];void init(){ for(int i=1; i<maxn; i++) for(int j=i; j<maxn; j+=i) ++cnt[j]; for(int i=1; i<maxn; i++) id[i] = cnt[id[i-1]] >= cnt[i] ? id[i-1] : i;}void pushup(int rt){ sum[rt]=sum[rt<<1]+sum[rt<<1|1];}void build(int l, int r, int rt){ sum[rt]=1; if(l==r)return ; int mid=(l+r)>>1; build(lson); build(rson); pushup(rt);}int update(int p, int l, int r, int rt){ sum[rt]--; if(l==r)return l; int mid=(l+r)>>1; if(sum[rt<<1] >= p)return update(p, lson); else return update(p-sum[rt<<1], rson);}int main(){ init(); int n, k, op; while(~scanf("%d%d", &n, &k)) { for(int i=1; i<=n; i++) scanf("%s%d", name[i], &A[i]); build(1, n, 1); int pos=0; A[0]=0; for(int i=0; i<id[n]; i++) { if(A[pos]>0) k=((k+A[pos]-2)%sum[1]+sum[1])%sum[1]+1; else k=((k+A[pos]-1)%sum[1]+sum[1])%sum[1]+1; pos=update(k, 1, n, 1); } printf("%s %d\n", name[pos], cnt[id[n]]); } return 0;}
0 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? 线段树
- 自定义UITableViewCell (通过代码实现:每个CELL格式不一样)
- poj-1704 nim变形
- iOS7——UIControlEventTouchDown延迟响应问题
- Thetest
- 安装svn客户端和vs2012的svn插件之后,在项目右键菜单里没有“更新项目至最新版本”和“提交项目变更”选项
- POJ - 2886 Who Gets the Most Candies?(线段树)
- SVN安装配置使用
- Java Secret: Using an enum to build a State machine(Java秘术:用枚举构建一个状态机)
- 练手小项目(2)-生活小助手--星座运势查询
- AmqpTemplate-发送-接收-消息
- mac升级系统之后,eclipse找不到JDK,java工程报错
- 第十二周项目二(1)求N组数的最大公约数
- ScrollView分析
- 《Java并发编程实战》---线程安全性---对象的共享