-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
#include "../../../template.cc" | ||
|
||
using int64 = long long int; | ||
|
||
// Single modification, range query. | ||
struct SegTree { | ||
int n; | ||
vector<int> 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<int>& 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<int> instructions; | ||
int expected; | ||
}; | ||
|
||
int main() { | ||
vector<Test> 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
void print(std::ostream& out) { out << std::endl; } | ||
|
||
template <typename T, typename T2> | ||
void printOne(std::ostream& out, const std::pair<T, T2>& a) { | ||
out << "{" << a.first << ", " << a.second << "}"; | ||
} | ||
|
||
template <typename T> | ||
void printOne(std::ostream& out, const T& a) { | ||
out << a; | ||
} | ||
|
||
template <typename T> | ||
void printOne(std::ostream& out, const std::vector<T>& a) { | ||
out << "{"; | ||
for (int i = 0; i < a.size(); i++) { | ||
if (i) out << ", "; | ||
printOne(out, a[i]); | ||
} | ||
out << "}"; | ||
} | ||
|
||
template <typename T> | ||
void printOne(std::ostream& out, const std::deque<T>& a) { | ||
out << "{"; | ||
for (int i = 0; i < a.size(); i++) { | ||
if (i) out << ", "; | ||
printOne(out, a[i]); | ||
} | ||
out << "}"; | ||
} | ||
|
||
template <typename T> | ||
void printOne(std::ostream& out, const std::set<T>& a) { | ||
out << "{"; | ||
for (auto it : a) { | ||
printOne(out, it); | ||
out << ","; | ||
} | ||
out << "}"; | ||
} | ||
|
||
template <typename T, typename V> | ||
void printOne(std::ostream& out, const std::map<T, V>& a) { | ||
out << "{"; | ||
for (auto [key, val] : a) { | ||
out << "["; printOne(out, key); out << " = "; printOne(out, val); out << "],"; | ||
} | ||
out << "}"; | ||
} | ||
|
||
|
||
template <typename T, typename... Tail> | ||
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 |