#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
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);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
3220 KB |
Output is correct |
2 |
Correct |
0 ms |
3220 KB |
Output is correct |
3 |
Correct |
0 ms |
3220 KB |
Output is correct |
4 |
Correct |
0 ms |
3220 KB |
Output is correct |
5 |
Correct |
0 ms |
3220 KB |
Output is correct |
6 |
Correct |
0 ms |
3220 KB |
Output is correct |
7 |
Correct |
0 ms |
3220 KB |
Output is correct |
8 |
Correct |
0 ms |
3220 KB |
Output is correct |
9 |
Correct |
0 ms |
3220 KB |
Output is correct |
10 |
Correct |
0 ms |
3220 KB |
Output is correct |
11 |
Correct |
0 ms |
3220 KB |
Output is correct |
12 |
Correct |
0 ms |
3352 KB |
Output is correct |
13 |
Correct |
0 ms |
3352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3484 KB |
Output is correct |
2 |
Correct |
3 ms |
3616 KB |
Output is correct |
3 |
Correct |
13 ms |
4012 KB |
Output is correct |
4 |
Correct |
23 ms |
4672 KB |
Output is correct |
5 |
Correct |
53 ms |
6256 KB |
Output is correct |
6 |
Correct |
109 ms |
9292 KB |
Output is correct |
7 |
Correct |
173 ms |
12592 KB |
Output is correct |