Submission #1300362

#TimeUsernameProblemLanguageResultExecution timeMemory
1300362nguyenletrung건물 4 (JOI20_building4)C++20
100 / 100
174 ms22008 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; int n, a[1000005], b[1000005], c[1000005]; vector<char> ans, res; void golef(int id) { while (true) { if (id == 1 || max(a[id-1], b[id-1]) <= c[id]) return; else { if (ans[id-1] != ' ') { if (c[id-1] > c[id]) { cout << -1; exit(0); } return; } if (a[id-1] < b[id-1]) { c[id-1] = a[id-1]; ans[id-1] = 'A'; } else { c[id-1] = b[id-1]; ans[id-1] = 'B'; } id--; } } } void gorig(int id) { while (true) { if (id == n || c[id] <= min(a[id+1], b[id+1])) return; else { if (ans[id+1] != ' ') { if (c[id] > c[id+1]) { cout << -1; exit(0); } return; } if (c[id] > max(a[id+1], b[id+1])) { cout << -1; exit(0); } if (a[id+1] < b[id+1]) { c[id+1] = b[id+1]; ans[id+1] = 'B'; } else { c[id+1] = a[id+1]; ans[id+1] = 'A'; } id++; } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n; n *= 2; // resize ans/res once (index from 0..n+1, we'll use 1..n) ans.assign(n + 2, ' '); res.assign(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 - 1; ++i) { if (ans[i] != ' ') continue; if (min(a[i], b[i]) > max(a[i+1], b[i+1])) { cout << -1; return 0; } if (max(a[i], b[i]) > max(a[i+1], b[i+1])) { if (a[i] > b[i]) { c[i] = b[i]; ans[i] = 'B'; } else { c[i] = a[i]; ans[i] = 'A'; } golef(i); gorig(i); } if (min(a[i], b[i]) > min(a[i+1], b[i+1])) { if (a[i+1] > b[i+1]) { c[i+1] = a[i+1]; ans[i+1] = 'A'; } else { c[i+1] = b[i+1]; ans[i+1] = 'B'; } golef(i+1); gorig(i+1); } } int cnt = 0; for (int i = 1; i <= n; ++i) { if (ans[i] == 'A') cnt++; else if (ans[i] == 'B') cnt--; else // ans[i] == ' ' { if (a[i] < b[i]) { res[i] = 'B'; cnt--; } else if (b[i] < a[i]) { res[i] = 'A'; cnt++; } else { // nếu bằng nhau, chọn tạm B (giữ tính nhất quán với code cũ) res[i] = 'B'; cnt--; } } } for (int i = 1; i <= n; ++i) { if (ans[i] == ' ' && res[i] != ' ') { int mx = 0, mn = 0, sum = 0, nexi = i; for (int j = i; j <= n; ++j) { if (res[j] == ' ') break; sum += (res[j] == 'A' ? -2 : 2); mx = max(mx, sum); mn = min(mn, sum); nexi = j; if (j != n && max(a[j], b[j]) <= min(a[j+1], b[j+1])) break; } if (cnt < 0) { if (cnt + mx > 0) { mx = -cnt; cnt = 0; } else cnt += mx; int cursum = 0; bool ok = false; for (int j = i; j <= nexi; ++j) { if (cursum == mx) ok = true; if (!ok) { if (res[j] == 'A') ans[j] = 'B'; else ans[j] = 'A'; } else ans[j] = res[j]; cursum += (res[j] == 'A' ? -2 : 2); } } else { if (cnt + mn < 0) { mn = -cnt; cnt = 0; } else cnt += mn; int cursum = 0; bool ok = false; for (int j = i; j <= nexi; ++j) { if (cursum == mn) ok = true; if (!ok) { if (res[j] == 'A') ans[j] = 'B'; else ans[j] = 'A'; } else ans[j] = res[j]; cursum += (res[j] == 'A' ? -2 : 2); } } i = nexi; } } for (int i = 1; i <= n; ++i) { if (ans[i] == ' ') { if (cnt < 0) { cnt += 2; ans[i] = 'A'; } else ans[i] = 'B'; } } if (cnt != 0) cout << -1; else { for (int i = 1; i <= n; ++i) cout << ans[i]; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...