이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define trav(x,a) for (const auto& x: a)
#define sz(x) (int)(x).size()
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%I64d", &(n))
#define nai(a, n) for (int _i = 1; _i <= (n); _i++) ni(a[_i])
#define nal(a, n) for (int _i = 1; _i <= (n); _i++) nl(a[_i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%I64d\n", (n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
const int N = 1e6 + 5;
const double eps = 1e-9;
int a[N], b[N]; // lo, hi / up, down
array<array<int,2>,2> dp[N];
char ans[N];
int main() {
int n; ni(n);
nai(a, 2*n); nai(b, 2*n);
dp[1][0][0] = 1;
dp[1][0][1] = 0;
dp[1][1][0] = 1;
dp[1][1][1] = 0;
for (int i = 2; i <= 2*n; i++) {
if (max(a[i],b[i]) < min(a[i-1],b[i-1]))
return !pri(-1);
dp[i][0][0] = min(dp[i-1][0][0] + (a[i] >= a[i-1] ? 1 : inf), dp[i-1][0][1] + (a[i] >= b[i-1] ? 1 : inf));
dp[i][0][1] = min(dp[i-1][0][0] + (b[i] >= a[i-1] ? 0 : inf), dp[i-1][0][1] + (b[i] >= b[i-1] ? 0 : inf));
dp[i][1][0] = max(dp[i-1][1][0] + (a[i] >= a[i-1] ? 1 : -inf), dp[i-1][1][1] + (a[i] >= b[i-1] ? 1 : -inf));
dp[i][1][1] = max(dp[i-1][1][0] + (b[i] >= a[i-1] ? 0 : -inf), dp[i-1][1][1] + (b[i] >= b[i-1] ? 0 : -inf));
}
if ((dp[2*n][1][0] < n || n < dp[2*n][0][0]) && (dp[2*n][1][1] < n || n < dp[2*n][0][1]))
return !pri(-1);
int cur = 1, cnt = 0;
if (dp[2*n][0][0] <= n && n <= dp[2*n][1][0])
cur = 0, cnt = 1;
ans[2*n] = cur ? 'B' : 'A';
auto can = [&](int i, int x, int y) { // transition from (i,x) to (i+1,y)
if (x == 0)
return a[i] <= (y ? b[i+1] : a[i+1]);
return b[i] <= (y ? b[i+1] : a[i+1]);
};
for (int i = 2*n-1; i; i--) {
if (dp[i][0][0] <= n-cnt && n-cnt <= dp[i][1][0] && can(i,0,cur)) { // go up
cnt++;
cur = 0;
} else {
cur = 1;
}
ans[i] = cur ? 'B' : 'A';
}
printf("%s\n", ans+1);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
building4.cpp: In function 'int main()':
building4.cpp:10:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
#define ni(n) scanf("%d", &(n))
~~~~~^~~~~~~~~~~~
building4.cpp:41:10: note: in expansion of macro 'ni'
int n; ni(n);
^~
building4.cpp:10:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
#define ni(n) scanf("%d", &(n))
~~~~~^~~~~~~~~~~~
building4.cpp:12:53: note: in expansion of macro 'ni'
#define nai(a, n) for (int _i = 1; _i <= (n); _i++) ni(a[_i])
^~
building4.cpp:42:3: note: in expansion of macro 'nai'
nai(a, 2*n); nai(b, 2*n);
^~~
building4.cpp:10:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
#define ni(n) scanf("%d", &(n))
~~~~~^~~~~~~~~~~~
building4.cpp:12:53: note: in expansion of macro 'ni'
#define nai(a, n) for (int _i = 1; _i <= (n); _i++) ni(a[_i])
^~
building4.cpp:42:16: note: in expansion of macro 'nai'
nai(a, 2*n); nai(b, 2*n);
^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |