QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#226381 | #7327. Median on Binary Tree | PPP# | WA | 21ms | 13824kb | C++17 | 2.6kb | 2023-10-25 21:44:54 | 2023-10-25 21:44:55 |
Judging History
answer
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long ll;
typedef long double ld;
int n;
const int maxN = 2e5 + 10;
int w[maxN];
vector<int> dp[maxN];
vector<int> sub[maxN];
int ans[maxN];
int TRASH[maxN];
int SZ = 0;
void solve() {
for (int i = 1; i <= n; i++) {
cin >> w[i];
}
for (int i = n; i >= 1; i--) {
sub[i].emplace_back(w[i]);
SZ = 0;
if (2 * i > n) {
SZ = 1;
TRASH[0] = n + 1;
}
else if (2 * i + 1 > n) {
SZ = sub[2 * i].size();
for (int j = 0; j < SZ; j++) TRASH[j] = sub[2 * i][j];
}
else {
merge(sub[2 * i].begin(), sub[2 * i].end() - 1, sub[2 * i + 1].begin(), sub[2 * i + 1].end(), TRASH);
SZ = sub[2 * i].size() + sub[2 * i + 1].size() - 1;
}
sub[i].resize(SZ + 1);
int fi_g = -1;
for (int j = 0; j < SZ; j++) {
if (TRASH[j] >= w[i]) {
fi_g = j;
break;
}
}
assert(fi_g != -1);
for (int p = 0; p < fi_g; p++) {
sub[i][p] = TRASH[p];
}
sub[i][fi_g] = w[i];
for (int p = fi_g; p < SZ; p++) {
sub[i][p + 1] = TRASH[p];
}
dp[i].resize(sub[i].size());
int pt_l = 0;
int pt_r = 0;
const int INF = -1e9;
int Q = 0;
for (int p : sub[i]) {
int S = -INF;
if (w[i] >= p) S = 1;
else S = -1;
if (n >= 2 * i) {
while (sub[2 * i][pt_l] < p) pt_l++;
assert(pt_l < sub[2 * i].size());
S += dp[2 * i][pt_l];
}
if (n >= 2 * i + 1) {
while (sub[2 * i + 1][pt_r] < p) {
pt_r++;
}
assert(pt_r < sub[2 * i + 1].size());
S += dp[2 * i + 1][pt_r];
}
if (S >= 1) {
ans[S - 1] = max(ans[S - 1], p);
}
S = max(S, 0);
dp[i][Q] = S;
Q++;
}
}
for (int i = n - 2; i >= 0; i--) ans[i] = max(ans[i], ans[i + 1]);
ans[0] = n;
for (int i = 0; i < n; i++) cout << ans[i] << " ";
cout << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef DEBUG
freopen("input.txt", "r", stdin);
#endif
while (cin >> n) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 12980kb
input:
5 1 2 3 4 5 10 9 10 4 2 3 5 7 1 8 6
output:
5 2 2 1 1 10 9 5 4 4 3 3 2 2 1
result:
ok 15 numbers
Test #2:
score: -100
Wrong Answer
time: 21ms
memory: 13824kb
input:
10 4 10 7 9 5 3 1 2 6 8 10 7 10 2 4 9 8 3 1 5 6 10 1 5 8 3 6 7 2 4 10 9 10 7 9 3 6 2 1 10 5 4 8 10 7 1 9 2 4 5 3 6 8 10 10 2 7 5 9 10 3 1 4 8 6 10 9 8 1 2 6 10 5 4 3 7 10 3 6 1 9 10 8 2 7 4 5 10 7 3 8 10 4 1 5 2 6 9 10 6 10 7 5 2 9 1 4 3 8 10 6 8 5 10 9 4 2 1 3 7 10 4 3 5 9 1 2 8 7 10 6 10 7 5 10 8 ...
output:
10 9 6 5 5 4 4 3 2 1 10 9 7 6 5 4 4 3 2 1 10 9 7 6 5 4 4 3 2 1 10 9 7 6 5 4 4 3 2 1 10 9 7 6 5 4 4 3 2 1 10 9 7 7 6 4 4 3 2 1 10 9 7 7 6 4 4 3 2 1 10 9 7 7 6 4 4 3 2 1 10 9 7 7 6 4 4 3 2 1 10 9 7 7 6 4 4 3 2 1 10 9 8 7 6 5 4 3 2 1 10 9 8 7 6 5 4 3 2 1 10 9 8 7 6 5 4 3 2 1 10 9 8 7 6 5 4...
result:
wrong answer 15th numbers differ - expected: '4', found: '5'