#include <bits/extc++.h>
using std::vector;
using std::array;
using std::tuple;
using std::pair;
template <class F> struct rec_lambda : private F {
explicit rec_lambda(F&& f) : F(std::forward<F>(f)) {}
template <class... Args> decltype(auto) operator()(Args&&... args) const {
return F::operator()(*this, std::forward<Args>(args)...);
}
};
vector<int> merge(const int top, const vector<int>& l, const vector<int>& r) {
const int n = l.size(), m = r.size();
vector<int> ret;
ret.reserve(1 + n + m);
ret.push_back(top);
int i = 0, j = 1;
while (i < n or i < m) {
for (int k = i; k < i + j and k < n; ++k) {
ret.push_back(l[k]);
}
for (int k = i; k < i + j and k < m; ++k) {
ret.push_back(r[k]);
}
i += j;
j <<= 1;
}
return ret;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int N;
std::cin >> N;
vector<int> X(N);
for (auto& x : X) {
std::cin >> x;
}
__gnu_pbds::gp_hash_table<long long, vector<int>> dp;
const auto code = [&](const int root, const int top) {
return ((long long)root << 18) + top;
};
const auto ans = rec_lambda([&](auto&& dfs, const int root, const int top) -> vector<int> {
auto& ret = dp[code(root, top)];
if (!ret.empty()) {
return ret;
}
const int lch = 2 * root + 1, rch = 2 * root + 2;
if (lch >= N) {
return ret = {top};
}
if (rch >= N) {
if (top < X[lch]) {
return ret = {top, X[lch]};
} else {
return ret = {X[lch], top};
}
}
int a = top, b = X[lch], c = X[rch];
if (a < b and a < c) {
return ret = merge(a, dfs(lch, b), dfs(rch, c));
}
if (b < a and b < c) {
return ret = merge(b, dfs(lch, a), dfs(rch, c));
}
return ret = std::min(merge(c, dfs(lch, a), dfs(rch, b)), merge(c, dfs(lch, b), dfs(rch, a)));
})(0, X[0]);
for (int i = 0; i < N; ++i) {
std::cout << ans[i] << " \n"[i + 1 == N];
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |