Submission #1032216

#TimeUsernameProblemLanguageResultExecution timeMemory
1032216shmaxArranging Shoes (IOI19_shoes)C++17
45 / 100
44 ms6744 KiB
#include "shoes.h" #include <bits/stdc++.h> using namespace std; using i32 = int; #define int long long #define len(x) (int)(x.size()) #define inf 1000'000'000'000'000'000LL #define all(x) x.begin(), x.end() #define low_bit(x) (x & (-x)) template<typename T> using vec = vector<T>; template<typename T> struct fenvik { private: vec<T> fen; T _get(int x) { T res = 0; for (int i = x; i > 0; i -= low_bit(i)) { res += fen[i]; } return res; } void _add(int x, T val) { for (int i = x; i < len(fen); i += low_bit(i)) { fen[i] += val; } } public: void init(int x) { fen.clear(); fen.resize(x + 1); } void inc(int i, T x) { i++; _add(i, x); } T get(int l, int r) { if (l == 0) return _get(r + 1); return _get(r + 1) - _get(l); } T get(int i) { return get(i, i); } void set(int i, T x) { inc(i, x - get(i)); } }; int count_swaps(vec<i32> s) { int n = len(s); deque<int> p1, p0; for (int i = 0; i < n; i++) { if (s[i] > 0) p1.push_back(i); else p0.push_back(i); } fenvik<int> exist; exist.init(n); for (int i = 0; i < n; i++) { exist.set(i, 1); } int last = 1; int ans = 0; for (int i = 0; i < n; i++) { if (exist.get(i, i) == 0) continue; if (s[i] == -last) { last *= -1; if (s[i] == 1) { p1.pop_front(); } if (s[i] == -1) { p0.pop_front(); } continue; } if (last == 1) { int p = p0.front(); p0.pop_front(); exist.set(p, 0); ans += exist.get(i, p); } else { int p = p1.front(); p1.pop_front(); exist.set(p, 0); ans += exist.get(i, p); } i--; last *= -1; } return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...