# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
245627 | thecodingwizard | Swap (BOI16_swap) | C++11 | 1032 ms | 256632 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000;
int n, A[maxn+10];
vector<int> dp[maxn+10][36], tmp1, tmp2;
bool v[maxn+10][40];
void merge(vector<int> &res, int root, vector<int> &L, vector<int> &R) {
res.push_back(root);
for (int len = 1, i = 0, j = 0; i < L.size() || j < R.size(); i += len, j += len, len *= 2) {
for (int k = 0; k < len && i+k<L.size(); k++) res.push_back(L[i+k]);
for (int k = 0; k < len && i+k<R.size(); k++) res.push_back(R[i+k]);
}
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin >> n; for (int i = 1; i <= n; i++) cin >> A[i];
for (int i = 1; i <= n; i++) for (int j = 0; j < 36; j++) v[i][j] = false;
v[1][0] = 1;
for (int i = 1; 2*i+1 <= n; i++) {
for (int j = 0; j < 36; j++) {
if (!v[i][j]) continue;
int idx = i>>(j/2);
if (j&1) idx *= 2;
int cur = A[idx];
v[i<<1][0] = v[i<<1^1][0] = true;
if (A[i<<1]<A[idx]&&A[i<<1]<A[i<<1^1]) {
v[i<<1][j+2] = true;
} else if (A[i<<1^1]<cur&&A[i<<1^1]<A[i<<1]) {
v[i<<1^1][3] = v[i<<1][j+2] = v[i<<1^1][j+2] = true;
}
}
}
for (int i = n; i; i--) {
int l = i<<1, r = i<<1^1;
for (int j = 0; j < 36; j++) {
if (!v[i][j]) continue;
int idx = i>>(j/2);
if (j&1) idx *= 2;
int cur = A[idx];
if (l > n && r > n) {
dp[i][j] = {cur};
} else if (r > n) {
dp[i][j] = {min(A[l],cur),max(A[l],cur)};
} else {
int L = A[l], R = A[r];
if (R < cur && R < L) {
tmp1 = {}, tmp2 = {};
// swap right
merge(tmp1, R, dp[l][0], dp[r][j+2]);
// swap both
merge(tmp2, R, dp[l][j+2], dp[r][3]);
dp[i][j] = min(tmp1, tmp2);
} else {
// no swap / swap left
merge(dp[i][j], cur<L?cur:L, dp[l][cur<L?0:j+2], dp[r][0]);
}
}
}
if (l <= n) {
for (int j = 0; j < 36; j++) if (v[l][j]) vector<int>().swap(dp[l][j]);
}
if (r <= n) {
for (int j = 0; j < 36; j++) if (v[r][j]) vector<int>().swap(dp[r][j]);
}
}
for (int v : dp[1][0]) cout << v << " ";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |