제출 #251184

#제출 시각아이디문제언어결과실행 시간메모리
251184imeimi2000건물 4 (JOI20_building4)C++17
100 / 100
332 ms45548 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...