#include <bits/stdc++.h>
using namespace std;
#define MAX 602020
int l[MAX], r[MAX];
int tree[MAX];
int arr[MAX];
int s;
vector<int>ans;
void init(int x = 1) {
if (x >= s) {
l[x] = r[x] = x - s + 1;
return;
}
init(x * 2);
init(x * 2 + 1);
l[x] = l[x * 2];
r[x] = r[x * 2 + 1];
}
void update(int loc) {
if (loc == 0) return;
tree[loc]++;
update(loc / 2);
}
int sumquery(int low, int high, int loc = 1) {
if (l[loc] == low && r[loc] == high) return tree[loc];
else if (r[loc * 2] >= high) return sumquery(low, high, loc * 2);
else if (l[loc * 2 + 1] <= low) return sumquery(low, high, loc * 2 + 1);
else return sumquery(low, r[loc * 2], loc * 2) + sumquery(l[loc * 2 + 1], high, loc * 2 + 1);
}
int main(void) {
int N;
cin >> N;
s = 1 << (int)ceil(log2(2 * N - 1));
int i;
init();
int a;
cin >> a;
ans.push_back(a);
update(a + s - 1);
arr[a] = 1;
int p, q;
p = 1;
q = 2 * N - 1;
for (i = 1; i <= N - 1; i++) {
cin >> a;
if (arr[a] == 0) {
ans.push_back(a);
update(a);
arr[a] = 1;
if (sumquery(1, a) < sumquery(a, 2 * N - 1)) {
while (arr[p] == 1) p++;
ans.push_back(p);
update(p + s - 1);
arr[p] = 1;
}
else {
while (arr[q] == 1) q--;
ans.push_back(q);
update(q + s - 1);
arr[q] = 1;
}
}
else {
int r1, r2;
r1 = sumquery(1, a);
r2 = sumquery(a, 2 * N - 1);
if (r1 < r2) {
while (arr[p] == 1) p++;
ans.push_back(p);
update(p + s - 1);
arr[p] = 1;
while (arr[p] == 1) p++;
ans.push_back(p);
update(p + s - 1);
arr[p] = 1;
}
else if (r1 > r2) {
while (arr[q] == 1) q--;
ans.push_back(q);
update(q + s - 1);
arr[q] = 1;
while (arr[q] == 1) q--;
ans.push_back(q);
update(q + s - 1);
arr[q] = 1;
}
else {
while (arr[p] == 1) p++;
ans.push_back(p);
update(p + s - 1);
arr[p] = 1;
while (arr[q] == 1) q--;
ans.push_back(q);
update(q + s - 1);
arr[q] = 1;
}
}
}
for (auto i : ans) cout << i << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
9 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
11 |
Incorrect |
2 ms |
364 KB |
Output isn't correct |
12 |
Incorrect |
3 ms |
364 KB |
Output isn't correct |
13 |
Incorrect |
5 ms |
364 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
492 KB |
Output isn't correct |
2 |
Incorrect |
20 ms |
620 KB |
Output isn't correct |
3 |
Incorrect |
46 ms |
1004 KB |
Output isn't correct |
4 |
Incorrect |
83 ms |
1644 KB |
Output isn't correct |
5 |
Incorrect |
161 ms |
2920 KB |
Output isn't correct |
6 |
Execution timed out |
328 ms |
5348 KB |
Time limit exceeded |
7 |
Execution timed out |
545 ms |
9184 KB |
Time limit exceeded |