poj-2886 Who Gets the Most Candies?
2014-08-15 13:10
369 查看
http://poj.org/problem?id=2886
分析 : http://blog.csdn.net/sdj222555/article/details/6878651
借鉴了上面的思想,写的很好。
有几点要注意:
1.反素数和反素数的因子 我一直在这里弄混
2.相对位置和最初位置的转换,用到了线段树,比较难理解。
3. 因为A的值可能很大,所以会用到模运算,这就要求下标是从0开始,但是小孩的编号从1开始,所以在更新的时候,要考虑两者的转换,即从编号下标从1开始的,转换成编号下标从0开始的,再转换回去,这也是为什么在A小于零的时候,先是减1,最后再加1的原因。在大于零的时候,K再减1,使得移动的步数保持不变,不然移动的步数就要减去1。
分析 : http://blog.csdn.net/sdj222555/article/details/6878651
借鉴了上面的思想,写的很好。
有几点要注意:
1.反素数和反素数的因子 我一直在这里弄混
2.相对位置和最初位置的转换,用到了线段树,比较难理解。
3. 因为A的值可能很大,所以会用到模运算,这就要求下标是从0开始,但是小孩的编号从1开始,所以在更新的时候,要考虑两者的转换,即从编号下标从1开始的,转换成编号下标从0开始的,再转换回去,这也是为什么在A小于零的时候,先是减1,最后再加1的原因。在大于零的时候,K再减1,使得移动的步数保持不变,不然移动的步数就要减去1。
#include<cstdio> #define lson l , m ,rt << 1 #define rson m+1 , r , rt << 1 | 1 const int maxn = 500002; int sum[maxn << 2] ; struct child { char name[25]; int v; }p[maxn]; void build(int l,int r,int rt) { sum[rt] = r - l + 1 ; if(l==r) return; int m=(l+r) >> 1; build(lson); build(rson); } int update(int k, int l, int r, int rt) { sum[rt]--; //空位数减 1 if(l==r) return l; int m = (l+r) >> 1; if(sum[rt<<1] >= k) return update(k , lson); else return update(k-sum[rt<<1] , rson); //查询右子树 } 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}; const int factor[] = {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}; int main() { //freopen("a.txt","r",stdin); int n,k,i; while(~scanf("%d%d",&n,&k)) { build(1,n,1); for(i=1;i<=n;i++) { scanf("%s%d",p[i].name,&p[i].v); } int cnt=0; while(antiprime[cnt]<=n) cnt++; cnt--; int pos=0; p[pos].v=0; for(i=0; i<antiprime[cnt];i++) { if(p[pos].v>0) k=((k+p[pos].v-2)%sum[1]+sum[1])%sum[1]+1; else k=((k+p[pos].v-1)%sum[1]+sum[1])%sum[1]+1; pos=update(k,1,n,1); } printf("%s %d\n",p[pos].name,factor[cnt]); } return 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?
- POJ2886--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?(线段树#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?
- POJ 2886 Who Gets the Most Candies?
- poj 2886 Who Gets the Most Candies?