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

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

题意:从第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. php中运用GD库实现简单验证码

    昨天学习了运用php的GD库进行验证码的实现. 首先可以用phpinfo()函数看一下GD库有没有安装,我用的wampserver是自动给安装的. 主要的步骤是: 1.生成验证码图片 2.随机生成字符 ...

  2. 27个Jupyter Notebook使用技巧及快捷键(翻译版)

    Jupyter Notebook Jupyter Notebook 以前被称为IPython notebook.Jupyter Notebook是一款能集各种分析包括代码.图片.注释.公式及自己画的图 ...

  3. NYOJ-104最大和

    我看了好多博客,都是拿一维的做基础,一维的比较简单,所以要把二维的化成一维的,一维的题目大意:给了一个序列,求那个子序列的和最大,这时候就可以用dp来做,首先dp[i]表示第i个数能构成的最大子序列和 ...

  4. Entity Framework 重写OnModelCreating,控制生成表名的单复数

    重写OnModelCreating,控制生成表名的单复数 public class MYDbContext : DbContext { public DbSet<User> Users { ...

  5. bootstrap初探

    bootstrap资源 http://getbootstrap.com http://github.com/twbs http://www.bootcss.com bootstrap栅格系统 容器:流 ...

  6. python 下的数据结构与算法---8:哈希一下【dict与set的实现】

    少年,不知道你好记不记得第三篇文章讲python内建数据结构的方法及其时间复杂度时里面关于dict与set的时间复杂度[为何访问元素为O(1)]原理我说后面讲吗?其实就是这篇文章讲啦. 目录: 一:H ...

  7. Hyper-V虚拟机和主机的网络配置

    Hyper-V虚拟机和主机的网络配置 方式1.共享式 这种方式是将Hyper-V内部的虚拟网络与外部网络共享.使得内部是一个私有的网络.属于NAT的类型.(不知道这么说对不对) 优点: 相对来说属于私 ...

  8. 真机测试,Xcode报错:process launch failed: Security

    解决办法:手机->通用->设备管理->信任开发商应用即可

  9. php学习之路

    1.php拼接字符串+查询 $floor_id = M('house_floor_input')->where($map1)->field('id')->select(); $flo ...

  10. 对于没有Command属性时,怎么来达到相同的效果

    控件是第三方Telerik控件  CellEditEnded事件想写成Command{Binding CellEditEndedCommand}  这样的效果如下代码 需要引用 System.Wind ...