#include<bits/stdc++.h>
//https://oj.uz/problem/view/balkan11_mediansxx
using namespace std;
int main(){
ios::sync_with_stdio(false); cin.tie(0);
int n; cin>>n;
vector<int> b(n);
for(int i = 0; i < n; i++) cin>>b[i];
vector<int> suffMax(n), suffMin(n);
suffMax[n-1] = b[n-1];
suffMin[n-1] = b[n-1];
for(int i = n-2; i >= 0; i--){
suffMax[i] = max(suffMax[i+1], b[i]);
suffMin[i] = min(suffMin[i+1], b[i]);
}
vector<int> a(2*n-1);
vector<int> f(2*n);
a[0] = b[0];
set<int> s;
for(int i = 1; i <= 2*n-1; i++){
if(i == b[0]) continue;
s.insert(i);
}
for(int i = 1; i < n; i++){
if(b[i] > b[i-1]){
assert(s.size() > 0);
auto it = s.end(); it--;
if(f[b[i]] == 0){ // not inserted yet, insert now
f[b[i]] = 1;
f[*it] = 1;
a[i*2-1] = b[i];
a[i*2] = *it;
s.erase(it);
s.erase(b[i]);
}else{
f[*it] = 1;
a[i*2-1] = *it;
s.erase(it);
assert(s.size() > 0);
it = s.end(); it--;
f[*it] = 1;
a[i*2] = *it;
s.erase(it);
s.erase(b[i]);
}
}else if(b[i] < b[i-1]){
assert(s.size() > 0);
auto it = s.begin();
if(f[b[i]] == 0){ // not inserted yet, insert now
f[b[i]] = 1;
f[*it] = 1;
a[i*2-1] = b[i];
a[i*2] = *it;
s.erase(it);
}else{
f[*it] = 1;
a[i*2-1] = *it;
s.erase(it);
assert(s.size() > 0);
it = s.begin();
f[*it] = 1;
a[i*2] = *it;
s.erase(it);
}
}else if(b[i] == b[i-1]){
assert(s.size() > 0);
auto it = s.end(); it--;
f[*it] = 1;
a[i*2-1] = *it;
s.erase(it);
assert(s.size() > 0);
it = s.begin();
f[*it] = 1;
a[i*2] = *it;
s.erase(it);
}else{
assert(false);
}
}
for(int i = 0; i < a.size(); i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
Compilation message
medians.cpp: In function 'int main()':
medians.cpp:96:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < a.size(); i++){
~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Not a permutation |
2 |
Incorrect |
2 ms |
384 KB |
Not a permutation |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Incorrect |
2 ms |
384 KB |
Not a permutation |
5 |
Incorrect |
2 ms |
384 KB |
Not a permutation |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Incorrect |
2 ms |
384 KB |
Not a permutation |
9 |
Incorrect |
3 ms |
384 KB |
Not a permutation |
10 |
Incorrect |
2 ms |
384 KB |
Not a permutation |
11 |
Incorrect |
2 ms |
384 KB |
Not a permutation |
12 |
Incorrect |
2 ms |
384 KB |
Not a permutation |
13 |
Incorrect |
2 ms |
512 KB |
Not a permutation |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
640 KB |
Not a permutation |
2 |
Incorrect |
4 ms |
896 KB |
Not a permutation |
3 |
Incorrect |
7 ms |
1408 KB |
Not a permutation |
4 |
Incorrect |
13 ms |
2432 KB |
Not a permutation |
5 |
Incorrect |
28 ms |
4608 KB |
Not a permutation |
6 |
Incorrect |
54 ms |
8952 KB |
Not a permutation |
7 |
Incorrect |
196 ms |
13616 KB |
Not a permutation |