POJ 2886 Who Gets the Most Candies? 线段树。。还有方向感
这道题不仅仅是在考察线段树,还他妹的在考察一个人的方向感。。。。
和线段树有关的那几个函数写了一遍就对了,连改都没改,一直在转圈的问题的出错。。。。
题意:从第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? 线段树。。还有方向感的更多相关文章
- POJ 2886.Who Gets the Most Candies? -线段树(单点更新、类约瑟夫问题)
线段树可真有意思呢续集2... 区间成段的替换和增减,以及区间求和等,其中夹杂着一些神奇的操作,数据离散化,简单hash,区间异或,还需要带着脑子来写题. 有的题目对数据的操作并不是直接按照题面意思进 ...
- POJ 2886 Who Gets the Most Candies?(线段树·约瑟夫环)
题意 n个人顺时针围成一圈玩约瑟夫游戏 每一个人手上有一个数val[i] 開始第k个人出队 若val[k] < 0 下一个出队的为在剩余的人中向右数 -val[k]个人 val[k ...
- POJ 2886 Who Gets the Most Candies? 线段树
题目: http://poj.org/problem?id=2886 左右转的果断晕,题目不难,关键是准确的转啊转.因为题目要求输出约数个数最多的数,所以预处理[1,500000]的约数的个数就行了. ...
- poj 2886 "Who Gets The Most Candies?"(树状数组)
传送门 参考资料: [1]:http://www.hankcs.com/program/algorithm/poj-2886-who-gets-the-most-candies.html 题意: 抢糖 ...
- 线段树(单点更新) POJ 2886 Who Gets the Most Candies?
题目传送门 #include <cstdio> #include <cstring> #define lson l, m, rt << 1 #define rson ...
- POJ 2828 Buy Tickets(排队问题,线段树应用)
POJ 2828 Buy Tickets(排队问题,线段树应用) ACM 题目地址:POJ 2828 Buy Tickets 题意: 排队买票时候插队. 给出一些数对,分别代表某个人的想要插入的位 ...
- POJ 2886 Who Gets the Most Candies? (线段树)
[题目链接] http://poj.org/problem?id=2886 [题目大意] 一些人站成一个圈,每个人手上都有一个数字, 指定从一个人开始淘汰,每次一个人淘汰时,将手心里写着的数字x展示 ...
- (中等) 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 ...
- POJ 2886 Who Gets the Most Candies?(反素数+线段树)
点我看题目 题意 :n个小盆友从1到n编号按顺时针编号,然后从第k个开始出圈,他出去之后如果他手里的牌是x,如果x是正数,那下一个出圈的左手第x个,如果x是负数,那出圈的是右手第-x个,游戏中第p个离 ...
随机推荐
- uploadify上传大文件时出现404错误
出现这个错误的话一般是IIs限制了文件大小.IIS7下的默认设置限制了上传大小.这个时候Web.Config中的大小设置也就失效了.具体步骤:1.打开IIS管理器,找到Default Web Site ...
- MySQL(11):存储引擎
1.存储引擎是什么? MySQL中的数据用各种不同的技术存储在文件(或者内存)中.这些技术中的每一种技术都使用不同的存储机制.索引技巧.锁定水平并且最终提供广泛的不同的功能和能力.通过选择 ...
- CentOS 7 安装教程
参考资料: http://www.cnblogs.com/bobbylinux/articles/centos7.html
- 一个好用的Python备份mysql的脚本
前几天打算用Python写一个mysql脚本,上Google看了下老外写的,写的挺好的,原地址在http://tecadmin.net/python-script-for-mysql-database ...
- 属性动画 LayoutTransition AnimatorInflater Keyframe 新特性
LayoutTransition设置动画 使用LayoutTransition可为布局的容器设置动画,当容器中的视图层次发生变化时产生相应的过渡的动画效果 过渡的类型一共有四种: LayoutTran ...
- sublime开启php自动代码补全
配置文件添加 "auto_complete_selector": "source, text", 配置文件打开方式:preferences->settin ...
- libthrift0.9.0解析(一)之TServer
TServer 属性serverTransport 为TServerTransport类型,类图如下: 构造函数,简单根据args设置几个成员,大部分是工厂类: protected TServer(A ...
- oracle 语句
1.查询TRENDCHART_DLT表中的30条数据,统计字段FRONT01='0',BACK12='0'的条数 select sum(case when FRONT01='0' then 1 els ...
- mac brew 安装包下载失败解决
1.FQ或者用别的方式把安装包下载下来 2.查看缓存存储目录 brew --cache 3.将下载的包拷贝到缓存目录中,再此执行安装命令,如果安装还是去下载,检查下缓存目录是否多出一个下载中的文件,将 ...
- sql用户权限
登录 1)右键根目录属性 点下面的sql server 和 windows 身份验证模式 2)安全性右键新建,选择登陆 去掉 那个"用户下次登陆是必须改密码" ,下面默认数据库改为 ...