#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define x1 fdgfdgf
#define y1 fdhfdgfdg
#define endl '\n'
const ll DIM = 1E6+7;
const ll INF = 1E18;
ll A[DIM],B[DIM],ans[DIM];
pair<ll,ll> dp[2][DIM];
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll n;
cin>>n;
n*=2;
ll last = 0;
for(ll i = 1;i<=n;++i) {
cin>>A[i];
}
for(ll i = 1;i<=n;++i){
cin>>B[i];
}
for(ll i = 1;i<=n;++i){
dp[0][i] = dp[1][i] = {INF,-INF};
if (A[i]>=A[i-1]){
dp[0][i].first = min(dp[0][i].first,dp[0][i-1].first+1);
dp[0][i].second = max(dp[0][i].second,dp[0][i-1].second+1);
}
if (A[i]>=B[i-1]){
dp[0][i].first = min(dp[0][i].first,dp[1][i-1].first+1);
dp[0][i].second = max(dp[0][i].second,dp[1][i-1].second+1);
}
if (B[i]>=A[i-1]){
dp[1][i].first = min(dp[1][i].first,dp[0][i-1].first);
dp[1][i].second = max(dp[1][i].second,dp[0][i-1].second);
}
if (B[i]>=B[i-1]){
dp[1][i].first = min(dp[1][i].first,dp[1][i-1].first);
dp[1][i].second = max(dp[1][i].second,dp[1][i-1].second);
}
}
ll got = n/2;
pair<ll,ll> cur;
if (dp[0][n].first<=n/2 && n/2<=dp[0][n].second){
cur = {0,n};
got--;
ans[n] = 0;
}
else if (dp[1][n].first<=n/2 && n/2<=dp[1][n].second){
cur = {1,n};
ans[n] = 1;
}
else{
cout<<"-1\n";
return 0;
}
while(cur.second>1){
ll val;
if (cur.first==0)val = A[cur.second];
else val = B[cur.second];
if (dp[0][cur.second-1].first<=got && got<=dp[0][cur.second-1].second && A[cur.second-1]<=val){
--got;
ans[cur.second-1] = 0;
cur = {0,cur.second-1};
}
else{
ans[cur.second-1] = 1;
cur = {1,cur.second-1};
}
}
for(ll i = 1;i<=n;++i){
if (ans[i]==0)cout<<'A';
else cout<<'B';
}
cout<<endl;
return 0;
}
Compilation message
building4.cpp: In function 'int main()':
building4.cpp:18:8: warning: unused variable 'last' [-Wunused-variable]
18 | ll last = 0;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
620 KB |
Output is correct |
7 |
Correct |
2 ms |
492 KB |
Output is correct |
8 |
Correct |
2 ms |
620 KB |
Output is correct |
9 |
Correct |
2 ms |
620 KB |
Output is correct |
10 |
Correct |
2 ms |
620 KB |
Output is correct |
11 |
Correct |
2 ms |
620 KB |
Output is correct |
12 |
Correct |
3 ms |
620 KB |
Output is correct |
13 |
Correct |
3 ms |
620 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
16 |
Correct |
2 ms |
620 KB |
Output is correct |
17 |
Correct |
2 ms |
620 KB |
Output is correct |
18 |
Correct |
2 ms |
620 KB |
Output is correct |
19 |
Correct |
3 ms |
620 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
2 ms |
620 KB |
Output is correct |
22 |
Correct |
2 ms |
620 KB |
Output is correct |
23 |
Correct |
3 ms |
640 KB |
Output is correct |
24 |
Correct |
2 ms |
620 KB |
Output is correct |
25 |
Correct |
2 ms |
620 KB |
Output is correct |
26 |
Correct |
3 ms |
620 KB |
Output is correct |
27 |
Correct |
2 ms |
620 KB |
Output is correct |
28 |
Correct |
2 ms |
620 KB |
Output is correct |
29 |
Correct |
2 ms |
620 KB |
Output is correct |
30 |
Correct |
2 ms |
620 KB |
Output is correct |
31 |
Correct |
2 ms |
620 KB |
Output is correct |
32 |
Correct |
2 ms |
620 KB |
Output is correct |
33 |
Correct |
2 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
620 KB |
Output is correct |
7 |
Correct |
2 ms |
492 KB |
Output is correct |
8 |
Correct |
2 ms |
620 KB |
Output is correct |
9 |
Correct |
2 ms |
620 KB |
Output is correct |
10 |
Correct |
2 ms |
620 KB |
Output is correct |
11 |
Correct |
2 ms |
620 KB |
Output is correct |
12 |
Correct |
3 ms |
620 KB |
Output is correct |
13 |
Correct |
3 ms |
620 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
16 |
Correct |
2 ms |
620 KB |
Output is correct |
17 |
Correct |
2 ms |
620 KB |
Output is correct |
18 |
Correct |
2 ms |
620 KB |
Output is correct |
19 |
Correct |
3 ms |
620 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
2 ms |
620 KB |
Output is correct |
22 |
Correct |
2 ms |
620 KB |
Output is correct |
23 |
Correct |
3 ms |
640 KB |
Output is correct |
24 |
Correct |
2 ms |
620 KB |
Output is correct |
25 |
Correct |
2 ms |
620 KB |
Output is correct |
26 |
Correct |
3 ms |
620 KB |
Output is correct |
27 |
Correct |
2 ms |
620 KB |
Output is correct |
28 |
Correct |
2 ms |
620 KB |
Output is correct |
29 |
Correct |
2 ms |
620 KB |
Output is correct |
30 |
Correct |
2 ms |
620 KB |
Output is correct |
31 |
Correct |
2 ms |
620 KB |
Output is correct |
32 |
Correct |
2 ms |
620 KB |
Output is correct |
33 |
Correct |
2 ms |
620 KB |
Output is correct |
34 |
Correct |
263 ms |
65676 KB |
Output is correct |
35 |
Correct |
285 ms |
70144 KB |
Output is correct |
36 |
Correct |
289 ms |
69100 KB |
Output is correct |
37 |
Correct |
300 ms |
70380 KB |
Output is correct |
38 |
Correct |
274 ms |
70380 KB |
Output is correct |
39 |
Correct |
271 ms |
69612 KB |
Output is correct |
40 |
Correct |
288 ms |
74796 KB |
Output is correct |
41 |
Correct |
272 ms |
71172 KB |
Output is correct |
42 |
Correct |
297 ms |
73068 KB |
Output is correct |
43 |
Correct |
305 ms |
75500 KB |
Output is correct |
44 |
Correct |
309 ms |
75500 KB |
Output is correct |
45 |
Correct |
313 ms |
75500 KB |
Output is correct |
46 |
Correct |
288 ms |
75500 KB |
Output is correct |
47 |
Correct |
288 ms |
74604 KB |
Output is correct |
48 |
Correct |
285 ms |
74348 KB |
Output is correct |
49 |
Correct |
299 ms |
75520 KB |
Output is correct |
50 |
Correct |
308 ms |
74732 KB |
Output is correct |
51 |
Correct |
309 ms |
74676 KB |
Output is correct |
52 |
Correct |
234 ms |
63852 KB |
Output is correct |
53 |
Correct |
230 ms |
63724 KB |
Output is correct |
54 |
Correct |
211 ms |
63724 KB |
Output is correct |
55 |
Correct |
214 ms |
63596 KB |
Output is correct |
56 |
Correct |
290 ms |
75756 KB |
Output is correct |
57 |
Correct |
259 ms |
70508 KB |
Output is correct |
58 |
Correct |
255 ms |
70764 KB |
Output is correct |
59 |
Correct |
255 ms |
70764 KB |
Output is correct |
60 |
Correct |
240 ms |
66924 KB |
Output is correct |
61 |
Correct |
265 ms |
71276 KB |
Output is correct |
62 |
Correct |
257 ms |
70252 KB |
Output is correct |
63 |
Correct |
264 ms |
71404 KB |
Output is correct |
64 |
Correct |
247 ms |
68716 KB |
Output is correct |
65 |
Correct |
259 ms |
71284 KB |
Output is correct |