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个离 ...
随机推荐
- MongoDB的地埋空间数据存储、空间索引以及空间查询
一.关于MongoDB 在众多NoSQL数据库,MongoDB是一个优秀的产品.其官方介绍如下: MongoDB (from "humongous") is a scalable, ...
- debugfs恢复文件
echo "this is test" >xx debugfs: ls -d /root/test1 () . () .. () xx <> () test.c ...
- 精灵类(CCSprite)
一.对精灵的理解 玩家控制的主角.AI控制的NPC,以及地图上的宝箱.石块,甚至游戏主菜单的背景图片都是精灵.因此,可以这样认为,玩家看到的一切几乎都是由精灵构成的. 精灵不一定是静态的.通常,一个精 ...
- Android(java)学习笔记141:SQLiteDatabase的query方法参数分析
public Cursor query (boolean distinct, String table, String[] columns, String selection, String[] se ...
- 集合练习——Map部分
练习: 输入诗的名称查询出诗的内容,当输入exit时,退出程序,“春晓”,“静夜思”,“鹅”. package CollectionPart; public class Poetry { privat ...
- FWFT FIFO读操作注意
FWFT:First Word Fall Through的缩写,好像是Xilinx的说法,Altera对应的概念是Show-ahead synchronous(SASO).即数据在rdreq有效之前就 ...
- Android开发——构建自定义组件
Android中,你的应用程序程序与View类组件有着一种固定的联系,例如按钮(Button). 文本框(TextView), 可编辑文本框(EditText), 列表框(ListView), 复选框 ...
- Fetch的使用
import React,{ Component } from 'react'; import { AppRegistry, ListView, Image, Text, StyleSheet, Vi ...
- css position 定位
fixed 属于绝对定位,相对于浏览器窗口定位 (IE 6不支持) relative 相对定位,通过设置垂直或水平位置,让这个元素"相对于"它的原始起点进行移动. ...
- .NET Entity Framework入门简介及简单操作
Entity Framework是微软借鉴ORM思想开发自己的一个ORM框架. ORM就是将数据库表与实体对象(相当于三层中的Model类)相互映射的一种思想. 最大的优点就是非常方便的跨数据库平台. ...