C. Swap Game
C. Swap Game
Alice and Bob are playing a game on an array of positive integers. Alice and Bob make alternating moves with Alice going first.
In his/her turn, the player makes the following move:
- If , the player loses the game, otherwise:
- Player chooses some with . Then player decreases the value of by and swaps with .
Determine the winner of the game if both players play optimally.
Input
The input consists of multiple test cases. The first line contains a single integer — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer — the length of the array .
The second line of each test case contains integers — the elements of the array .
It is guaranteed that sum of over all test cases does not exceed .
Output
For each test case, if Alice will win the game, output "Alice". Otherwise, output "Bob".
You can output each letter in any case. For example, "alIcE", "Alice", "alice" will all be considered identical.
Example
input
3 2 1 1 2 2 1 3 5 4 4
output
Bob
Alice
Alice
Note
In the first testcase, in her turn, Alice can only choose , making the array equal . Then Bob, in his turn, will also choose and make the array equal . As , Alice loses.
In the second testcase, once again, players can only choose . Then the array will change as follows: , and Bob loses.
In the third testcase, we can show that Alice has a winning strategy.
解题思路
数组首个元素最先减到的就会输,因此可以考虑一开始的和这两种情况。
如果是,那么Alice可以每次把最小的元素换到第一个位置,然后轮到Bob时就会对这个最小的元素减(这个元素还是最小元素)。而Bob只能将大于最小值(也就是大于)的元素换到第一个位置。可以发现,这样子Alice一定可以将最小元素减到,使得Bob输掉比赛。
如果是,那么一开始Ailce对减去后,就变成最小元素被换到数组的后面,这意味着下一次轮到Bob时,Bob是属于这种情况的,意味着Bob会赢得比赛而Alice会输掉比赛。
AC代码如下:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int N = 2e5 + 10; 5 6 int a[N]; 7 8 void solve() { 9 int n; 10 scanf("%d", &n); 11 for (int i = 0; i < n; i++) { 12 scanf("%d", a + i); 13 } 14 printf("%s\n", a[0] == *min_element(a, a + n) ? "Bob" : "Alice"); 15 } 16 17 int main() { 18 int t; 19 scanf("%d", &t); 20 while (t--) { 21 solve(); 22 } 23 24 return 0; 25 }
参考资料
Codeforces Round #832 (Div. 2) Editorial:https://codeforces.com/blog/entry/108782
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/16884344.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效