#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using pi = pair<int,int>;
#define f first
#define s second
#define mp make_pair
const int MX = 200005;
int n; int num[MX]; map<pi, vi> dp;
vi merge(vi &a, int x, vi &b) {
vi ret;
ret.pb(x);
int p1 = 0, p2 = 0;
for (int i = 1; i < 36; i++) {
for (int j = 0; j < i; j++) {
if (p1 < sz(a)) {
ret.pb(a[p1++]);
}
}
for (int j = 0; j < i; j++) {
if (p2 < sz(b)) {
ret.pb(b[p2++]);
}
}
}
return ret;
}
void pr(vi &x) {
for (int v : x) {
cout << v << " ";
}
cout << '\n';
}
vi solve(int v, int c) {
// cout << "v: " << v << " " << c << endl;
if (!dp[mp(v, c)].empty()) {
// pr(dp[mp(v, c)]);
return dp[mp(v, c)];
}
int l = 2 * v + 1, r = 2 * v + 2;
if (2 * v + 1 >= n) {
// cout << c << '\n';
return dp[mp(v, c)] = {c};
}
vi best;
// no swaps
vi fst = solve(l, num[l]);
vi scnd = solve(r, num[r]);
best = merge(fst, c, scnd);
// swap left
fst = solve(l, c), scnd = solve(r, num[r]);
best = min(best, merge(fst, num[l], scnd));
// swap right
if (r < n) {
fst = solve(l, num[l]);
scnd = solve(r, c);
best = min(best, merge(fst, num[r], scnd));
}
// swap left, then right
if (r < n) {
fst = solve(l, c);
scnd = solve(r, num[l]);
best = min(best, merge(fst, num[r], scnd));
}
// pr(best);
return dp[mp(v, c)] = best;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> num[i];
}
solve(0, num[0]);
vi ans = dp[mp(0, num[0])];
for (int i = 0; i < n; i++) {
cout << ans[i] << ' ';
}
cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |