(中等) 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 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?
题意大致等同于约瑟夫环的问题,一次出来一个,然后出来x个人,其中x表示约数个数最多的数中最小的那个。
做这个题时还不知道什么是反素数,用最笨的方法 (用了3s+时间) 打出了表,直接复制上的。然后就是构建线段树了,这个的线段树不难构建,就是记录区间内还没出去的人的个数。然后更新的话是标记某个点为出去了,并且返回这个点的位置,作为下一次计数的起点。询问的话就是某个区间的没出去的个数。
反素数的话也在学习中,推荐ACdreamer的文章:http://blog.csdn.net/ACdreamers/article/details/25049767
代码如下:(注:写的比较乱,水平有限。)
#include<iostream>
#include<cstdio> using namespace std; const int remMax[][]={
{,},{,},{,},{,},{,},
{,},{,},{,},{,},
{,},{,},{,},{,},
{,},{,},{,},{,},
{,},{,},{,},{,},
{,},{,},{,},{,},
{,},{,},{,},{,},
{,},{,},{,},{,},
{,},{,},{,}}; int N,K;
char name[][];
int number[];
int BIT[*]; int findM(int x)
{
for(int i=;i<;++i)
if(remMax[i][]<=x&&remMax[i+][]>x)
return i;
} void pushUP(int po)
{
BIT[po]=BIT[po*]+BIT[po*+];
} void build_tree(int L,int R,int po)
{
BIT[po]=(R-L+); if(R==L) return; int M=(L+R)/;
build_tree(L,M,po*);
build_tree(M+,R,po*+);
} int query(int ql,int qr,int L,int R,int po)
{
if(ql>qr)
return ; if(ql<=L&&qr>=R)
return BIT[po]; int M=(L+R)/;
int temp=; if(ql<=M)
temp+=query(ql,qr,L,M,po*);
if(qr>M)
temp+=query(ql,qr,M+,R,po*+); return temp;
} int update(int un,int L,int R,int po)
{
--BIT[po]; if(L==R)
return L; int M=(L+R)/; if(BIT[po*]>=un)
return update(un,L,M,po*);
else
return update(un-BIT[po*],M+,R,po*+);
} int main()
{
int temp,temp1;
int n,las;
int times,fp; while(~scanf("%d %d",&N,&K))
{
for(int i=;i<=N;++i)
scanf("%s %d",name[i],&number[i]); build_tree(,N,); temp=findM(N);
times=remMax[temp][];
fp=remMax[temp][]; las=; for(int i=;i<=times;++i)
{
if(K>)
{
K%=(N-i+);
if(K==)
K=N-i+;
}
else
{
K%=(N-i+);
if(K==)
K=;
else
K=(N-i+)+K;
} temp1=query(las+,N,,N,); if(temp1>=K)
K=(N-i+)-(temp1-K);
else
K=(K-temp1); temp1=update(K,,N,); las=temp1;
K=number[las];
} printf("%s %d\n",name[las],fp);
} return ;
}
(中等) POJ 2886 Who Gets the Most Candies? , 反素数+线段树。的更多相关文章
- POJ 2886 Who Gets the Most Candies?(反素数+线段树)
		点我看题目 题意 :n个小盆友从1到n编号按顺时针编号,然后从第k个开始出圈,他出去之后如果他手里的牌是x,如果x是正数,那下一个出圈的左手第x个,如果x是负数,那出圈的是右手第-x个,游戏中第p个离 ... 
- 线段树(单点更新) POJ 2886 Who Gets the Most Candies?
		题目传送门 #include <cstdio> #include <cstring> #define lson l, m, rt << 1 #define rson ... 
- poj 2886 Who Gets the Most Candies?(线段树和反素数)
		题目:http://poj.org/problem?id=2886 题意:N个孩子顺时针坐成一个圆圈且从1到N编号,每个孩子手中有一张标有非零整数的卡片. 第K个孩子先出圈,如果他手中卡片上的数字A大 ... 
- POJ 2886 Who Gets the Most Candies?  线段树
		题目: http://poj.org/problem?id=2886 左右转的果断晕,题目不难,关键是准确的转啊转.因为题目要求输出约数个数最多的数,所以预处理[1,500000]的约数的个数就行了. ... 
- POJ 2886 Who Gets the Most Candies? (线段树)
		[题目链接] http://poj.org/problem?id=2886 [题目大意] 一些人站成一个圈,每个人手上都有一个数字, 指定从一个人开始淘汰,每次一个人淘汰时,将手心里写着的数字x展示 ... 
- 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 位置的 孩子,他的 数字是 +num 那么因为他自己本身是要被踢走的,所以相对位置 为k= k+num-1 如果数字是 -num,那么按正着数就没影响,k=k-num.线段树存储当前 ... 
- POJ 2886 Who Gets the Most Candies? 线段树。。还有方向感
		这道题不仅仅是在考察线段树,还他妹的在考察一个人的方向感.... 和线段树有关的那几个函数写了一遍就对了,连改都没改,一直在转圈的问题的出错.... 题意:从第K个同学开始,若K的数字为正 则往右转, ... 
随机推荐
- document.body与document.documentElement
			document.body 获取的是body,document.documentElement获取的是html,在任何浏览器上都是如此 相关问题: 1.获取页面滚动条滚动距离 chrome,safar ... 
- meta标签使360浏览器默认极速模式
			在head标签中添加一行代码: <html> <head> <meta name=”renderer” content=”webkit|ie-comp|ie-stand” ... 
- 项目总结SpringMVC相关
			流程文字概述1.用户发送请求至前端控制器DispatcherServlet2.DispatcherServlet收到请求调用HandlerMapping处理器映射器.3.处理器映射器找到具体的处理器, ... 
- IE6下绝对定位元素和浮动元素并列绝对定位元素消失
			<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ... 
- 函数FindFirstFile
			文件查找 Struct Lnode{ Char table[256]; //保存文件夹名 Struct Lnode*next; } 加文件夹入链表 Lnode *newList; Vo ... 
- hadoop中联结不同来源数据
			装载自http://www.cnblogs.com/dandingyy/archive/2013/03/01/2938462.html 有时可能需要对来自不同源的数据进行综合分析: 如下例子: 有Cu ... 
- MySql数据库基本介绍和基本语法
			一.数据库简单介绍 1. 按照数据库的发展时间顺序,主要出现了以下类型数据库系统: Ø 网状型数据库 Ø 层次型数据库 Ø 关系型数据库 Ø 面向对象数据库 上面4中数据库系统中,关系型数据库使用最为 ... 
- “玲珑杯”ACM比赛 Round #1 题解
			A:DESCRIPTION Eric has an array of integers a1,a2,...,ana1,a2,...,an. Every time, he can choose a co ... 
- cortexm内核 栈的8字节对齐及关键字PRESERVE8
			一.什么是栈对齐? 栈的字节对齐,实际是指栈顶指针须是某字节的整数倍.因此下边对系统栈与MSP,任务栈与PSP,栈对齐与SP对齐 这三对概念不做区分.另外下文提到编译器的时候,实际上是对编译器汇编器连 ... 
- C# 通过接口 post 请求
			/// <summary> /// 提交数据请求 方法一 /// </summary> /// <param name="POSTURL">请求 ... 
