#include <bits/stdc++.h>
#define NMAX 100005
using namespace std;
int n, v[NMAX], lst[NMAX], fr[2*NMAX], med, ans[2*NMAX], N;
int sL[8*NMAX], sR[8*NMAX], LL[8*NMAX], LR[8*NMAX];
set<int> S;
vector<int> mn[2*NMAX];
void propag(int s[], int lz[], int nod){
s[nod * 2] += lz[nod];
s[nod * 2 + 1] += lz[nod];
lz[nod * 2] += lz[nod];
lz[nod * 2 + 1] += lz[nod];
lz[nod] = 0;
}
void add(int s[], int lz[], int l, int r, int val, int st = 1, int dr = N, int nod = 1){
if (l <= st && dr <= r){
lz[nod] += val;
s[nod] += val;
return;
}
propag(s, lz, nod);
int mid = (st + dr) / 2;
if (l<=mid) add(s, lz, l, r, val, st, mid, nod*2);
if (mid+1<=r) add(s, lz, l, r, val, mid+1, dr, nod*2+1);
s[nod] = min(s[nod*2], s[nod*2+1]);
}
int queryL(int st = 1, int dr = N, int nod = 1){
if (st == dr){
if (sL[nod] > 0) return st;
return st + 1;
}
propag(sL, LL, nod);
int mid = (st + dr) / 2;
if (sL[nod*2+1] > 0)
return queryL(st, mid, nod * 2);
return queryL(mid+1, dr, nod * 2 + 1);
}
int queryR(int st = 1, int dr = N, int nod = 1){
if (st == dr){
if (sR[nod] > 0) return st;
return st - 1;
}
propag(sR, LR, nod);
int mid = (st + dr) / 2;
if (sR[nod*2] > 0)
return queryR(mid+1, dr, nod * 2 + 1);
return queryR(st, mid, nod * 2);
}
int popLeft(){
int x = queryL();
x = *S.lower_bound(x);
S.erase(x);
add(sL, LL, x, 2 * n - 1, -1);
add(sR, LR, 1, x, -1);
return x;
}
int popRight(){
int x = queryR();
x = *prev(S.upper_bound(x));
S.erase(x);
add(sL, LL, x, 2 * n - 1, -1);
add(sR, LR, 1, x, -1);
return x;
}
void readAndPre(){
cin >> n;
for (int i=1;i<=2*n-1;i++){
if (fr[i] == 0) S.insert(i);
mn[i].push_back(0);
}
for (int i=1;i<=n;i++){
cin >> v[i];
S.erase(v[i]);
fr[v[i]]++;
if (i!=n) mn[v[i]].push_back(i);
}
for (int i=n;i>=1;i--){
fr[v[i]]--;
if (fr[v[i]] == 0){
lst[i] = 1;
}
}
med = n;
N = 1;
while (N < 2*n-1) N *= 2;
N = 2*n-1;
for (int i=1;i<=2*n-1;i++){
add(sL, LL, i, i, i-mn[i].back());
add(sR, LR, i, i, 2*n-i-mn[i].back());
}
for (int i=2*n;i<=N;i++){
add(sL, LL, i, i, 1e9);
add(sR, LR, i, i, 1e9);
}
}
int main()
{
readAndPre();
if (lst[n]) S.insert(v[n]);
for (int i=n-1;i>=1;i--){
int a,b;
if (v[i] == v[i+1]){
a = popLeft();
b = popRight();
}
else if (v[i] < v[i+1]){
a = popRight();
b = popRight();
}
else{
a = popLeft();
b = popLeft();
}
ans[2*i+1] = a;
ans[2*i] = b;
if (lst[i])
S.insert(v[i]);
a = mn[v[i]].back();
b = mn[v[i]][mn[v[i]].size() - 2];
add(sL, LL, v[i], v[i], a - b);
add(sR, LR, v[i], v[i], a - b);
mn[v[i]].pop_back();
}
ans[1] = v[1];
for (int i=1;i<=2*n-1;i++) cout << ans[i] << ' ';
return 0;
}
/*
5
1 5 5 5 5
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
3 ms |
5120 KB |
Output is correct |
5 |
Correct |
3 ms |
5120 KB |
Output is correct |
6 |
Correct |
4 ms |
5120 KB |
Output is correct |
7 |
Correct |
3 ms |
5120 KB |
Output is correct |
8 |
Correct |
3 ms |
5120 KB |
Output is correct |
9 |
Correct |
3 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
6 ms |
5248 KB |
Output is correct |
13 |
Correct |
7 ms |
5376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
5632 KB |
Output is correct |
2 |
Correct |
20 ms |
6144 KB |
Output is correct |
3 |
Correct |
40 ms |
7192 KB |
Output is correct |
4 |
Correct |
82 ms |
9328 KB |
Output is correct |
5 |
Correct |
168 ms |
13416 KB |
Output is correct |
6 |
Execution timed out |
350 ms |
22008 KB |
Time limit exceeded |
7 |
Execution timed out |
574 ms |
33056 KB |
Time limit exceeded |