POJ 2886 Who Gets the Most Candies?(线段树·约瑟夫环)
题意 n个人顺时针围成一圈玩约瑟夫游戏 每一个人手上有一个数val[i] 開始第k个人出队 若val[k] < 0 下一个出队的为在剩余的人中向右数 -val[k]个人 val[k] > 0 时向左数val[k]个 第m出队的人能够得到m的约数个数个糖果 问得到最多糖果的人是谁
约瑟夫环问题 n比較大 直接模拟会超时 通过线段树能够让每次出队在O(logN)时间内完毕 类似上一道插队的题 线段树维护相应区间还有多少个人没出队 那么当我们知道出队的人在剩余人中排第几个也就能够通过线段树知道他在原始环中排第几个了
至于第几个出队的人糖果最多就是求1...n中约数最多的数 能够利用反素数相关知识
对于不论什么正整数x 其约数的个数记做g(x) 比如g(1)=1 g(6)=4 假设某个正整数x满足 对于随意i(0<i<x) 都有g(i)<g(x) 则称x为反素数
我直接用的别人的反素数表
#include <cstdio>
#define lc p<<1, s, mid
#define rc p<<1|1, mid + 1, e
#define mid ((s+e)>>1)
using namespace std;
const int N = 5e5 + 5;
int tot[N * 4], val[N];
//tot维护相应区间还有多少人没出去
char name[N][20]; int ip[] = {1, 2, 4, 6, 12, 24, 36, 48, 60, 120, 180, 240, 360, 720,
840, 1260, 1680, 2520, 5040, 7560, 10080, 15120, 20160,
25200, 27720, 45360, 50400, 55440, 83160, 110880,
166320, 221760, 277200, 332640, 498960, 500001
}; //反素数 int div[] = {1, 2, 3, 4, 6, 8, 9, 10, 12, 16, 18, 20, 24, 30, 32, 36,
40, 48, 60, 64, 72, 80, 84, 90, 96, 100, 108, 120,
128, 144, 160, 168, 180, 192, 200
};//反素数相应的约数个数 void pushup(int p)
{
tot[p] = tot[p << 1] + tot[p << 1 | 1];
} void build(int p, int s, int e)
{
if(s == e)
{
tot[p] = 1;
return;
}
build(lc);
build(rc);
pushup(p);
} int update(int p, int s, int e, int x)
{
int ret;
if(s == e)
{
tot[p] = 0;
return s;
}
if(x <= tot[p << 1]) ret = update(lc, x);
else ret = update(rc, x - tot[p << 1]);
pushup(p);
return ret;
} int main()
{
int n, k, m, r, ans, pos;
while(scanf("%d%d", &n, &k) != EOF)
{
build(1, 1, n);
for(int i = 1; i <= n; ++i)
scanf("%s%d", name[i], &val[i]); for(int i = 0; ip[i] <= n; ++i)
{
m = ip[i]; //m为小于等于n的第一个反素数
ans = div[i]; //ans相应m的约数个数
} r = n; //还剩r个人
for(int i = 0; i < m; ++i)
{
r--;
pos = update(1, 1, n, k);
//pos为剩余序列中排第k的人在原始队列中的位置
if(!r) break;
if(val[pos] >= 0) //顺时针
k = (k - 1 - 1 + val[pos]) % r + 1;
//第一个-1是把1開始转换为0開始
//第二个是删除第k个后如今位于第k-1个 要前进val[pos]步
//后面的+1是把0開始换回1開始
else //逆时针
k = ((k - 1 + val[pos]) % r + r) % r + 1;
//逆时针删除第k个后如今还位于第k个 要后退-val[pos]步
}
printf("%s %d\n", name[pos], ans);
} return 0;
}
//Last modified : 2015-07-13 19:13
Description
N children are sitting in a circle to play a game.
The children are numbered from 1 to N in clockwise order. Each of them has a card with a non-zero integer on it in his/her hand. The game starts from the K-th child, who tells all the others the integer on his card and jumps out of the
circle. The integer on his card tells the next child to jump out. Let A denote the integer. If A is positive, the next child will be the A-th child to the left. If A is negative, the next child will be the (−A)-th
child to the right.
The game lasts until all children have jumped out of the circle. During the game, the p-th child jumping out will get F(p) candies where F(p) is the number of positive integers that perfectly divide p.
Who gets the most candies?
Input
(consisting of at most 10 letters) and the integers (non-zero with magnitudes within 108) on their cards in increasing order of the children’s numbers, a name and an integer separated by a single space in a line with no leading or trailing
spaces.
Output
Output one line for each test case containing the name of the luckiest child and the number of candies he/she gets. If ties occur, always choose the child who jumps out of the circle first.
Sample Input
4 2
Tom 2
Jack 4
Mary -1
Sam 1
Sample Output
Sam 3
POJ 2886 Who Gets the Most Candies?(线段树·约瑟夫环)的更多相关文章
- POJ 2886.Who Gets the Most Candies? -线段树(单点更新、类约瑟夫问题)
线段树可真有意思呢续集2... 区间成段的替换和增减,以及区间求和等,其中夹杂着一些神奇的操作,数据离散化,简单hash,区间异或,还需要带着脑子来写题. 有的题目对数据的操作并不是直接按照题面意思进 ...
- POJ 2886 Who Gets the Most Candies? 线段树。。还有方向感
这道题不仅仅是在考察线段树,还他妹的在考察一个人的方向感.... 和线段树有关的那几个函数写了一遍就对了,连改都没改,一直在转圈的问题的出错.... 题意:从第K个同学开始,若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 题意: 抢糖 ...
- POJ2886Who Gets the Most Candies?(线段树之约瑟夫)
约瑟夫问题的升级版,每次出去的是前一个出去的人位置+手上的数字(正往前,负往后).第i个出去的人拿的糖是i的约数的个数.求拿糖最多的人和他的糖果数. 这里用到了反素数的知识,在这直接打表 题目 AC代 ...
- 线段树(单点更新) 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?(线段树和反素数)
题目:http://poj.org/problem?id=2886 题意:N个孩子顺时针坐成一个圆圈且从1到N编号,每个孩子手中有一张标有非零整数的卡片. 第K个孩子先出圈,如果他手中卡片上的数字A大 ...
随机推荐
- json和XML
发请求(url) 1.client ---------------->服务端 发送数据(Json/xml) < - ...
- javascript进阶教程第一章案例实战
javascript进阶教程第一章案例实战 一.学习任务 通过几个案例练习回顾学过的知识 通过练习积累JS的使用技巧 二.实例 练习1:删除确认提示框 实例描述: 防止用户小心单击了“删除”按钮,在用 ...
- c++中重载、重写、覆盖的区别
Overload(重载):在C++程序中,可以将语义.功能相似的几个函数用同一个名字表示,但参数或返回值不同(包括类型.顺序不同),即函数重载.(1)相同的范围(在同一个类中):(2)函数名字相同:( ...
- css选择器和优先级总结
本文转自http://www.cnblogs.com/zxjwlh/p/6213239.html CSS三大特性—— 继承. 优先级和层叠. 继承:即子类元素继承父类的样式; 优先级:是指不同类别样式 ...
- 83.const与类
const常量对象,无法改变数据,只能引用尾部带const方法 类的成员如果是const,可以默认初始化,也可以构造的初始化,不可在构造函数内部初始化 类中的const成员,无法直接修改,可以间接修改 ...
- Sql Server 基础语法
来自:http://www.cnblogs.com/AaronYang/archive/2012/04/24/2468093.html Sql Server 基础语法 -- 查看数据表 select ...
- 实现CSS样式垂直水平完全居中
1.水平居中 a.内联元素(inline or inline-*)居中? 你可以让他相对父级块级元素居中对齐 .center-children { text-align: center; } b.块级 ...
- let---bash中用于计算的工具
- 洛谷 P1102 A-B数对
P1102 A-B数对 题目描述 出题是一件痛苦的事情! 题目看多了也有审美疲劳,于是我舍弃了大家所熟悉的A+B Problem,改用A-B了哈哈! 好吧,题目是这样的:给出一串数以及一个数字C,要求 ...
- Android使用BroadCastRecevier广播实现接收短信,并利用Toast弹出显示内容
在上一篇文章 Android简单实现BroadCastReceiver广播机制 中简单的实现了一个广播机制,这里利用BroadCarstRecevier实现一个接收短信并显示内容的案例,当然至于接收到 ...