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个离 ...
随机推荐
- nginx-systemtap-toolkit
https://github.com/openresty/nginx-systemtap-toolkit
- 插件化技术在安卓sdk开发中实际应用
笔者从 2016 年初就因为公司业务需求转战 android sdk 开发, 应用插件化技术将公司 android sdk 重新翻版.先来说说需求. 由于笔者所在一家创业公司, android sdk ...
- iOS开发篇-申请开发者账号流程
1.注册一个苹果的apple id申请apple id的地址: https://appleid.apple.com/account 2.如申请公司账号,请使用以下链接免费获取邓白氏号码,以下的申请表格 ...
- install lua client for redis-server on Mac
1. lua client library for redis-server https://github.com/nrk/redis-lua 2. dependent luasocket https ...
- elasticsearch学习一、安装和配置
原文链接:http://jingyan.baidu.com/article/48206aead42b53216bd6b372.html ElasticSearch是一个基于Lucene的搜索服务器.它 ...
- Maven笔记(一)
1. 坐标 Maven坐标为各种构件引入了秩序,任何一个构件都有必须明确定义自己的坐标,而一组Maven坐标是通过一些元素定义的,它们是groupId, artifactId, version, pa ...
- MySQL(5.6) 函数
字符串函数 ASCII(str) 说明:返回字符串 str 最左边字符的 ASCII 值 mysql'); mysql); mysql> SELECT ASCII('a'); mysql> ...
- MyEclipse2014中项目名更改后如何使用新的项目名部署到Tomcat中去
在项目中调试的时候突然发现我复制的项目(项目名修改过了)部署在Tomcat中运行的时候还是显示的是原来的项目名,以至于我使用新的项目名称作为URL请求竟然是404,我去,当时感觉就不怎么好了. 当然, ...
- 小技巧之指定refer
在当前页面A的控制台输入window.location.href='要跳去的页面B',B页面的refer即为A页面.
- .net 安卓IOS跨平台des加解密双向的(可以互相加解密)
#region 跨平台加解密(c# 安卓 IOS) // public static string sKey = "12345678"; // /// // /// 解密 // / ...