This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
#define int long long
#define ll long long
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define freopen(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout);
const int N = 1000000 + 5;
const int M = 4000 + 5;
const ll mod = 1e9 + 7;
const ll inf = 1e17;
int n, a[N], b[N], mna[N], mxa[N], mnb[N], mxb[N], cnt;
string s;
void solve(){
cin >> n;
n *= 2;
for(int i = 1; i <= n; i ++){
cin >> a[i];
}
for(int i = 1; i <= n; i++){
cin >> b[i];
}
for(int i = 1; i <= n; i++){
mna[i] = inf;
mnb[i] = inf;
mxa[i] = -inf;
mxb[i] = -inf;
}
for(int i = 1; i <= n; i++){
if(a[i] >= a[i - 1]){
mna[i] = mna[i - 1] + 1;
mxa[i] = mxa[i - 1] + 1;
}
if(a[i] >= b[i - 1]){
mna[i] = min(mna[i], mnb[i - 1] + 1);
mxa[i] = max(mxa[i], mxb[i - 1] + 1);
}
if(b[i] >= a[i - 1]){
mnb[i] = mna[i - 1];
mxb[i] = mxa[i - 1];
}
if(b[i] >= b[i - 1]){
mnb[i] = min(mnb[i], mnb[i - 1]);
mxb[i] = max(mxb[i], mxb[i - 1]);
}
}
char c = 'C';
cnt = n / 2;
if(mna[n] <= cnt && mxa[n] >= cnt){
c = 'A';
cnt--;
}else if(mnb[n] <= cnt && mxb[n] >= cnt){
c = 'B';
}
if(c == 'C'){
cout << -1;
return;
}
s += c;
while(n > 1){
if(c == 'A'){
if(a[n] >= a[n - 1] && mna[n - 1] <= cnt && mxa[n - 1] >= cnt){
c = 'A';
cnt--;
}else if(a[n] >= b[n - 1] && mnb[n - 1] <= cnt && mxb[n - 1] >= cnt){
c = 'B';
}
}else{
if(b[n] >= a[n - 1] && mna[n - 1] <= cnt && mxa[n - 1] >= cnt){
c = 'A';
cnt--;
}else if(b[n] >= b[n - 1] && mnb[n - 1] <= cnt && mxb[n - 1] >= cnt){
c = 'B';
}
}
s += c;
n--;
}
reverse(all(s));
cout << s;
}
main() {
fast
int tt = 1;
// cin >> tt;
while(tt--){
solve();
}
}
Compilation message (stderr)
building4.cpp:3: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
3 | #pragma GCC optimization("O3")
|
building4.cpp:4: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
4 | #pragma GCC optimization("unroll-loops")
|
building4.cpp:5: warning: ignoring '#pragma comment ' [-Wunknown-pragmas]
5 | #pragma comment(linker, "/stack:200000000")
|
building4.cpp:98:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
98 | main() {
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |