Submission #37323

#TimeUsernameProblemLanguageResultExecution timeMemory
3732314kgmedians (balkan11_medians)C++11
100 / 100
173 ms12592 KiB
#include <stdio.h> #include <set> using namespace std; int n, nn, tree[524288]; set<int> S; void Update(int lev) { tree[lev] = tree[lev * 2] + tree[lev * 2 + 1]; if (lev > 1) Update(lev / 2); } void Push(int x) { S.erase(x), printf("%d ", x); tree[nn + x - 1] = 1, Update((nn + x - 1) / 2); } void P_left() { set<int>::iterator it = S.begin(); Push(*it); } void P_right() { set<int>::iterator it = S.end(); it--, Push(*it); } int f(int lev, int l, int r, int x, int y) { int mid = (l + r) / 2; if (x <= l && r <= y) return tree[lev]; if (r < x || y < l) return 0; return f(lev * 2, l, mid, x, y) + f(lev * 2 + 1, mid + 1, r, x, y); } int main() { int x; scanf("%d", &n); for (nn = 1; nn < 2 * n - 1; nn *= 2); for (int i = 1; i < 2 * n; i++) S.insert(i); for (int i = 1; i <= n; i++) { scanf("%d", &x); if (i == 1) { Push(x); continue; } int left = f(1, 1, nn, 1, x), right = f(1, 1, nn, x, nn); if (!tree[nn + x - 1]) { Push(x); if (left > right) P_right(); else P_left(); } else if (left > right) P_right(), P_right(); else if (left == right) P_left(), P_right(); else P_left(), P_left(); } }

Compilation message (stderr)

medians.cpp: In function 'int main()':
medians.cpp:34:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
                 ^
medians.cpp:39:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &x);
                  ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...