POJ 2886 Who Gets the Most Candies? 线段树
题目: http://poj.org/problem?id=2886
左右转的果断晕,题目不难,关键是准确的转啊转。因为题目要求输出约数个数最多的数,所以预处理[1,500000]的约数的个数就行了。还有利用反素数的做法,太专业了,还是暴力预处理吧。。。
#include <stdio.h>
#include <string.h>
#include <math.h> const int MAXN = ; struct Tree_Node
{
int left, right, sum;
Tree_Node(){}
Tree_Node(int left, int right, int sum)
{
this->left = left;
this->right = right;
this->sum = sum;
}
}tree[MAXN<<]; void build(int left, int right, int step)
{
tree[step] = Tree_Node(left, right, right-left+);
if(tree[step].left == tree[step].right)
return;
int mid = left + (right-left) / ;
build(left, mid, step<<);
build(mid+, right, step<<|);
} int query(int num, int step)
{
tree[step].sum--;
if(tree[step].left == tree[step].right)
{
return tree[step].left;
}
if(num <= tree[step<<].sum)
return query(num, step<<);
else
return query(num - tree[step<<].sum, step<<|);
} int div[MAXN];
void cnt_divisible()
{
int x = sqrt(MAXN);
for(int i = ; i <= x; i++)
{
int y = MAXN / i;
for(int j = i+; j <= y; j++)
div[i*j] += ;
div[i*i]++;
}
} char name[MAXN][];
int val[MAXN];
int main()
{
cnt_divisible();
int n, k;
while(scanf("%d %d", &n, &k) != EOF)
{
build(, n, );
for(int i = ; i <= n; i++)
scanf("%s %d", name[i], &val[i]);
int ans = , num = , cnt = ;
while(n > )
{
n--;
int x = query(k, );
if(div[++cnt] > ans)
{
ans = div[cnt];
num = x;
}
if(n == )
break;
if(val[x] > )
k = (k + val[x] - ) % n + ;
else
k = ((k + val[x] - ) % n + n) % n + ;
}
printf("%s %d\n", name[num], ans);
}
return ;
}
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? 线段树。。还有方向感
这道题不仅仅是在考察线段树,还他妹的在考察一个人的方向感.... 和线段树有关的那几个函数写了一遍就对了,连改都没改,一直在转圈的问题的出错.... 题意:从第K个同学开始,若K的数字为正 则往右转, ...
- 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个离 ...
随机推荐
- MySQL 数据库常用命令 超级实用版分享
1.MySQL常用命令 create database name; 创建数据库 use databasename; 选择数据库 drop database name 直接删除数据库,不提醒 show ...
- PHP【第一篇】安装
一.准备 1.环境 系统平台:Red Hat Enterprise Linux Server release 7.3 (Maipo) 内核版本:3.10.0-514.el7.x86_64 2.下载安装 ...
- update 改写 merge into
update语句改写成merge into有时会提高运行速度 看两个案例 1.根据业务将两个嵌套子查询改写成max,速度有3min提升到3s UPDATE OPER_792.LL_SCB_YDKB_2 ...
- ASIC 前端功能验证等级与对应年薪划分[个人意见] (2011-07-04 15:33:35
下面的讨论转载自eetop,我选取了一些有意义的讨论,加了我的评注. 楼主zhhzhuawei认为 ===================================== 对于ASIC的前端功能验 ...
- HDU-1031(水题)
Design T-Shirt Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) P ...
- 在jsp中的css
div#one{}div#two{ width:auto; height:20px;background-color:#FAEBD7;text-align:right;}div#three{ widt ...
- Oracle SQL 基础学习
oracel sql 基础学习 CREATE TABLE USERINFO ( ID ,) PRIMARY KEY, USERNAME ), USERPWD ), EMAIL ), REDATE DA ...
- com.sun.org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException
在日志中, 查看导入的包是否是 import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory;
- linux下apache-tomcat的安装
一.JDK安装 1.安装JDK软件包 本例使用的JDK安装包为jdk-6u19-linux-x64.bin,该包是一个编译好的二进制可执行程序包,只需要执行即可安装. 首先进入存放JDK安装包的目录( ...
- OC加强-day03
#program mark - 0_18 分类的使用注意 [掌握] 1.分类的作用 作用:讲一个类分为多个模块,将相似功能的方法写在同一个模块中,方便我们后面代码的维护 "强调 1.分类中只 ...