C. Swap Game

C. Swap Game

Alice and Bob are playing a game on an array $a$ of $n$ positive integers. Alice and Bob make alternating moves with Alice going first.

In his/her turn, the player makes the following move:

  • If $a_1=0$, the player loses the game, otherwise:
  • Player chooses some $i$ with $2 \leq i \leq n$. Then player decreases the value of $a_1$ by $1$ and swaps $a_1$ with $a_i$.

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 $t$ $(1 \leq t \leq 2 \cdot {10}^{4})$ — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $n$ $(2 \leq n \leq {10}^{5})$ — the length of the array $a$.

The second line of each test case contains $n$ integers $a_1,a_2 \ldots a_n$ $(1 \leq a_i \leq {10}^{9})$ — the elements of the array $a$.

It is guaranteed that sum of $n$ over all test cases does not exceed $2 \cdot {10}^{5}$.

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 $i=2$, making the array equal $[1,0]$. Then Bob, in his turn, will also choose $i=2$ and make the array equal $[0,0]$. As $a_1=0$, Alice loses.

In the second testcase, once again, players can only choose $i=2$. Then the array will change as follows: $[2, 1] \to [1, 1] \to [1, 0] \to [0, 0]$, and Bob loses.

In the third testcase, we can show that Alice has a winning strategy.

 

解题思路

  数组首个元素最先减到$0$的就会输,因此可以考虑一开始的$a_1 > \min\limits_{1 \leq i \leq n}{a_i}$和$a_1 = \min\limits_{1 \leq i \leq n}{a_i}$这两种情况。

  如果是$a_1 > \min\limits_{1 \leq i \leq n}{a_i}$,那么Alice可以每次把最小的元素换到第一个位置,然后轮到Bob时就会对这个最小的元素减$1$(这个元素还是最小元素)。而Bob只能将大于最小值(也就是大于$a_1$)的元素换到第一个位置。可以发现,这样子Alice一定可以将最小元素减到$0$,使得Bob输掉比赛。

  如果是$a_1 = \min\limits_{1 \leq i \leq n}{a_i}$,那么一开始Ailce对$a_1$减去$1$后,$a_1$就变成最小元素被换到数组的后面,这意味着下一次轮到Bob时,Bob是属于$a_1 > \min\limits_{1 \leq i \leq n}{a_i}$这种情况的,意味着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

posted @ 2022-11-12 18:09  onlyblues  阅读(42)  评论(0编辑  收藏  举报
Web Analytics