POJ2886 Who Gets the Most Candies? 线段树+预处理
来源:互联网 发布:淘宝图片上传后变小 编辑:程序博客网 时间:2024/05/04 00:13
题目链接:http://poj.org/problem?id=2886
题意:n个孩子围成一圈,顺时针编号,每个孩子有一个名字和一个非零数字,指定从第k个孩子开始,每次使被抽到的孩子的号码a上对应的孩子退出去,如果数字是正的,那么就从这个孩子的左手边第a个孩子退出去,负数就从右边开始数。计算他们的推出序号p,求使得p的因子个数最大的孩子的名字,以及这个p的因子个数。
思路:也是人数题,单点更新,和之前的插队题差不多,维护线段树的方法是一样的,节点存储区间人数,如果p < sum[rt<<1]那么就更新左子树,否则更新右子树,并且修改p为p-sum[rt<<1]。另外可以预处理一下1到某一个区间内的反素数的值,也就是这个区间内的最大的因子个数的x是谁。打个表就行了,n的范围也不大,打出来的反素数表也就几十个而已。最后因为是一个环,计算相对位置要记得取模。
#include <iostream>#include <stdio.h>#include <algorithm>#include <cmath>#include <string>#include <cstring>using namespace std;const int maxn = 5e5+10;char name[maxn][15];int a[maxn];int sum[maxn << 2];const int antiprime[]={1,2,4,6,12,24,36,48,60,120,180,240,360,720,840, 1260,1680,2520,5040,7560,10080,15120,20160,25200, 27720,45360,50400,55440,83160,110880,166320,221760, 277200,332640,498960,554400,665280 };const int factorNum[]={1,2,3,4,6,8,9,10,12,16,18,20,24,30,32,36,40,48,60, 64,72,80,84,90,96,100,108,120,128,144,160,168,180, 192,200,216,224 };void PushUp(int rt){ sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];}void build(int l, int r, int rt){ if (l == r) { sum[rt]=1; return; } int m = (l + r) >> 1; build(l, m, rt << 1); build(m + 1, r, rt << 1 | 1); PushUp(rt);}int update(int p, int l, int r, int rt){ sum[rt]--; if (l == r) { return l; } int m = (l + r) >> 1; if (p <= sum[rt<<1]) return update(p, l, m, rt << 1); else return update(p-sum[rt<<1], m + 1, r, rt << 1 | 1);}int main(){ int n,k; 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 cnt=0; while(cnt<35&&antiprime[cnt]<=n) cnt++; cnt--; int pos=0; a[pos]=0; for(int i=0;i<antiprime[cnt];i++){ int mod=n-i; if(a[pos]>0) k=((k+a[pos]-2)%mod+mod)%mod+1; else k=((k+a[pos]-1)%mod+mod)%mod+1; pos=update(k,1,n,1); } printf("%s %d\n",name[pos],factorNum[cnt]); } return 0;}
阅读全文
0 0
- POJ2886 Who Gets the Most Candies? 线段树+预处理
- POJ2886:Who Gets the Most Candies?(线段树单点更新)
- poj2886-Who Gets the Most Candies?(线段树)
- 线段树:POJ2886 Who Gets the Most Candies?
- poj2886 Who Gets the Most Candies? 线段树
- poj2886--Who Gets the Most Candies?(线段树+反素数)
- 【POJ2886】Who Gets the Most Candies?-线段树+反素数
- 【poj2886】Who Gets the Most Candies?——线段树
- poj2886 Who Gets the Most Candies?反素数+线段树
- poj2886 Who Gets the Most Candies? 线段树加反素数
- POJ2886--Who Gets the Most Candies?
- poj2886 Who Gets the Most Candies?
- poj2886 Who Gets the Most Candies?
- POJ2886 Who Gets the Most Candies?
- poj2886 Who Gets the Most Candies?
- POJ2886-Who Gets the Most Candies?
- POJ2886 Who Gets the Most Candies? 【线段树】+【单点更新】+【模拟】+【反素数】
- poj2886 Who Gets the Most Candies?(反素数 + 线段树)
- 常用工具汇总
- Linux 基础操作(三)————vim进阶使用、输出及管理输出、管道、建立用户或用户组
- ubuntu 14.04 安装 samba
- Caused by: java.lang.ClassNotFoundException: org.hibernate.engine.transaction.spi.TransactionContext
- 利用双向循环链表实现任意长的整数进行加法运算
- POJ2886 Who Gets the Most Candies? 线段树+预处理
- xcode的多版本管理
- JetBrains全家桶(IDE)激活
- EditText的所有属性
- 在MAC上安装docker并部署oracle12.2
- WEB篇三 JS
- 在mac上安装postgres数据库
- Python字符串详解
- gcc,make,gdb命令用法