Gym 100827G Number Game (博弈)
Number Game
Alice and Bob are playing a game on a line of N squares. The line is initially populated with one of each of the numbers from 1 to N. Alice and Bob take turns removing a single number from the line, subject to the restriction that a number may only be removed if it is not bordered by a higher number on either side. When the number is removed, the square that contained it is now empty. The winner is the player who removes the 1 from the line. Given an initial configuration, who will win, assuming Alice goes first and both of them play optimally?
Input
Input begins with a line with a single integer T, 1 ≤ T ≤ 100, denoting the number of test cases. Each test case begins with a line with a single integer N, 1 ≤ N ≤ 100, denoting the size of the line. Next is a line with the numbers from 1 to N, space separated, giving the numbers in line
order from left to right.
Output
For each test case, print the name of the winning player on a single line.
Sample Input
4 4
2 1 3 4
4
1 3 2 4
3
1 3 2
6
2 5 1 6 4 3
Sample Output
Bob
Alice
Bob
Alice
题意:输入一个N, 然后给一个1~N的数列。两个人轮流拿数字,当一个数字两边没有比它大的数字的时候(旁边是空格也可以),这个数字就可以被拿走。
题解:
考虑到如果要拿1,就要先把1旁边的两个数都拿走,那么如果1旁边两个数其中的一个已经被拿走,两个人肯定是不想拿剩下的一个数,所以此时所走的步数已经确定。Alice作为先手,优势就是选择两数之一。1在旁边特殊考虑一下,其实原理是一样的。
画个图表示下(略抽象):
附上几组数据:
8
7 3 5 2 4 1 6 8
6
1 5 3 4 2 6
7
7 2 4 6 3 1 5
6
1 5 3 4 2 6
5
3 4 1 5 2 Bob
Alice
Alice
Alice
Bob
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = ;
int a[N]; // 窝乃辣鸡
// 感谢小杭=.= bool MDZZ()
{
int n;
scanf("%d", &n);
int pone, pl, pr;
pl = ; pr = n+;
for (int i = ; i <= n; ++i) {
scanf("%d", a+i);
if (a[i] == ) pone = i;
}
for (int i = pone - ; i > ; --i) {
if (a[i] < a[i-]) { pl = i-; break; }
}
for (int i = pone + ; i < n; ++i) {
if (a[i] < a[i+]) { pr = i+; break; }
}
int ex = (n - pr + ) + (pl); int ls = pone - pl - ; // 左边中间的
int rs = pr - pone - ; // 右边中间的 if (n == ) return true; // 如果1在最左边或者最右边
if (pone == || pone == n) {
if ((ex+) % == ) return true;
}
// 1不在边上
else{
bool f = false;
// 能先拿1左边的数
if ((pl == ) || (ls > )) {
if ((ex+ls+) % == ) return true;
f = true;
}
// 能先拿1右边的数
if ((pr == n+) || (rs > )) {
if ((ex+rs+) % == ) return true;
f = true;
}
// 1左右的数都不能拿 必须把全部的拿走才行
if (!f && n % == ) return true;
}
return false;
} int main()
{
int t;
scanf("%d", &t);
while (t--) puts(MDZZ() ? "Alice" : "Bob");
return ;
}
Gym 100827G Number Game (博弈)的更多相关文章
- HDU 5623 KK's Number (博弈DP)
KK's Number 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/K Description Our lovely KK h ...
- UVA 1558 - Number Game(博弈dp)
UVA 1558 - Number Game 题目链接 题意:20之内的数字,每次能够选一个数字,然后它的倍数,还有其它已选数的倍数组合的数都不能再选,谁先不能选数谁就输了,问赢的方法 思路:利用dp ...
- Gym 100512F Funny Game (博弈+数论)
题意:给两个数 n,m,让你把它们分成 全是1,每次操作只能分成几份相等的,求哪一个分的次数最多. 析:很明显,每次都除以最小的约数是最优的. 代码如下: #pragma comment(linker ...
- Codeforces Round #651 (Div. 2) C. Number Game (博弈,数学)
题意:对于正整数\(n\),每次可以选择使它变为\(n-1\)或者\(n/t\) (\(n\ mod\ t=0\)且\(t\)为奇数),当\(n=1\)时便不可以再取,问先手赢还是后手赢. 题解:首先 ...
- 2017 ACM-ICPC EC-Final ShangHai 东亚洲大陆-上海
比赛链接:传送门 Gym 101775A Chat Group(签到:待补) Gym 101775B Scapegoat(待补) Gym 101775C Traffic Light(贪心+思维) 思路 ...
- 2016-2017 ACM-ICPC CHINA-Final
A Gym 101194A Number Theory Problem 7 的二进制是111,2k-1 的二进制是 k 个 1.所以 k 能被 3 整除时 2k-1 才能被 7 整除. #includ ...
- Gym 100952G&&2015 HIAST Collegiate Programming Contest G. The jar of divisors【简单博弈】
G. The jar of divisors time limit per test:2 seconds memory limit per test:64 megabytes input:standa ...
- Gym - 101350E Competitive Seagulls (博弈)
There are two seagulls playing a very peculiar game. First they line up N unit squares in a line, al ...
- Gym 101194A / UVALive 7897 - Number Theory Problem - [找规律水题][2016 EC-Final Problem A]
题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...
随机推荐
- 极客范:如何使用 Cloud Insight 来监控闭路电视?
最近新上线支持 Windows 系统及其组件 监控功能的 Cloud Insight,在系统监控领域基本囊括了对所有主流和部分非主流平台的支持.但是这还不够,Cloud Insight 可不仅仅是一个 ...
- js jquery学习
1.js api http://api.vfreesoft.com/ 2.26个jquery小技巧 http://www.cnblogs.com/shouce/p/5084565.html 3. ...
- Linux内核中的中断
http://blog.csdn.net/weiqing1981127/article/details/8298585 中断处理程序是被内核调用来响应中断的,它运行在中断上下文,中断处理程序是上半部, ...
- POJ2115——C Looooops(扩展欧几里德+求解模线性方程)
C Looooops DescriptionA Compiler Mystery: We are given a C-language style for loop of type for (vari ...
- js监听输入框值的即时变化onpropertychange、oninput
js监听输入框值的即时变化onpropertychange.oninput 很多情况下我们都会即时监听输入框值的变化,以便作出即时动作去引导浏览者增强网站的用户体验感. // // 要达到的效果 ...
- itoa函数的实现(不同进制)
2013-07-08 17:12:30 itoa函数相对于atoi函数,比较简单,还是要注意考虑的全面. 小结: 一下几点需要考虑: 对负数,要加上负号: 考虑不同进制,根据要求进行处理:对不同的进制 ...
- CenOS7.1安装VNC——让win7远程桌面linux
参考:http://wic.xingning.gov.cn/blog/29 https://linux.cn/article-5335-1.html 1.检查是否安装VNC, rpm -q tiger ...
- android SharedPreferences apply和commit的区别
1.apply没有返回值而commit返回boolean表明修改是否提交成功2.apply是将修改数据原子提交到内存, 而后异步真正提交到硬件磁盘, 而commit是同步的提交到硬件磁盘3.apply ...
- Linux内核与根文件系统的关系1
Linux内核与根文件系统的关系开篇题外话:对于Linux初学者来说,这是一个很纠结的问题,但这也是一个很关键的问题!一语破天机: “尽管内核是 Linux 的核心,但文件却是用户与操作系统交互所采用 ...
- 到底怎么样才叫看书?——Tony Zhao's
到底怎么样才叫看书?——上篇 目录: 一.引入 二.经历了就能理解 三.读书要分级 四.只读经典 五.别吝惜你动笔的那点时间 一.引入 看到这个题目的时候你可能会感到有点好笑:“这还用问,看书就是把书 ...