#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;
const int MAXN = 10000;
vector<vector<int>> dp[MAXN];
vector<int> subtr[MAXN];
int p[MAXN];
vector<int> unite_subtr(vector<int> ltr, vector<int> rtr) {
auto lit = ltr.begin();
auto rit = rtr.begin();
vector<int> ans = {-1};
for (int sz = 1; lit < ltr.end() || rit < rtr.end(); sz *= 2) {
for (int i = 0; i < sz && lit + i < ltr.end(); ++i)
ans.push_back(lit[i]);
for (int i = 0; i < sz && rit + i < rtr.end(); ++i)
ans.push_back(rit[i]);
lit += sz;
rit += sz;
}
return ans;
}
vector<int> merge(vector<int> a, vector<int> b) {
auto bit = b.begin();
vector<int> ans;
for (auto ait = a.begin(); ait < a.end(); ++ait) {
while (bit < b.end() && *bit < *ait) {
ans.push_back(*(bit++));
}
ans.push_back(*ait);
}
while (bit < b.end()) {
ans.push_back(*(bit++));
}
return ans;
}
vector<int> get_substit(vector<int> v, int pvl, int nvl) {
vector<int> ans;
for (auto x : v) {
if (x == pvl)
ans.push_back(nvl);
else
ans.push_back(x);
}
return ans;
}
vector<int> get_dp(int i, int t) {
int lchd = i * 2 + 1;
int rchd = i * 2 + 2;
if (subtr[i].size() == 1)
return {t};
if (subtr[i].size() == 2)
return {min(t, p[lchd]), max(t, p[lchd])};
int ti = lower_bound(subtr[i].begin(), subtr[i].end(), t) - subtr[i].begin();
assert(t < subtr[i][ti]);
if (t != subtr[i][ti] - 1) {
return get_substit(get_dp(i, subtr[i][ti] - 1), subtr[i][ti] - 1, t);
}
if (dp[i][ti].size()) {
return dp[i][ti];
}
if (t < p[lchd] && t < p[rchd]) {
dp[i][ti] = unite_subtr(get_dp(lchd, p[lchd]), get_dp(rchd, p[rchd]));
dp[i][ti][0] = t;
return dp[i][ti];
}
if (p[lchd] < t && p[lchd] < p[rchd]) {
dp[i][ti] = unite_subtr(get_dp(lchd, t), get_dp(rchd, p[rchd]));
dp[i][ti][0] = p[lchd];
return dp[i][ti];
}
vector<int> dp0 = unite_subtr(get_dp(lchd, p[lchd]), get_dp(rchd, t));
vector<int> dp1 = unite_subtr(get_dp(lchd, t), get_dp(rchd, p[lchd]));
dp[i][ti] = min(dp0, dp1);
dp[i][ti][0] = p[rchd];
return dp[i][ti];
}
signed main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> p[i];
--p[i];
}
for (int i = n - 1; i > 0; --i) {
int rt = (i - 1) / 2;
subtr[rt] = merge(subtr[rt], subtr[i]);
subtr[rt] = merge(subtr[rt], {p[i]});
}
for (int i = 0; i < n; ++i) {
subtr[i].push_back(n + 1);
dp[i].resize(subtr[i].size());
}
for (auto x : get_dp(0, p[0])) {
cout << x + 1 << ' ';
}
cout << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
2 ms |
1160 KB |
Output is correct |
13 |
Correct |
3 ms |
1028 KB |
Output is correct |
14 |
Correct |
2 ms |
1108 KB |
Output is correct |
15 |
Correct |
2 ms |
1032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
2 ms |
1160 KB |
Output is correct |
13 |
Correct |
3 ms |
1028 KB |
Output is correct |
14 |
Correct |
2 ms |
1108 KB |
Output is correct |
15 |
Correct |
2 ms |
1032 KB |
Output is correct |
16 |
Runtime error |
14 ms |
1620 KB |
Execution killed with signal 11 |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
2 ms |
1160 KB |
Output is correct |
13 |
Correct |
3 ms |
1028 KB |
Output is correct |
14 |
Correct |
2 ms |
1108 KB |
Output is correct |
15 |
Correct |
2 ms |
1032 KB |
Output is correct |
16 |
Runtime error |
14 ms |
1620 KB |
Execution killed with signal 11 |
17 |
Halted |
0 ms |
0 KB |
- |