This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <set>
using namespace std;
int n, nn, tree[524288];
set<int> S;
void Update(int lev) {
tree[lev] = tree[lev * 2] + tree[lev * 2 + 1];
if (lev > 1) Update(lev / 2);
}
void Push(int x) {
S.erase(x), printf("%d ", x);
tree[nn + x - 1] = 1, Update((nn + x - 1) / 2);
}
void P_left() {
set<int>::iterator it = S.begin();
Push(*it);
}
void P_right() {
set<int>::iterator it = S.end();
it--, Push(*it);
}
int f(int lev, int l, int r, int x, int y) {
int mid = (l + r) / 2;
if (x <= l && r <= y) return tree[lev];
if (r < x || y < l) return 0;
return f(lev * 2, l, mid, x, y) + f(lev * 2 + 1, mid + 1, r, x, y);
}
int main() {
int x;
scanf("%d", &n);
for (nn = 1; nn < 2 * n - 1; nn *= 2);
for (int i = 1; i < 2 * n; i++) S.insert(i);
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
if (i == 1) {
Push(x);
continue;
}
int left = f(1, 1, nn, 1, x), right = f(1, 1, nn, x, nn);
if (!tree[nn + x - 1]) {
Push(x);
if (left > right) P_right();
else P_left();
}
else if (left > right) P_right(), P_right();
else if (left == right) P_left(), P_right();
else P_left(), P_left();
}
}
Compilation message (stderr)
medians.cpp: In function 'int main()':
medians.cpp:34:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
^
medians.cpp:39:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &x);
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |