【主席树维护mex】 【SG函数递推】 Problem H. Cups and Beans 2017.8.11

时间:2022-09-12 00:22:19

Problem H. Cups and Beans 2017.8.11

原题:

There are N cups numbered 0 through N − 1. For each i(1 ≤ i ≤ N − 1), the cup i contains Ai beans,
and this cup is labeled with an integer Ci.
Two people will play the following game:
• In each turn, the player chooses a bean from one of the cups except for the cup 0.
• If he chooses a bean from the cup i, he must move it to one of the cups i − Ci;…… ; i − 1.
• The players take turns alternately. If a player can’t choose a bean, he loses.
Who will win if both players play optimally?

Input:
Input Format:
N
C(1)   A(1)
C(2)   A(2)

C(N−1)   A(N−1)
Constraints:
• 2 ≤ N ≤ 105
• 1 ≤ Ci ≤ i
• 0 ≤ Ai ≤ 109
• At least one of Ai is nonzero.
• All values in the input are integers.

Output:
Print the name of the winner: \First” or \Second”.

Examples:

standard input standard output
3
1 0
1 1
Second
7
1 1
2 0
1 0
2 0
4 1
3 0
First
7
1 1
2 0
1 9
2 10
4 3
3 5
Second

Note:
Notes to the Sample 1:
• In the first turn, the first player must move a bean from 2 to 1.
• In the second turn, the second player must move a bean from 1 to 0.
• In the third turn, the first player can’t choose a bean and loses.

题目大意:

典型的博弈论,N个瓶子,第一个瓶子里没有石子,后面N-1个瓶子里分别有A[i]个石子,每个瓶子上贴了标签C[i],表示每次操作仅可以将这个瓶子里的一个石子石子最多可以移动到前面C[i]个瓶子里(也就是只可以将该瓶子里的一个石子放到 i-C[i] 到 i-1 号瓶子里),最后不能进行移动的人输,求谁赢。

解法分析:

可以将这个问题转化成限定条件的Nim取石子问题,题目的操作是将高位瓶子的石子移动到低位瓶子里,那么石子所在的瓶子标号其实就可以认为是石子堆的石子数,对于每个特定数量的石子堆 i,都有一个限定条件 C[i] 表示最多取i个,A[i] 则表示石子数为i的石子堆有A[i]个。

知道了题意,就可以直接暴力了,枚举前面的 i-C[i] 到 i – 1 的状态然后递推。可是数据范围是1e5,这样做无疑会超时,在周大爷的敏锐观察下,套用了一个主席树维护mex数的模板,就过了。

代码:

#include <bits/stdc++.h>

#define MAXN 100005
using namespace std;
typedef long long LL; #define lc(x) t[x].l
#define rc(x) t[x].r
const int N = 2e5 + , MX = 1e9 + ; struct node {
int l, r, mn;
} t[N * ];
int sz, root[N]; void ins(int &x, int l, int r, int p, int v) {//right of p is v
t[++sz] = t[x];
x = sz;
if (l == r) t[x].mn = v;
else {
int mid = (l + r) >> ;
if (p <= mid) ins(t[x].l, l, mid, p, v);
else ins(t[x].r, mid + , r, p, v);
t[x].mn = min(t[lc(x)].mn, t[rc(x)].mn);
}
} int query(int x, int l, int r, int v) {
if (l == r) return l;
else {
int mid = (l + r) >> ;
if (t[lc(x)].mn < v) return query(t[x].l, l, mid, v);
else return query(t[x].r, mid + , r, v);
}
} int SG[], n;
bool vis[];
int C[], A[]; int main() {
int i, j, k;
scanf("%d", &n);
for (i = ; i < n; i++) {
scanf("%d%d", &C[i + ], &A[i + ]);
}
SG[] = ;
ins(root[],,MX,SG[],);//ins是插入操作,root[1]表示当前树,0表示最小值,MX表示最大值,SG[0]是值,1是位置
for (i = ; i <= n; i++) {
int l = max(i - C[i], ), r = i - ;
SG[i] = query(root[r], , MX, l);
root[i] = root[i-];
ins(root[i],,MX,SG[i],i);
}
// for(int i = 1;i <= n;i++) printf("%d ",SG[i]);
int ans = ;
for (i = ; i <= n; i++) {
if (A[i] % == ) ans ^= SG[i];
}
if (ans) printf("First\n");
else printf("Second\n");
return ;
}

【主席树维护mex】 【SG函数递推】 Problem H. Cups and Beans 2017.8.11的更多相关文章

  1. 牛课练习赛34 Flittle w and Discretization 主席树维护Mex

    ittle w and Discretization 主席树维护Mex. 每个右端点 r 维护出一棵 在[1, r ] 区间中 其他所有的 值离这个 r 最近的的位置是多少. 然后询问区间[L,R]的 ...

  2. 【博弈论】【SG函数】【线段树】Petrozavodsk Summer Training Camp 2016 Day 9&colon; AtCoder Japanese Problems Selection&comma; Thursday&comma; September 1&comma; 2016 Problem H&period; Cups and Beans

    一开始有n个杯子,每个杯子里有一些豆子,两个人轮流操作,每次只能将一个豆子移动到其所在杯子之前的某个杯子里,不过可以移动到的范围只有一段区间.问你是否先手必胜. 一个杯子里的豆子全都等价的,因为sg函 ...

  3. 一类SG函数递推性质的深入分析——2018ACM陕西邀请赛H题

    题目描述 定义一种有根二叉树\(T(n)\)如下: (1)\(T(1)\)是一条长度为\(p\)的链: (2)\(T(2)\)是一条长度为\(q\)的链: (3)\(T(i)\)是一棵二叉树,它的左子 ...

  4. UVa 10561 &lpar;SG函数 递推&rpar; Treblecross

    如果已经有三个相邻的X,则先手已经输了. 如果有两个相邻的X或者两个X相隔一个.,那么先手一定胜. 除去上面两种情况,每个X周围两个格子不能再放X了,因为放完之后,对手下一轮再放一个就输了. 最后当“ ...

  5. Light OJ 1296 - Again Stone Game &lpar;博弈sg函数递推&rpar;

    F - Again Stone Game Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  6. CodeForces 464E The Classic Problem &vert; 呆克斯歘 主席树维护高精度

    题意描述 有一个\(n\)点\(m\)边的无向图,第\(i\)条边的边权是\(2^{a_i}\).求点\(s\)到点\(t\)的最短路长度(对\(10^9 + 7\)取模). 题解 思路很简单--用主 ...

  7. 【10&period;10校内测试】【线段树维护第k小&plus;删除】【lca&plus;主席树维护前驱后驱】

    贪心思想.将a排序后,对于每一个a,找到对应的删除m个后最小的b,每次更新答案即可. 如何删除才是合法并且最优的?首先,对于排了序的a,第$i$个那么之前就应该删除前$i-1$个a对应的b.剩下$m- ...

  8. 【洛谷5294】&lbrack;HNOI2019&rsqb; 序列(主席树维护单调栈&plus;二分)

    点此看题面 大致题意: 给你一个长度为\(n\)的序列\(A\),每次询问修改一个元素(只对当前询问有效),然后让你找到一个不下降序列\(B\),使得这两个序列相应位置之差的平方和最小,并输出这个最小 ...

  9. POJ&lowbar;3090 Visible Lattice Points 【欧拉函数 &plus; 递推】

    一.题目 A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), ...

随机推荐

  1. android 中theme和style的语法相关

    1.theme和style都是一组属性的集合,用于定义文本.颜色.大小等显示风格.他们都是资源,可以用android系统级别的一些默认的风格和主题资源,你也可以自定义你自己的主题和风格资源. 2.自定 ...

  2. CodeIgniter中驱动器的使用方法

    驱动器“Drivers”是CodeIgniter框架从2.0版本开始加入的新特性.正如中文版译者所言: 笔者看了这三篇英文参考,加上自己的一些理解,对官方文档关于驱动器的这一部分进行了一些补充. 1. ...

  3. 一些gem的简要翻译(欢迎提出问题共同讨论)

    写这篇文章主要有两方面用途 1.希望给rails同行一定的帮助,翻译水平有限,贴出中英文,翻译有误的地方欢迎指正,非常感谢,转载请标明出处,谢谢. 2.加深作者对gem的理解,有需要更详细了解安装以及 ...

  4. 执行mount命令时找不到介质或者mount&colon;no medium found的解决办法

    使用vmware时,在虚拟机设置里,设置CD/DVD为系统镜像,挂载时,有时会有找不到介质或者no medium found之类的提示. 根本原因是iso镜像并没有加载到虚拟机系统内. 解决办法: 首 ...

  5. 帧与场 - djf&lowbar;1985的专栏 - 博客频道 - CSDN&period;NET

    帧与场 - djf_1985的专栏 - 博客频道 - CSDN.NET 电视信号是通过摄像机对自然景物的扫描并经光电转换形成的.扫描方式分为“逐行扫描”和“隔行扫描”.“逐行扫描”指每幅图像均是由电子 ...

  6. Memento pattern

    21.5 再谈备忘录的封装 备忘录是一个很特殊的对象,只有原发器对它拥有控制的权力,负责人只负责管理,而其他类无法访问到备忘录,因此我们需要对备忘录进行封装. 为了实现对备忘录对象的封装,需要对备忘录 ...

  7. Best Cow Line---POJ 3617&lpar;贪心)

    FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year" competiti ...

  8. Accordion CodeForces - 1101B (实现)

    An accordion is a string (yes, in the real world accordions are musical instruments, but let's forge ...

  9. ArcGIS API for JavaScript

    以3.14版本为例: 1.部署环境: 下载:https://developers.arcgis.com/downloads/apis-and-sdks?product=javascript# 部署:h ...

  10. Linux 网络编程之 Select

    /*server*/ #include <stdio.h> #include <string.h> #include <unistd.h> #include &lt ...