이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <complex>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
using namespace std;
// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define derr if(1) cerr
typedef vector<int> vi;
// END NO SAD
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<vector<ll>> matrix;
typedef pair<int, pii> state;
void die() {
cout << "-1\n";
exit(0);
}
int a[1000000];
int b[1000000];
pii dp[1000001][2]; // [i][0] gives range of min, max of A's, selecting A last
const pii BAD = {1e9, -1e9};
void solve() {
int n;
cin >> n;
n *= 2;
int want = n/2;
for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 0; i < n; i++) cin >> b[i];
dp[1][0] = {1, 1};
dp[1][1] = {0, 0};
for(int i = 1; i < n; i++) {
int clhs = 1e9;
int crhs = -1e9;
if(a[i] >= a[i-1]) {
clhs = min(clhs, dp[i][0].first + 1);
crhs = max(crhs, dp[i][0].second + 1);
}
if(a[i] >= b[i-1]) {
clhs = min(clhs, dp[i][1].first + 1);
crhs = max(crhs, dp[i][1].second + 1);
}
dp[i+1][0] = {clhs, crhs};
clhs = 1e9;
crhs = -1e9;
if(b[i] >= a[i-1]) {
clhs = min(clhs, dp[i][0].first);
crhs = max(crhs, dp[i][0].second);
}
if(b[i] >= b[i-1]) {
clhs = min(clhs, dp[i][1].first);
crhs = max(crhs, dp[i][1].second);
}
dp[i+1][1] = {clhs, crhs};
}
pii src = {-1, -1};
if(dp[n][0].first <= want && want <= dp[n][0].second) src = {n, 0};
if(dp[n][1].first <= want && want <= dp[n][1].second) src = {n, 1};
if(src.first == -1) die();
vector<char> ret;
int numa = want;
while(src.first) {
if(src.second) ret.push_back('B');
else {
ret.push_back('A');
numa--;
}
if(--src.first == 0) break;
int now = src.second ? b[src.first] : a[src.first];
if(now >= a[src.first-1] && dp[src.first][0].first <= numa && numa <= dp[src.first][0].second) {
src.second = 0;
}
else {
src.second = 1;
}
assert(dp[src.first][src.second].first <= numa && numa <= dp[src.first][src.second].second);
}
reverse(all(ret));
for(char out: ret) cout << out; cout << "\n";
}
// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
solve();
}
컴파일 시 표준 에러 (stderr) 메시지
building4.cpp: In function 'void solve()':
building4.cpp:97:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
for(char out: ret) cout << out; cout << "\n";
^~~
building4.cpp:97:35: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
for(char out: ret) cout << out; cout << "\n";
^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |