POJ-2886 Who Gets the Most Candies?---线段树+约瑟夫环
题目链接:
https://cn.vjudge.net/problem/POJ-2886
题目大意:
N个人围成一圈第一个人跳出圈后会告诉你下一个谁跳出来跳出来的人(如果他手上拿的数为正数,从他左边数A个,反之,从他右边数A个) 跳出来的人所得到的糖果数量和他跳出的顺序有关 所得的糖果数为 (假设他是第k个跳出的) 则他得到的糖数为k能被多少个数整除
解题思路:
首先将因子个数打表:因子个数打表模板
然后先求出1-n中因子个数最大的为x,约瑟夫环进行x次
用线段树求出每次约瑟夫环后的具体下标。因为随性和约瑟夫环的进行,人在一个一个减少,需要求出约瑟夫环中的第i个在原数组中的下标
#include<iostream>
#include<cstdio>
#define MID(l, r) (l + (r - l) / 2)
#define lson(o) (o * 2)
#define rson(o) (o * 2 + 1)
using namespace std;
typedef long long ll;
const int INF = 1e9 +;
const int maxn = 2e6 + ;
int h, w, n;
struct node
{
int l, r, sum;
}tree[maxn];
int ans[maxn];
void build(int o, int l, int r)
{
tree[o].l = l, tree[o].r = r;
if(l == r)
{
tree[o].sum = ;
return;
}
int m = MID(l, r);
int lc = lson(o), rc = rson(o);
build(lc, l, m);
build(rc, m + , r);
tree[o].sum = tree[lc].sum + tree[rc].sum;
}
int id;
void query(int o, int a)//查询第a个数下标,同时删除这个数
{
if(tree[o].l == tree[o].r)
{
id = tree[o].l;
tree[o].sum = ;
return;
}
int lc = lson(o), rc = rson(o);
if(a <= tree[lc].sum)query(lc, a);
else query(rc, a - tree[lc].sum);
tree[o].sum = tree[lc].sum + tree[rc].sum;
}
int divisor_num[maxn];
void init(int n)
{
for(int i = ; i <= n; i++)
{
divisor_num[i]++;
for(int j = i * ; j <= n; j += i)
divisor_num[j]++;
}
}
int solve(int n)
{
int max = , maxid;
for(int i = ; i <= n; i++)
if(divisor_num[i] > max)
{
max = divisor_num[i];
maxid = i;
}
return maxid;
}
char name[][];
int a[maxn];
int main()
{
int n, m;
init();
while(scanf("%d%d", &n, &m) != EOF)
{
for(int i = ; i <= n; i++)
scanf("%s%d", name[i], &a[i]);
build(, , n);
int ansid = solve(n), mod = n;
int t = ansid;
while()
{
query(, m);
if(--t == )break;
mod--;
if(a[id] > )
{
m = m - + a[id];
m = ((m - ) % mod + mod) % mod + ;
}
else
{
m = m + a[id];
m = ((m - ) % mod + mod) % mod + ;
}
}
printf("%s %d\n", name[id], divisor_num[ansid]);
}
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? 线段树
题目: 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 题意: 抢糖 ...
- 线段树(单点更新) 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 ...
随机推荐
- AIDL(2):服务端回调客户端
1.大致流程 在服务端声明远程服务接口IRemoteService.aidl,并声明回调接口ICallback.aidl 在服务端实现远程服务接口IRemoteService.Stub 使用Remot ...
- css盒子居中
方法1(margin: 0 auto)<!DOCTYPE html> <html lang="en"> <head> <meta char ...
- TOJ 1721 Partial Sums
Description Given a series of n numbers a1, a2, ..., an, the partial sum of the numbers is defined a ...
- unity规范
Unity VS脚本自动添加头部注释 http://blog.csdn.net/yupu56/article/details/52326930 Unity3D C#代码注释规范及文档生成 http:/ ...
- 数据挖掘:基于Spark+HanLP实现影视评论关键词抽取(1)
1. 背景 近日项目要求基于爬取的影视评论信息,抽取影视的关键字信息.考虑到影视评论数据量较大,因此采用Spark处理框架.关键词提取的处理主要包含分词+算法抽取两部分.目前分词工具包较为主流的,包括 ...
- hdu 5242——Game——————【树链剖分思想】
Game Time Limit:1500MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- git本地创建新分支并推送到远程仓库
1,在当前项目目录,从已有的分支创建新的分支(如从master分支),创建一个dev分支 git checkout -b dev 2,创建完可以查看一下,分支已经切换到dev git branch * ...
- AtCoder Grand Contest 023 E - Inversions
Description 给出长度为 \(n\) 序列 \(A_i\),求出所有长度为 \(n\) 的排列 \(P\),满足 \(P_i<=A_i\),求所有满足条件的 \(P\) 的逆序对数之和 ...
- C# .net页面乱码
可在web.config中设置 <globalization requestEncoding="utf-8" responseEncoding="utf-8&quo ...
- 03.枚举和string以及int类型之间的转换
练习1: 将枚举类型强转成int类型 namespace _04.枚举类型的练习01 { //声明一个QQState类型的枚举 public enum QQState { OnLine, OffL ...