Submission #1004762

#TimeUsernameProblemLanguageResultExecution timeMemory
1004762tvladm2009Building 4 (JOI20_building4)C++17
100 / 100
170 ms45624 KiB
// https://oj.uz/submission/945240 #include <iostream> // am dat o simulare pe vjudge mai demult si am bagat pb asta, o urc si pe main #include <complex> #include <vector> #include <string> #include <algorithm> #include <cstdio> #include <numeric> #include <cstring> #include <ctime> #include <cstdlib> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <list> #include <cmath> #include <bitset> #include <cassert> #include <queue> #include <stack> #include <deque> #include <random> using namespace std; template<typename T1, typename T2> inline void chkmin(T1 &a, T2 b) {if (a > b) a = b;} template<typename T1, typename T2> inline void chkmax(T1 &a, T2 b) {if (a < b) a = b;} #define files(FILENAME) read(FILENAME); write(FILENAME) #define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin) #define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout) #define all(c) (c).begin(), (c).end() #define sz(c) (int)(c).size() #define left left228 #define right right228 #define y1 y1228 #define mp make_pair #define pb push_back #define y2 y2228 #define rank rank228 using ll = long long; using ld = long double; const string FILENAME = "input"; const int MAXN = 500228; int n; int a[2 * MAXN], b[2 * MAXN]; int L[2 * MAXN][2], R[2 * MAXN][2]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); //read(FILENAME); cin >> n; for (int i = 1; i <= 2 * n; i++) { cin >> a[i]; } for (int i = 1; i <= 2 * n; i++) { cin >> b[i]; } for (int i = 1; i <= 2 * n; i++) { L[i][0] = L[i][1] = 1e9; R[i][0] = R[i][1] = -1e9; } L[1][0] = 1; R[1][0] = 1; L[1][1] = 0; R[1][1] = 0; for (int i = 1; i < 2 * n; i++) { if (a[i] <= a[i + 1]) { chkmin(L[i + 1][0], L[i][0] + 1); chkmax(R[i + 1][0], R[i][0] + 1); } if (a[i] <= b[i + 1]) { chkmin(L[i + 1][1], L[i][0]); chkmax(R[i + 1][1], R[i][0]); } if (b[i] <= a[i + 1]) { chkmin(L[i + 1][0], L[i][1] + 1); chkmax(R[i + 1][0], R[i][1] + 1); } if (b[i] <= b[i + 1]) { chkmin(L[i + 1][1], L[i][1]); chkmax(R[i + 1][1], R[i][1]); } } if (!(L[2 * n][0] <= n && R[2 * n][0] >= n) && !(L[2 * n][1] <= n && R[2 * n][1] >= n)) { cout << -1 << '\n'; return 0; } int i = 2 * n; int cnt = n; int last = ((L[2 * n][0] <= n && R[2 * n][0] >= n) ? 0 : 1); string sol = ""; while (i >= 2) { if (last == 0) { sol += 'A'; } else { sol += 'B'; } if (last == 0) { if (a[i - 1] <= a[i] && L[i - 1][0] + 1 <= cnt && cnt <= R[i - 1][0] + 1) { last = 0; } else if (b[i - 1] <= a[i] && L[i - 1][1] + 1 <= cnt && cnt <= R[i - 1][1] + 1) { last = 1; } cnt--; } else { if (a[i - 1] <= b[i] && L[i - 1][0] <= cnt && cnt <= R[i - 1][0]) { last = 0; } else if (b[i - 1] <= b[i] && L[i - 1][1] <= cnt && cnt <= R[i - 1][1]) { last = 1; } } i--; } if (last == 0) { sol += 'A'; } else { sol += 'B'; } reverse(all(sol)); cout << sol << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...