제출 #494632

#제출 시각아이디문제언어결과실행 시간메모리
494632aryan12중앙값 배열 (balkan11_medians)C++17
5 / 100
63 ms27356 KiB
#include <bits/stdc++.h> using namespace std; #define int long long mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count()); const int N = 2e5 + 5; int seg[N * 4]; void Upd(int l, int r, int pos, int qpos) { if(l == r) { seg[pos]++; return; } int mid = (l + r) >> 1; if(qpos <= mid) Upd(l, mid, pos * 2, qpos); else Upd(mid + 1, r, pos * 2 + 1, qpos); seg[pos] = seg[pos * 2] + seg[pos * 2 + 1]; } int Query(int l, int r, int pos, int ql, int qr) { if(ql <= l && r <= qr) { return seg[pos]; } if(ql > r || l > qr) { return 0; } int mid = (l + r) >> 1; return Query(l, mid, pos * 2, ql, qr) + Query(mid + 1, r, pos * 2 + 1, ql, qr); } void Solve() { int n; cin >> n; int b[n + 1]; for(int i = 1; i <= n; i++) { cin >> b[i]; } int a[n * 2 + 1]; set<int> s; for(int i = 1; i < 2 * n; i++) { s.insert(i); } for(int i = 1; i < n * 2; i++) { a[i] = 0; } vector<int> ans; ans.push_back(b[1]); a[b[1]] = 1; s.erase(b[1]); Upd(1, 2 * n - 1, 1, b[1]); for(int i = 2; i <= n; i++) { if(a[b[i]] == 1) { int ans1 = Query(1, 2 * n - 1, 1, 1, b[i] - 1), ans2 = Query(1, 2 * n - 1, 1, b[i] + 1, 2 * n - 1); assert(abs(ans1 - ans2) == 2 || ans1 == ans2); while(ans1 <= ans2) { auto f = s.begin(); s.erase(*f); ans.push_back(*f); a[*f] = 1; Upd(1, 2 * n - 1, 1, *f); ans1++; } while(ans2 < ans1) { auto f = --s.end(); s.erase(*f); ans.push_back(*f); a[*f] = 1; Upd(1, 2 * n - 1, 1, *f); ans2++; } } else { ans.push_back(b[i]); a[b[i]] = 1; s.erase(b[i]); Upd(1, 2 * n - 1, 1, b[i]); int ans1 = Query(1, 2 * n - 1, 1, 1, b[i] - 1), ans2 = Query(1, 2 * n - 1, 1, b[i] + 1, 2 * n - 1); assert(abs(ans1 - ans2) == 1); if(ans1 < ans2) { auto f = s.begin(); s.erase(*f); ans.push_back(*f); a[*f] = 1; Upd(1, 2 * n - 1, 1, *f); } else { auto f = --s.end(); s.erase(*f); ans.push_back(*f); a[*f] = 1; Upd(1, 2 * n - 1, 1, *f); } } } for(int i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } cout << "\n"; } int32_t main() { auto begin = std::chrono::high_resolution_clock::now(); ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; //cin >> t; while(t--) { Solve(); } auto end = std::chrono::high_resolution_clock::now(); auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin); cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; return 0; }

컴파일 시 표준 에러 (stderr) 메시지

medians.cpp: In function 'void Solve()':
medians.cpp:96:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |  for(int i = 0; i < ans.size(); i++) {
      |                 ~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...