diff --git a/solved/LeetCode/Challenges/2020/January/10.cc b/solved/LeetCode/Challenges/2020/January/10.cc new file mode 100644 index 00000000..f6885556 --- /dev/null +++ b/solved/LeetCode/Challenges/2020/January/10.cc @@ -0,0 +1,71 @@ +#include + +using namespace std; + +#include "../../../template.cc" + +using int64 = long long int; + +// Single modification, range query. +struct SegTree { + int n; + vector t; + + SegTree(int N) : n(N), t(2 * N) {} + + void add(int p, int value) { + for (t[p += n] += value; p > 1; p >>= 1) { + t[p>>1] = t[p] + t[p^1]; + } + } + + // sum on interval [l, r) + int query(int l, int r) { + int res = 0; + for (l += n, r += n; l < r; l >>= 1, r >>= 1) { + if (l&1) res += t[l++]; + if (r&1) res += t[--r]; + } + return res; + } +}; + + +class Solution { + public: + int64 mod = 1e9 + 7; + int createSortedArray(vector& instructions) { + int mmax = (*max_element(instructions.begin(), instructions.end())) + 10; + SegTree tree(mmax + 1); + int ans = 0; + for (auto val: instructions) { + int cur = min(tree.query(0, val), tree.query(val + 1, mmax)); + ans = (ans + cur) % mod; + tree.add(val, 1); + } + return ans; + } + private: + +}; + +struct Test { + vector instructions; + int expected; +}; + +int main() { + vector test = { + { {1,5,6,2}, 1}, + { {1,2,3,6,5,4}, 3}, + { {1,3,3,3,2,4,2,1,2}, 4}, + { {1,2,1,2,1,2,1,2,1,2,1,2}, 0} + }; + for (auto t : test) { + Solution sol; + int actual = sol.createSortedArray(t.instructions); + debug(t.instructions, t.expected, actual); + assert(t.expected == actual); + } + return 0; +} diff --git a/solved/LeetCode/template.cc b/solved/LeetCode/template.cc new file mode 100644 index 00000000..1f0229a4 --- /dev/null +++ b/solved/LeetCode/template.cc @@ -0,0 +1,67 @@ +void print(std::ostream& out) { out << std::endl; } + +template +void printOne(std::ostream& out, const std::pair& a) { + out << "{" << a.first << ", " << a.second << "}"; +} + +template +void printOne(std::ostream& out, const T& a) { + out << a; +} + +template +void printOne(std::ostream& out, const std::vector& a) { + out << "{"; + for (int i = 0; i < a.size(); i++) { + if (i) out << ", "; + printOne(out, a[i]); + } + out << "}"; +} + +template +void printOne(std::ostream& out, const std::deque& a) { + out << "{"; + for (int i = 0; i < a.size(); i++) { + if (i) out << ", "; + printOne(out, a[i]); + } + out << "}"; +} + +template +void printOne(std::ostream& out, const std::set& a) { + out << "{"; + for (auto it : a) { + printOne(out, it); + out << ","; + } + out << "}"; +} + +template +void printOne(std::ostream& out, const std::map& a) { + out << "{"; + for (auto [key, val] : a) { + out << "["; printOne(out, key); out << " = "; printOne(out, val); out << "],"; + } + out << "}"; +} + + +template +void print(std::ostream& out, const T& first, const Tail... tail) { + printOne(out, first); + if (sizeof...(tail) != 0) { + out << ", "; + } + print(out, tail...); +} + +#ifdef LOCAL_CP +#define debug(...) \ + std::cerr << "[DEBUG] [" << #__VA_ARGS__ << "] = ", print(std::cerr, __VA_ARGS__) +#else +#define debug(...) 0 +#endif