POJ (线段树) Who Gets the Most Candies?
这道题综合性挺强的,又牵扯到数论,又有线段树。
线段树维护的信息就是区间中有多少个人没跳出去,然后计算出下一个人是剩下的人中第几个。
我在这调程序调了好久,就是那个模来模去的弄得我头晕。
不过题确实是好题,给赞。
#include <cstdio>
#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
using namespace std; const int maxn = + ;
int n, k;
int sum[maxn << ]; void build(int o, int L, int R)
{
if(L == R) { sum[o] = ; return; }
int M = (L + R) / ;
build(o*, L, M);
build(o*+, M+, R);
sum[o] = sum[o*] + sum[o*+];
} int update(int o, int L, int R, int p)
{
if(L == R) { sum[o] = ; return L; }
int M = (L + R) / ;
int ans;
if(sum[o*] >= p) ans = update(o*, L, M, p);
else ans = update(o*+, M+, R, p-sum[o*]);
sum[o] = sum[o*] + sum[o*+];
return ans;
} const int maxp = ;
int prime[maxp], pcnt = ;
bool vis[maxp]; void prime_table()
{
int m = sqrt(maxp);
for(int i = ; i <= m; i++) if(!vis[i])
for(int j = i * i; j < maxp; j += i) vis[j] = true;
for(int i = ; i < maxp; i++) if(!vis[i]) prime[pcnt++] = i;
} int F(int n)
{
int ans = ;
for(int i = ; i < pcnt && n > ; i++) if(n % prime[i] == )
{
int c = ;
while(n % prime[i] == ) { n /= prime[i]; c++; }
ans *= c + ;
}
if(n > ) ans <<= ;
return ans;
} int next[maxn];
char stu[maxn][]; int main()
{
//freopen("in.txt", "r", stdin); prime_table(); while(scanf("%d%d", &n, &k) == )
{
build(, , n - );
int _max = , id;
for(int i = ; i < n; i++) { scanf("%s%d", stu[i], &next[i]); } int pos = k - ;
for(int i = ; i <= n; i++)
{
int t = update(, , n - , pos + );
int f = F(i);
if(f > _max)
{
_max = f;
id = t;
}
if(i == n) break;
int MOD = n - i;
if(next[t] < ) next[t]++; //负数的情况还要有一点小改动,在这坑了好久
pos = (((pos + next[t] - ) % MOD) + MOD) % MOD;
}
printf("%s %d\n", stu[id], _max);
} return ;
}
代码君
POJ (线段树) Who Gets the Most Candies?的更多相关文章
- POJ 2886Who Gets the Most Candies?(线段树)
POJ 2886 题目大意是说有n个人围成一圈,游戏的起点是k,每个人持有一个数字(非编号)num,每次当前的人退出圈,下一个人是他左边的第num个(也就是说下一个退出的是k+num, k可以为负数, ...
- (中等) 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?
http://poj.org/problem?id=2886 一群孩子从编号1到n按顺时针的方向围成一个圆,每个孩子手中卡片上有一个数字,首先是编号为k的孩子出去,如果他手上的数字m是正数,那么从他左 ...
- POJ 2886.Who Gets the Most Candies? -线段树(单点更新、类约瑟夫问题)
线段树可真有意思呢续集2... 区间成段的替换和增减,以及区间求和等,其中夹杂着一些神奇的操作,数据离散化,简单hash,区间异或,还需要带着脑子来写题. 有的题目对数据的操作并不是直接按照题面意思进 ...
- POJ 2886 Who Gets the Most Candies? (线段树)
[题目链接] http://poj.org/problem?id=2886 [题目大意] 一些人站成一个圈,每个人手上都有一个数字, 指定从一个人开始淘汰,每次一个人淘汰时,将手心里写着的数字x展示 ...
- 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?(反素数+线段树)
点我看题目 题意 :n个小盆友从1到n编号按顺时针编号,然后从第k个开始出圈,他出去之后如果他手里的牌是x,如果x是正数,那下一个出圈的左手第x个,如果x是负数,那出圈的是右手第-x个,游戏中第p个离 ...
- POJ 2886 Who Gets the Most Candies? 线段树。。还有方向感
这道题不仅仅是在考察线段树,还他妹的在考察一个人的方向感.... 和线段树有关的那几个函数写了一遍就对了,连改都没改,一直在转圈的问题的出错.... 题意:从第K个同学开始,若K的数字为正 则往右转, ...
- Who Gets the Most Candies? POJ - 2886 (线段树)
按顺时针给出n个小孩,n个小孩每个人都有一个纸,然后每个人都有一个val,这个val等于自己的因子数,如果这个val是正的,那就顺时针的第val个孩子出去,如果是负的话,就逆时针的第val个孩子出去, ...
随机推荐
- EditorWindow edit ScriptableObject
using UnityEngine; [System.Serializable] public class Weapon { //[SerializeField] public string weap ...
- Xmarks丢失书签
想体验下Xmarks,不同浏览器同步书签,听说很好用,就安装Chrome插件,没想到竟然把我的所有书签都丢了. 不过在网上找到了回复的办法,也很简单: 原始地址:http://irising.me/2 ...
- SGU 102
For given integer N (1<=N<=104) find amount of positive numbers not greater than N that coprim ...
- nested pop animation can result in corrupted navigation bar
nested pop animation can result in corrupted navigation barFinishing up a navigation transition in a ...
- java基础知识回顾之javaIO类---BufferedInputStream和BufferedOutputStream
MP3的复制过程: package com.lp.ecjtu; import java.io.BufferedInputStream; import java.io.BufferedOutputStr ...
- Apache POI 解析 microsoft word 图片文字都不放过
http://blog.csdn.net/njchenyi/article/details/6894500 http://haohaoxuexi.iteye.com/blog/2031335
- <Win32_1>深入浅出windows消息机制[转自crocodile_]
上学期学习了Java ,感觉Java写一个窗口真心简单,很易上手,也就难怪很多开发人员选择Java作为自己的开发编程语言.但是由于自身对windows的热爱,让我觉得c.c++语言才是我亲睐的编程语言 ...
- jmeter线程组之间传递参数
JMeter 变量作用域局限于所属线程.这样设计是经过深思熟虑的,目的是让测试线程能够独立运转.有时候用户可能需要在不同线程间(可能属于同一个线程组,也可能不属于同一个线程组)传递变量. 其中一种方法 ...
- Useful for Android the development engineer from Github
Original:http://sysmagazine.com/posts/216591/ Many plowing on open space Github, I found assemblage ...
- converntion
One convention that we have is to use the names of fruits and vegetables for variables(only in small ...