当前位置:网站首页>2018 China Collegiate Programming Contest - Guilin Site J. stone game
2018 China Collegiate Programming Contest - Guilin Site J. stone game
2022-04-23 01:58:00 【kai_wei_】
J. Stone Game
time limit per test
1 second
memory limit per test
256 megabytes
Alice and Bob are always playing game! The game today is about taking out stone from the stone piles in turn.
There are n piles of stones, and the i-th pile contains A[i] stones.
As the number of stones in each pile differs from its neighbor’s, they determine to take out exactly one stone from one of them in one turn without breaking that property. Alice goes first.
The player who cannot take stone will lose the game.
Now you have to determine the winner given n numbers representing the piles, if they both play optimally.
You should notice that even a pile of 0 stone is still regarded as a pile!
Input
The first line of input file contains an integer T ( 1 ≤ T ≤ 100 ) (1\le T\le 100) (1≤T≤100), describing the number of test cases.
Then there are 2 \times T lines, with every two lines representing a test case.
The first line of each test case contains only one integer n ( 1 ≤ n ≤ 1 0 5 ) (1\le n\le 10^5) (1≤n≤105) as described above.
The second line of that contains exactly n numbers, representing the number of stone(s) in a pile in order. All these numbers are ranging in [ 0 , 1 0 9 ] [0,10^9] [0,109].
It is guaranteed that the sum of n in all cases does not exceed 1 0 6 10^6 106.
Output
You should output exactly T lines.
For each test case, print Case d: (d represents the order of the test case) first, then print the name of winner, Alice or Bob.
Example
input
Copy
2
2
1 3
3
1 3 1
output
Copy
Case 1: Alice
Case 2: Bob
Note
Sample 1:
There is a possible stone-taking sequence: ( 1 , 3 ) → ( 1 , 2 ) → ( 0 , 2 ) → ( 0 , 1 ) (1,3)\rightarrow(1,2)\rightarrow(0,2)\rightarrow(0,1) (1,3)→(1,2)→(0,2)→(0,1)
Here is an invalid sequence: ( 1 , 3 ) → ( 1 , 2 ) → ( 1 , 1 ) → ( 0 , 1 ) (1,3)\rightarrow(1,2)\rightarrow(1,1)\rightarrow(0,1) (1,3)→(1,2)→(1,1)→(0,1). Though it has the same result as the first sequence, it breaks that property “the number of stones in each pile differs from its neighbor’s”.
博弈论+拓扑排序
首先要找规律,从两堆石子开始尝试,发现取奇数个石子的时候,Alice赢,取偶数个石子的,Bob赢。
比如3 1
最后的情况是1 0,取了3个石子,那么一定是Alice赢。
比如4 1
最后的情况还是1 0,取4个石子,那么一定是Bob赢。
事实上和石子的总数大小没什么关系,只要判断奇偶性即可。取完两个石堆之后,就尝试3个以上的。
比如1 3 1
最后情况是0 1 0,取4个石子,还是Bob赢
那么我们可以看出,只要确定了最终状态,然后再判断取石子的奇偶性,就能得出答案。
比如5 4 8 9 7 3 6
最后的状态就是 1 0 1 2 1 0 1
因此我们可以把原数组看成一个DAG,小的数指向大的数,然后通过拓扑排序,一次一次递增,得到最终序列.
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
typedef long long ll;
using namespace std;
#define N 100005
int t, cnt;
ll sum = 0;
int a[N], in[N], f[N];
vector<int> edge[N];
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
int n;
cin >> n;
while (n--) {
// scanf("%d", &t);
cin >> t;
for (int i = 1; i <= t; i++) {
// scanf("%d", &a[i]);
cin >> a[i];
sum += a[i];
}
a[0] = a[t + 1] = 1e9 + 1;
for (int i = 2; i <= t; i++) {
if (a[i] > a[i - 1]) {
// a[i-1]->a[i]
// cout << a[i] << ' ' << a[i - 1] << ' ';
edge[i - 1].push_back(i);
in[i]++;
}
if (a[i] < a[i - 1]) {
// a[i]->a[i-1]
// cout << a[i] << ' ' << a[i - 1] << ' ';
edge[i].push_back(i - 1);
in[i - 1]++;
}
}
//---------------------------------------
queue<int> Q;
for (int i = 1; i <= t; i++) {
if (in[i] == 0) {
Q.push(i);
}
}
while (!Q.empty()) {
int top = Q.front();
Q.pop();
for (int i = 0; i < edge[top].size(); i++) {
int now = edge[top][i];
f[now] = max(f[now], f[top] + 1);
if (--in[now] == 0) {
Q.push(now);
}
}
}
//-------------------------------
for (int i = 1; i <= t; i++) {
sum -= f[i];
}
cnt++;
if (sum % 2 == 1) {
// printf("Case %d: Alice\n", cnt);
cout << "Case " << cnt << ": "
<< "Alice" << '\n';
} else {
// printf("Case %d: Bob\n", cnt);
cout << "Case " << cnt << ": "
<< "Bob" << '\n';
}
for (int i = 1; i <= t; i++) {
edge[i].clear();
f[i] = 0;
in[i] = 0;
}
sum = 0;
}
}
版权声明
本文为[kai_wei_]所创,转载请带上原文链接,感谢
https://blog.csdn.net/kai_wei_/article/details/124332510
边栏推荐
- What problems will you encounter when dialing VPS?
- Some tips for using proxy IP.
- EBS:PO_EMPLOYEE_HIERARCHIES_ALL
- Problem solving: dpkg DEB: error: package name has characters that are't lowercase alphanums or '- +‘
- EBS:PO_ EMPLOYEE_ HIERARCHIES_ ALL
- What should I pay attention to when using proxy IP?
- Virtual serial port function of j-link V9 using skills
- [tutorial] how to use GCC "zero assembly" for white whoring MDK
- What is BGP server and what are its advantages?
- Quel est le fichier makefile?
猜你喜欢
有哪些常见的代理ip问题?
How to initialize "naming and surname" in C language
一些使用代理IP的小技巧。
关于C4D动画如何导入Lumion
2022 crane driver (limited to bridge crane) examination question bank and online simulation examination
Challenges often faced by client project management
The sixth season of 2022, the perfect children's model IPA national race leads the yuanuniverse track
浅析静态代理ip的三大作用。
W801/W800/W806唯一ID/CPUID/FLASHID
The leader / teacher asks to fill in the EXCEL form document. How to edit the word / Excel file on the mobile phone and fill in the Excel / word electronic document?
随机推荐
Sqlserver data transfer to MySQL
在使用代理IP前需要了解哪些分类?
postman里面使用 xdebug 断点调试
About how to import C4d animation into lumion
一些使用代理IP的小技巧。
Shardingsphere read write separation
PID refinement
What is a boolean type?
What code needs unit testing?
Do447 manage user and team access
Leetcode 112 Total path (2022.04.22)
FL studio20.8最新中文版本安装下载图文教程
The sixth season of 2022, the perfect children's model IPA national race leads the yuanuniverse track
代理IP可用率是不是等同于代理IP的效率?
easyswoole环境配置
Ziguang micro financial report is outstanding. What does the triple digit growth of net profit in 2021 depend on
Shardingsphere broadcast table and binding table
W801 / w800 WiFi socket development (I) - UDP
浅析静态代理ip的三大作用。
什么是bgp服务器,有哪些优势?