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>
using namespace std;
typedef pair<int, int> pii;
const pii inf = pii(1e8, -1e8);
pii operator+(pii a, pii b) {
return pii(min(a.first, b.first), max(a.second, b.second));
}
pii operator+(pii a, int b) {
return pii(a.first + b, a.second + b);
}
bool operator>(pii a, int b) {
return a.first <= b && b <= a.second;
}
int n;
int A[1000001][2];
pii dp[1000001][2];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n; n <<= 1;
for (int j = 0; j < 2; ++j) for (int i = 1; i <= n; ++i)
cin >> A[i][j];
dp[1][0] = pii(-1, -1);
dp[1][1] = pii(1, 1);
for (int i = 2; i <= n; ++i) {
for (int j = 0; j < 2; ++j) {
dp[i][j] = inf;
for (int k = 0; k < 2; ++k) {
if (A[i - 1][k] <= A[i][j])
dp[i][j] = dp[i][j] + (dp[i - 1][k] + (j + j - 1));
}
}
}
int j;
for (j = 0; j < 2; ++j) if (dp[n][j] > 0) break;
if (j == 2) return printf("-1\n"), 0;
string ans;
for (int i = n, sum = 0; ; --i) {
ans.push_back('A' + j);
if (i == 1) break;
sum -= j + j - 1;
for (int k = 0; k < 2; ++k) {
if (A[i - 1][k] <= A[i][j] && dp[i - 1][k] > sum) {
j = k;
break;
}
}
}
reverse(ans.begin(), ans.end());
printf("%s\n", ans.c_str());
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |