这道题不仅仅是在考察线段树,还他妹的在考察一个人的方向感。。。。

和线段树有关的那几个函数写了一遍就对了,连改都没改,一直在转圈的问题的出错。。。。

题意:从第K个同学开始,若K的数字为正 则往右转,否则往左转,转到的那同学出圈,知道剩下最后一个人。

输出得到蛋糕最多的人的名字和块数。

线段树的节点中存的是这一段内还有几个人没有跳出,思路很简单,详见注释。

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <algorithm> using namespace std; int ncd[500010]; struct N
{
int c;
char n[11];
}pla[500010]; struct T
{
int l,r,sur;
}st[2000010]; void init(int l,int r,int site)//初始化线段树
{
st[site].l = l;
st[site].r = r;
st[site].sur = r-l+1;//开始时没有人跳出
if(l == r)
return ;
int mid = (l+r)>>1; init(l,mid,site<<1);
init(mid+1,r,site<<1|1);
} int F,Max; int updata(int m,int site,int R)
{
if(st[site].l == st[site].r)//此时已经找到了具体的人
{
st[site].sur--;//此段中有一人跳出 if(Max < ncd[R]) //比较得到蛋糕书的多少
{
F = st[site].l;
Max = ncd[R];
}
return pla[st[site].l].c;//返回卡片上的值
} int ln = site<<1,rn = site<<1|1;
//如果前半的段的人数 >= m 说明要找的人就在前半段
//反之 就在后半段
st[site].sur--;//此段中有一人跳出 if(m <= st[ln].sur)
{
updata(m,ln,R);
}
else
{
updata(m-st[ln].sur,rn,R);
}
} int main()
{
int i,j,k,n = sqrt(500000),m = 500000;
int tempr;
int tempc; for(i = 1;i <= n; ++i)
{
for(j = i;j <= m; ++j)
{
k = i*j;
if(k <= 500000)
{
if(i == j)
ncd[k]++;
else
ncd[k] += 2;
}
else break;
}
} while(scanf("%d %d",&n,&k) != EOF)
{
for(i = 1;i <= n; ++i)
{
scanf("%*c%s%d",pla[i].n,&pla[i].c);
} init(1,n,1); tempr = k; //tempr为第i个要被删除的人在删除i-1个人之后的位置
//tempc为第i个的人的卡片上的值 F = k;
Max = 1;
for(i = 1;; ++i)
{
tempc = updata(tempr,1,i);
if(i == n)
break; m = n-i;//m为剩下的人数 //下面讨论下一个要跳出的是谁。。。。错了N次。。。。
if(tempc > 0)//往右转
{
tempc %= m;
if(tempc == 0)//取余为零说明 tempc 为 m 的 倍数
tempc = m;
if(tempc <= m-tempr+1)//m-tempr+1 为tempr之后的人数
{
tempr += tempc - 1;
}
else
{
tempr = tempc - m+tempr-1;
}
}
else//往左转
{
tempc = - tempc;
tempc %= m;
if(tempc == 0)
tempc = m;
if(tempc <= tempr-1)
{
tempr = tempr-tempc;
}
else
{
tempr += m-tempc;
}
}
}
printf("%s %d\n",pla[F].n,Max);
}
return 0;
}

POJ 2886 Who Gets the Most Candies? 线段树。。还有方向感的更多相关文章

  1. POJ 2886.Who Gets the Most Candies? -线段树(单点更新、类约瑟夫问题)

    线段树可真有意思呢续集2... 区间成段的替换和增减,以及区间求和等,其中夹杂着一些神奇的操作,数据离散化,简单hash,区间异或,还需要带着脑子来写题. 有的题目对数据的操作并不是直接按照题面意思进 ...

  2. POJ 2886 Who Gets the Most Candies?(线段树&#183;约瑟夫环)

    题意  n个人顺时针围成一圈玩约瑟夫游戏  每一个人手上有一个数val[i]   開始第k个人出队  若val[k] < 0 下一个出队的为在剩余的人中向右数 -val[k]个人   val[k ...

  3. POJ 2886 Who Gets the Most Candies? 线段树

    题目: http://poj.org/problem?id=2886 左右转的果断晕,题目不难,关键是准确的转啊转.因为题目要求输出约数个数最多的数,所以预处理[1,500000]的约数的个数就行了. ...

  4. poj 2886 "Who Gets The Most Candies?"(树状数组)

    传送门 参考资料: [1]:http://www.hankcs.com/program/algorithm/poj-2886-who-gets-the-most-candies.html 题意: 抢糖 ...

  5. 线段树(单点更新) POJ 2886 Who Gets the Most Candies?

    题目传送门 #include <cstdio> #include <cstring> #define lson l, m, rt << 1 #define rson ...

  6. POJ 2828 Buy Tickets(排队问题,线段树应用)

    POJ 2828 Buy Tickets(排队问题,线段树应用) ACM 题目地址:POJ 2828 Buy Tickets 题意:  排队买票时候插队.  给出一些数对,分别代表某个人的想要插入的位 ...

  7. POJ 2886 Who Gets the Most Candies? (线段树)

    [题目链接] http://poj.org/problem?id=2886 [题目大意] 一些人站成一个圈,每个人手上都有一个数字, 指定从一个人开始淘汰,每次一个人淘汰时,将手心里写着的数字x展示 ...

  8. (中等) POJ 2886 Who Gets the Most Candies? , 反素数+线段树。

    Description N children are sitting in a circle to play a game. The children are numbered from 1 to N ...

  9. POJ 2886 Who Gets the Most Candies?(反素数+线段树)

    点我看题目 题意 :n个小盆友从1到n编号按顺时针编号,然后从第k个开始出圈,他出去之后如果他手里的牌是x,如果x是正数,那下一个出圈的左手第x个,如果x是负数,那出圈的是右手第-x个,游戏中第p个离 ...

随机推荐

  1. shell 死循环

      例1: 运行脚本后会自动加载firefox浏览器,并打开指定网页.如果用户关闭firefox,脚本会再次自动重新打开firefox.     如需结束循环,中止t2.sh进程即可.注意不要同时执行 ...

  2. MegaCLI SAS RAID Management Tool

    MegaCLI SAS RAID Management Tool  Ver 8.04.08 July 05, 2012    (c)Copyright 2011, LSI Corporation, A ...

  3. -bash: ulimit: open files: cannot modify limit: Operation not permitted

    普通用户登录系统报错,提示: -bash: ulimit: open files: cannot modify limit: Operation not permitted. 处理方法: #vi /e ...

  4. 0x05: post 守护进程(deamon) json 任务调度

    python 签名 post #coding:utf-8 import urllib,urllib2 url='http://wtf.thinkphp.com/index.php?m=&c=t ...

  5. Linux 字符集

    摘抄自网络--/etc/sysconfig/i18n 文件:LANG="zh_CN.GB18030"SUPPORTED="zh_CN.GB18030:zh_CN:zh:e ...

  6. (转徐明吉)C#生成随机数

    private static char[] constant = { ', 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p ...

  7. 本篇文章主要是对jquery+ajax+C#实现无刷新操作数据库数据的简单实例进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助

    我们知道同步执行和异步执行的区别,为了更好的提高用户的体验,我们都会采用异步方式去处理一些问题,毕竟单线程的同步可能回造成卡死等现象,很不友好,所以可以使用ajax来完成用户的体验,现在我们就来说说如 ...

  8. sql 删除多项

    delete from 表名 where 字段 in(值,值,值.......)

  9. img图片元素下多余空白解决方案

    在进行页面的DIV+CSS排版时,遇到IE6(当然有时Firefox下也会偶遇)浏览器中的图片元素img下出现多余空白的问题绝对是常见的对於 该问题的解决方法也是「见机行事」,根据原因的不同要用不同的 ...

  10. ZOJ2099

    题意:给多个点,连成折线,求一个矩形可以包含这条折线. 输入: 多组测试数据 多个点的坐标 每组测试数据以0,0结束 以0,0结束程序 输出: 矩形左下角和右上角的坐标 思路:水题,注意输入那里有点坑 ...