이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,avx2")
#pragma GCC optimize("unroll-loops,O2")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pll;
constexpr int N = 5e5+10,mod = 998244353,inf = 1e9+10,sq = 700;
inline int mkay(int a,int b){
if (a+b >= mod) return a+b-mod;
if (a+b < 0) return a+b+mod;
return a+b;
}
inline int poww(int a,int k){
if (k < 0) return 0;
int z = 1;
while (k){
if (k&1) z = 1ll*z*a%mod;
a = 1ll*a*a%mod;
k /= 2;
}
return z;
}
int n,a[N],b[N];
int mx[N][2],mi[N][2];
int main(){
ios :: sync_with_stdio(0); cin.tie(0);
cin >> n;
n *= 2;
rep(i,0,n) cin >> a[i];
rep(i,0,n) cin >> b[i];
int cur = 0;
rep(i,0,n){
if (max(a[i],b[i]) < cur){
cout << -1 << endl;
return 0;
}
if (min(a[i],b[i]) >= cur){
cur = min(a[i],b[i]);
}
else cur = max(a[i],b[i]);
}
mx[n-1][0] = mi[n-1][0] = 1;
mx[n-1][1] = mi[n-1][1] = -1;
repr(i,n-2,0){
mx[i][0] = mx[i][1] = -inf;
mi[i][0] = mi[i][1] = inf;
if (a[i] < a[i+1]){
mx[i][0] = max(mx[i][0],mx[i+1][0]+1);
mi[i][0] = min(mi[i][0],mi[i+1][0]+1);
}
if (a[i] < b[i+1]){
mx[i][0] = max(mx[i][0],mx[i+1][1]+1);
mi[i][0] = min(mi[i][0],mi[i+1][1]+1);
}
if (b[i] < a[i+1]){
mx[i][1] = max(mx[i][1],mx[i+1][0]-1);
mi[i][1] = min(mi[i][1],mi[i+1][0]-1);
}
if (b[i] < b[i+1]){
mx[i][1] = max(mx[i][1],mx[i+1][1]-1);
mi[i][1] = min(mi[i][1],mi[i+1][1]-1);
}
}
if ((mi[0][0] > 0 || mx[0][0] < 0) && (mi[0][1] > 0 || mx[0][1] < 0)){
cout << -1;
return 0;
}
cur = 0;
int s = 0;
string ans = "";
rep(i,0,n){
if (min(a[i],b[i]) < cur){
cur = max(a[i],b[i]);
if (a[i] == cur){
s++;
ans += 'A';
}
else{
s--;
ans += 'B';
}
continue;
}
if (i == n-1){
if (s == -1) ans += 'A';
else ans += 'B';
continue;
}
if ((a[i+1] < a[i] || mi[i+1][0] > -s-1 || mx[i+1][0] < -s-1) && (b[i+1] < a[i] || mi[i+1][1] > -s-1 || mx[i+1][1] < -s-1)){
ans += 'B';
s--;
cur = b[i];
}
else{
ans += 'A';
s++;
cur = a[i];
}
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |