aboutsummaryrefslogtreecommitdiff
path: root/store/works/life/chuanzhi-cup/final-contest/4.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'store/works/life/chuanzhi-cup/final-contest/4.cpp')
-rw-r--r--store/works/life/chuanzhi-cup/final-contest/4.cpp86
1 files changed, 86 insertions, 0 deletions
diff --git a/store/works/life/chuanzhi-cup/final-contest/4.cpp b/store/works/life/chuanzhi-cup/final-contest/4.cpp
new file mode 100644
index 0000000..19c66d3
--- /dev/null
+++ b/store/works/life/chuanzhi-cup/final-contest/4.cpp
@@ -0,0 +1,86 @@
+#include <iostream>
+#include <map>
+
+std::map<int, int> c[3];
+
+int main() {
+ std::ios_base::sync_with_stdio(false);
+ std::cin.tie(nullptr);
+
+ int n, m;
+
+ std::cin >> n >> m;
+
+ for (int i = 0; i < 3; i++) {
+ auto &cc = c[i];
+ for (int j = 0; j < n; j++) {
+ int k;
+ std::cin >> k;
+ cc[k]++;
+ }
+ }
+
+ int current = 0;
+ std::pair<int, int> last;
+ int last_put = 0;
+
+ while (true) {
+ auto &cc = c[current];
+
+ if (current == last_put) {
+ auto i = cc.begin();
+ last.first = i->first;
+ last.second = 1;
+ if (i->second == 1) {
+ cc.erase(i);
+ } else {
+ i->second--;
+ }
+ last_put = current;
+ } else {
+ bool can = false;
+
+ for (auto i = cc.upper_bound(last.first); i != cc.end(); ++i) {
+ if (i->second >= last.second) {
+ can = true;
+ i->second -= last.second;
+ last.first = i->first;
+ if (i->second == 0) {
+ cc.erase(i);
+ }
+ break;
+ }
+ }
+
+ if (!can) {
+ auto end = cc.upper_bound(last.first);
+ for (auto i = cc.begin(); i != end; ++i) {
+ if (i->second > last.second) {
+ can = true;
+ i->second -= last.second + 1;
+ last.first = i->first;
+ last.second++;
+ if (i->second == 0) {
+ cc.erase(i);
+ }
+ break;
+ }
+ }
+ }
+
+ if (can) {
+ last_put = current;
+ }
+ }
+
+ if (cc.empty()) {
+ std::cout << current + 1;
+ break;
+ }
+
+ current++;
+ current %= 3;
+ }
+
+ return 0;
+}