Submission #824984

#TimeUsernameProblemLanguageResultExecution timeMemory
824984xinkBuilding 4 (JOI20_building4)C++14
100 / 100
645 ms25900 KiB
#include <iostream> #include <vector> #include <utility> #include <sstream> #include <climits> #include <cstring> #include <algorithm> #define ll long long #define ld long double using namespace std; const ll mod = 1e9 + 7; const int maxn = 5e5 + 5, maxn1 = 2e3 + 5; typedef vector<int> vi; typedef pair<int, int> ii; typedef vector<ii> vii; bool can_construct[maxn1][maxn1][2]; int arr[2 * maxn][2], dp_min[2 * maxn][2], dp_max[2 * maxn][2]; void solve() { int n; cin >> n; for (int i = 0; i < 2 * n; i++) { cin >> arr[i][0]; } for (int i = 0; i < 2 * n; i++) { cin >> arr[i][1]; } dp_min[0][0] = 0, dp_max[0][0] = 0; dp_min[0][1] = 1, dp_max[0][1] = 1; for (int i = 1; i < 2 * n; i++) { for (int curr = 0; curr < 2; curr++) { dp_min[i][curr] = i + 1; for (int prev = 0; prev < 2; prev++) { if (arr[i][curr] >= arr[i - 1][prev]) { dp_min[i][curr] = min(dp_min[i][curr], dp_min[i - 1][prev] + curr); dp_max[i][curr] = max(dp_max[i][curr], dp_max[i - 1][prev] + curr); } } } } int curr_arr = -1, n_arr1 = n; for (int i = 0; i < 2; i++) { if (dp_min[2 * n - 1][i] <= n && n <= dp_max[2 * n - 1][i]) { curr_arr = i; break; } } if (curr_arr == -1) { cout << -1; return; } string ans; for (int i = 2 * n - 1; i >= 0; i--) { ans += (curr_arr ? 'B' : 'A'); for (int nxt = 0; nxt < 2; nxt++) { if (i > 0 && arr[i][curr_arr] >= arr[i - 1][nxt] && dp_min[i - 1][nxt] <= n_arr1 - curr_arr && n_arr1 - curr_arr <= dp_max[i - 1][nxt]) { n_arr1 -= curr_arr; curr_arr = nxt; break; } } } reverse(ans.begin(), ans.end()); cout << ans; } int main() { // freopen("input_text", "r", stdin); // freopen("output_text", "w", stdout); // ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0); int t = 1; // cin >> t; while (t-- > 0) solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...