Submission #161706

#TimeUsernameProblemLanguageResultExecution timeMemory
161706Anaimedians (balkan11_medians)C++14
100 / 100
108 ms12056 KiB
#include <bits/stdc++.h>
using namespace std;

const int M = 2e5 + 5;

int f[M];

vector<int> v, ant;
set<int> s;
int n, m;


int main() {
#ifdef HOME
	freopen("medians.in", "r", stdin);
	freopen("medians.out", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int lst;

	cin >> n;
	v.resize(n);
	for (auto &i: v) {
		cin >> i;
		f[i]+= 1; }
	m = 2 * n - 1;

	for (int i = 1; i <= m; ++i)
		if (!f[i])
			s.insert(i);

	lst = v.back();
	if (!--f[lst])
		s.insert(lst);

	for (int i = n - 2; i >= 0; --i) {
		if (v[i] == lst) {
			auto l = prev(s.lower_bound(v[i]));
			auto r = s.lower_bound(v[i]);
			ant.push_back(*l), ant.push_back(*r);
			s.erase(l);
			s.erase(r); }
		else if (v[i] > lst) {
			auto l = prev(s.upper_bound(v[i]));
			auto r = prev(l);
			ant.push_back(*l), ant.push_back(*r);
			s.erase(l);
			s.erase(r); }
		else {
			auto l = s.lower_bound(v[i]);
			auto r = next(l);
			ant.push_back(*l), ant.push_back(*r);
			s.erase(l);
			s.erase(r); }
		lst = v[i];
		if (--f[v[i]] == 0)
			s.insert(v[i]); }

	ant.push_back(*begin(s));

	reverse(begin(ant), end(ant));
	for (auto i: ant)
		cout << i << ' ';
	cout << endl;

	return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...