Removals Game
题面翻译
题目描述
Alice 得到了一个排列 a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an,它是 [ 1 , 2 , … , n ] [1,2,\ldots,n] [1,2,…,n] 的一个排列,而 Bob 也得到了另一个排列 b 1 , b 2 , … , b n b_1, b_2, \ldots, b_n b1,b2,…,bn,同样是 [ 1 , 2 , … , n ] [1,2,\ldots,n] [1,2,…,n] 的一个排列。他们打算用这两个数组来进行一个游戏。
每轮游戏中,以下事件按顺序发生:
- Alice 选择她数组中的第一个或最后一个元素并将其从数组中移除;
- Bob 选择他数组中的第一个或最后一个元素并将其从数组中移除。
游戏持续进行 n − 1 n-1 n−1 轮,之后两个数组都将只剩下一个元素: a a a 数组中的 x x x 和 b b b 数组中的 y y y。
如果 x = y x=y x=y,Bob 获胜;否则,Alice 获胜。假设两个玩家都采取最优策略,请找出哪个玩家将获胜。
输入格式
每个测试样例包含多个测试用例。第一行包含测试用例的数量 t t t( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104)。接下来是各个测试用例的描述。
每个测试用例的第一行包含一个整数 n n n( 1 ≤ n ≤ 3 ⋅ 1 0 5 1 \le n \le 3 \cdot 10^5 1≤n≤3⋅105)。
接下来的一行包含 n n n 个整数 a 1 , a 2 , … , a n a_1,a_2,\ldots,a_n a1,a2,…,an( 1 ≤ a i ≤ n 1 \le a_i \le n 1≤ai≤n,所有 a i a_i ai 都是不同的)——这是 Alice 的排列。
再下一行包含 n n n 个整数 b 1 , b 2 , … , b n b_1,b_2,\ldots,b_n b1,b2,…,bn( 1 ≤ b i ≤ n 1 \le b_i \le n 1≤bi≤n,所有 b i b_i bi 都是不同的)——这是 Bob 的排列。
题目保证所有 n n n 的总和不超过 3 ⋅ 1 0 5 3 \cdot 10^5 3⋅105。
输出格式
对于每个测试用例,输出一行包含胜利者的名字,假设两个玩家都采取最优策略。如果 Alice 获胜,输出 Alice \texttt{Alice} Alice;否则,输出 Bob \texttt{Bob} Bob。
提示说明
在第一个测试用例中,Bob 可以通过删除与 Alice 相同的元素来赢得游戏。
在第二个测试用例中,Alice 可以在第一轮删除 3 3 3,然后在第二轮删除与 Bob 第一轮删除的不同元素来赢得游戏。
题目描述
Alice got a permutation $ a_1, a_2, \ldots, a_n $ of $ [1,2,\ldots,n] $ , and Bob got another permutation $ b_1, b_2, \ldots, b_n $ of $ [1,2,\ldots,n] $ . They are going to play a game with these arrays.
In each turn, the following events happen in order:
- Alice chooses either the first or the last element of her array and removes it from the array;
- Bob chooses either the first or the last element of his array and removes it from the array.
The game continues for $ n-1 $ turns, after which both arrays will have exactly one remaining element: $ x $ in the array $ a $ and $ y $ in the array $ b $ .
If $ x=y $ , Bob wins; otherwise, Alice wins. Find which player will win if both players play optimally.
输入格式
Each test contains multiple test cases. The first line contains the number of test cases $ t $ ( $ 1\le t\le10^4 $ ). The description of the test cases follows.
The first line of each test case contains a single integer $ n $ ( $ 1\le n\le 3\cdot 10^5 $ ).
The next line contains $ n $ integers $ a_1,a_2,\ldots,a_n $ ( $ 1\le a_i\le n $ , all $ a_i $ are distinct) — the permutation of Alice.
The next line contains $ n $ integers $ b_1,b_2,\ldots,b_n $ ( $ 1\le b_i\le n $ , all $ b_i $ are distinct) — the permutation of Bob.
It is guaranteed that the sum of all $ n $ does not exceed $ 3\cdot 10^5 $ .
输出格式
For each test case, print a single line with the name of the winner, assuming both players play optimally. If Alice wins, print $ \texttt{Alice} $ ; otherwise, print $ \texttt{Bob} $ .
样例 #1
样例输入 #1
2
2
1 2
1 2
3
1 2 3
2 3 1
样例输出 #1
Bob
Alice
提示说明
In the first test case, Bob can win the game by deleting the same element as Alice did.
In the second test case, Alice can delete $ 3 $ in the first turn, and then in the second turn, delete the element that is different from the one Bob deleted in the first turn to win the game.
代码内容
// #include <iostream>
// #include <algorithm>
// #include <cstring>
// #include <stack>//栈
// #include <deque>//队列
// #include <queue>//堆/优先队列
// #include <map>//映射
// #include <unordered_map>//哈希表
// #include <vector>//容器,存数组的数,表数组的长度
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=3e5+10;
ll a[N],b[N];
int main()
{
ll t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
for(ll i=0;i<n;i++)
cin>>a[i];
for(ll i=0;i<n;i++)
cin>>b[i];
ll f1=0,f2=0;
for(ll i=0;i<n;i++)
{
if(a[i]!=b[i]) f1=1;
if(a[i]!=b[n-i-1]) f2=1;
}
if(!f1||!f2) cout<<"Bob"<<endl;
else cout<<"Alice"<<endl;
}
return 0;
}