Skip to content

Commit ce59b3e

Browse files
Add files via upload
1 parent 1b52740 commit ce59b3e

File tree

3 files changed

+191
-0
lines changed

3 files changed

+191
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import sys
2+
for _ in range(int(input())):
3+
4+
n = int(input())
5+
xs = [int(x) for x in input().split()]
6+
7+
# MEX
8+
mex = 1
9+
s = set(xs)
10+
while mex in s:
11+
mex += 1
12+
13+
if mex < n+1:
14+
print(f"? {mex} {1 if mex > 1 else 2}")
15+
sys.stdout.flush()
16+
res = int(input())
17+
if res == 0:
18+
print(f"! A")
19+
sys.stdout.flush()
20+
else:
21+
print(f"! B")
22+
sys.stdout.flush()
23+
else:
24+
for i in range(n):
25+
if xs[i] == 1:
26+
a = i+1
27+
if xs[i] == n:
28+
b = i+1
29+
30+
assert a is not None and b is not None
31+
32+
print(f"? {a} {b}")
33+
sys.stdout.flush()
34+
35+
res = int(input())
36+
if res < n-1:
37+
print(f"! A")
38+
sys.stdout.flush()
39+
else:
40+
print(f"? {b} {a}")
41+
sys.stdout.flush()
42+
res = int(input())
43+
if res < n-1:
44+
print(f"! A")
45+
sys.stdout.flush()
46+
else:
47+
print(f"! B")
48+
sys.stdout.flush()
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#pragma GCC optimize("Ofast")
2+
#pragma GCC optimize("unroll-loops")
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
typedef long long ll;
7+
8+
int n;
9+
multiset<int> s;
10+
int main(){
11+
int tc;
12+
cin >> tc;
13+
while(tc--){
14+
cin >> n;
15+
vector<int> arr(n);
16+
s.clear();
17+
int numZeros = 0;
18+
int firstZero = -1;
19+
for(int i = 0; i < n; i++){
20+
cin >> arr[i];
21+
s.insert(arr[i]);
22+
23+
if (arr[i] == 0){
24+
numZeros++;
25+
if (firstZero == -1){
26+
firstZero = i;
27+
}
28+
else{
29+
s.erase(s.find(0));
30+
}
31+
}
32+
}
33+
if (numZeros == 0){
34+
cout << n << endl;
35+
continue;
36+
}
37+
38+
int mex = 0;
39+
while(s.find(mex) != s.end()){
40+
mex++;
41+
}
42+
43+
// We try the whole sequence with the first zero (exclude all the other zeros)
44+
bool can = true;
45+
int mini = INT_MAX;
46+
for (int i=0; i<n; i++){
47+
if (arr[i] == 0){
48+
break;
49+
}
50+
51+
mini = min(mini, arr[i]);
52+
s.erase(s.find(arr[i]));
53+
if (arr[i] < mex && s.find(arr[i]) == s.end()){
54+
mex = arr[i];
55+
}
56+
57+
if (mini < mex){
58+
can = false;
59+
break;
60+
}
61+
}
62+
63+
if (can){
64+
cout << n - numZeros + 1<< endl;
65+
}
66+
else{
67+
cout << n - numZeros << endl;
68+
}
69+
}
70+
71+
return 0;
72+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#pragma GCC optimize("Ofast")
2+
#pragma GCC optimize("unroll-loops")
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
typedef long long ll;
7+
8+
int n;
9+
vector<int> jmp; // jmp[i] = j means a[i] XOR ... XOR a[j] = 0 and j is the smallest possible
10+
vector<int> a;
11+
12+
ll MOD = 1e9 + 7;
13+
vector<vector<ll>> memo;
14+
15+
ll dp(int idx, int t){
16+
// t = 0 => number of ways to finish given that the next index to choose is idx and v[X] = a[idx-1] for exactly one X in {0, 1, 2}
17+
// t = 1 => number of ways to finish given that the next index to choose is idx and v[0] = v[1] = v[2] = 0
18+
19+
if (idx == n) return 1;
20+
if (memo[idx][t] != -1) return memo[idx][t];
21+
22+
ll ans = 0;
23+
if (t == 1){
24+
if (jmp[idx] == -1){
25+
ans = 3;
26+
}
27+
else{
28+
ans = (dp(jmp[idx]+1, 1) * 3) % MOD;
29+
ans += (dp(jmp[idx]+1, 0) * 6) % MOD;
30+
}
31+
}
32+
else{
33+
if (jmp[idx-1] == -1){
34+
ans = 1;
35+
}
36+
else{
37+
ans = (dp(jmp[idx-1]+1, 1) * 1) % MOD;
38+
ans += (dp(jmp[idx-1]+1, 0) * 2) % MOD;
39+
}
40+
}
41+
42+
return memo[idx][t] = ans % MOD;
43+
}
44+
45+
int main(){
46+
int tc;
47+
cin >> tc;
48+
while(tc--){
49+
cin >> n;
50+
a.resize(n);
51+
jmp.assign(n, -1);
52+
53+
vector<int> prefixXor(n);
54+
map<int, int> m; // m[x] = i means a[0] XOR ... XOR a[i] = x
55+
m[0] = -1;
56+
for(int i = 0; i < n; i++){
57+
cin >> a[i];
58+
prefixXor[i] = a[i] ^ (i == 0 ? 0 : prefixXor[i-1]);
59+
60+
if (m.find(prefixXor[i]) != m.end()){
61+
jmp[m[prefixXor[i]] + 1] = i;
62+
}
63+
m[prefixXor[i]] = i;
64+
}
65+
66+
memo.assign(n, vector<ll>(2, -1));
67+
cout << dp(0, 1) << endl;
68+
}
69+
70+
return 0;
71+
}

0 commit comments

Comments
 (0)