Submission #119943

#TimeUsernameProblemLanguageResultExecution timeMemory
119943tutismedians (balkan11_medians)C++17
100 / 100
104 ms12240 KiB
/*input 5 1 3 3 4 5 */ #pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; int m[n]; for (int i = 0; i < n; i++) cin >> m[i]; set<int>nebus; for (int i = 1; i <= 2 * n - 1; i++) nebus.insert(i); for (int i = 0; i < n; i++) nebus.erase(m[i]); deque<int>answer; int p[2 * n]; fill_n(p, 2 * n, n + 10); for (int i = 0; i < n; i++) p[m[i]] = min(p[m[i]], i); for (int i = n - 1; i > 0; i--) { if (p[m[i]] == i) nebus.insert(m[i]); if (m[i] == m[i - 1]) { auto it = nebus.lower_bound(m[i]); assert(it != nebus.end()); answer.push_front(*it); nebus.erase(it); it = nebus.upper_bound(m[i]); assert(it != nebus.begin()); it--; answer.push_front(*it); nebus.erase(it); } else { if (m[i] > m[i - 1]) { auto it = nebus.lower_bound(m[i]); assert(it != nebus.end()); answer.push_front(*it); nebus.erase(it); it = nebus.lower_bound(m[i]); assert(it != nebus.end()); answer.push_front(*it); nebus.erase(it); } else { auto it = nebus.upper_bound(m[i]); assert(it != nebus.begin()); it--; answer.push_front(*it); nebus.erase(it); it = nebus.upper_bound(m[i]); assert(it != nebus.begin()); it--; answer.push_front(*it); nebus.erase(it); } } } nebus.insert(m[0]); assert(nebus.size() == 1); answer.push_front(*nebus.begin()); for (int i : answer) cout << i << " "; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...