当前位置:网站首页>2018 China Collegiate Programming Contest - Guilin Site J. stone game
2018 China Collegiate Programming Contest - Guilin Site J. stone game
2022-04-23 02:00: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”.
Game theory + A topological sort
First of all, find the law , Start with two piles of stones and try , When I found taking an odd number of stones ,Alice win , Take an even number of stones ,Bob win .
such as 3 1
In the end 1 0, Take it 3 A stone , Then it must be Alice win .
such as 4 1
The final situation is 1 0, take 4 A stone , Then it must be Bob win .
In fact, it has nothing to do with the total number and size of stones , Just judge the parity . After taking two stone piles , Just try 3 More than one .
such as 1 3 1
The last thing is 0 1 0, take 4 A stone , still Bob win
So we can see , As long as the final state is determined , Then judge the parity of the stones , You can get the answer .
such as 5 4 8 9 7 3 6
The final state is 1 0 1 2 1 0 1
So we can think of the original array as a DAG, A small number points to a large number , Then sort by Topology , One increment at a time , Get the final sequence .
#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://yzsam.com/2022/04/202204230158380523.html
边栏推荐
- When should I write unit tests? What is TDD?
- keil mdk中文乱码,两种解决方法,字体不再难看
- Virtual serial port function of j-link V9 using skills
- Do447 manage user and team access
- 关于C4D动画如何导入Lumion
- C语言中如何“指名道姓”的进行初始化
- 力扣(LeetCode)112. 路径总和(2022.04.22)
- Sqlserver data transfer to MySQL
- K zeros after leetcode factorial function
- 一些使用代理IP的小技巧。
猜你喜欢

J-link v9 使用技巧之虚拟串口功能

Thinkphp内核开发盲盒商城源码v2.0 对接易支付/阿里云短信/七牛云存储

客户端项目管理经常面临的挑战

How to choose a good dial-up server?

Challenges often faced by client project management

FL studio20.8最新中文版本安装下载图文教程

Micro build low code zero foundation introductory course
![[experience tutorial] Alipay balance automatically transferred to the balance of treasure how to set off, cancel Alipay balance automatically transferred to balance treasure?](/img/d5/6aa14af59144b8c99aa6a367479f6b.png)
[experience tutorial] Alipay balance automatically transferred to the balance of treasure how to set off, cancel Alipay balance automatically transferred to balance treasure?

Encrypted compressed backup bat script

最长公共子序列(记录路径版)
随机推荐
力扣(LeetCode)112. 路径总和(2022.04.22)
2022第六季完美童模 IPA国民赛领跑元宇宙赛道
中金财富跟中金公司是一家公司吗,安全吗
Shardingsphere broadcast table and binding table
使用代理IP是需要注意什么?
Do447 manage user and team access
领导/老师让填写电子excel表格文档可手机上如何编辑word/excel文件填写excel/word电子文档?
C语言中如何“指名道姓”的进行初始化
How to install mysql-5.7.9 in RPM mode under Linux system
Communication summary between MCU and 4G module (EC20)
NPM yarn startup error [resolved]
What is a proxy IP pool and how to build it?
Leetcode 112 Total path (2022.04.22)
今天终于会写System.out.println()了
App optimization and advanced scoreboard Part 2 [Mui + flask + mongodb]
代理IP可用率是不是等同于代理IP的效率?
How to set computer IP?
Longest common subsequence (record path version)
K zeros after leetcode factorial function
2022 Saison 6 perfect Kid Model IPA national race Leading the Meta - Universe Track