Submission #526148

#TimeUsernameProblemLanguageResultExecution timeMemory
526148PurpleCrayonComparing Plants (IOI20_plants)C++17
25 / 100
4082 ms184584 KiB
#include "plants.h" #include <bits/stdc++.h> using namespace std; #define sz(v) int(v.size()) const int INF = 1e9+10; int n, k; vector<int> a, b; vector<int> nxt, prv; void init(int K, vector<int> r) { n = sz(r), k = K; vector<bool> done(n); a.resize(n); nxt.assign(2*n, -1), prv.assign(2*n, -1); set<int> cand; set<int> bad_cand; auto prv_cand = [&](int x, bool which) { auto& use = (which ? cand : bad_cand); if (!sz(use)) return INF; auto it = use.lower_bound(x); if (it != use.begin()) return *prev(it); return *use.rbegin(); }; auto nxt_cand = [&](int x, bool which) { auto& use = (which ? cand : bad_cand); if (!sz(use)) return INF; auto it = use.upper_bound(x); if (it == use.end()) return *use.begin(); return *it; }; auto forward_dist = [&](int a, int b) { if (a == INF || b == INF) return INF; if (a < b) return b - a; return n - a + b; }; vector<bool> has_add(n); for (int i = 0; i < n; i++) if (!r[i]) cand.insert(i), has_add[i] = 1; vector<vector<int>> stop(n); for (int i = 0; i < n; i++) if (!r[i]) { int p = prv_cand(i, true); if (forward_dist(p, i) < k) { stop[p].push_back(i); bad_cand.insert(i); } } for (int x : bad_cand) cand.erase(x); auto prv_cand_both = [&](int x) { int one = prv_cand(x, true), two = prv_cand(x, false); if (forward_dist(one, x) < forward_dist(two, x)) return one; return two; }; auto nxt_cand_both = [&](int x) { int one = nxt_cand(x, true), two = nxt_cand(x, false); if (forward_dist(x, one) < forward_dist(x, two)) return one; return two; }; auto add_cand = [&](int x) { // cerr << "add_cand(" << x << ")\n"; int p = prv_cand_both(x); if (p != INF && forward_dist(p, x) < k) { stop[p].push_back(x); cand.erase(x); bad_cand.insert(x); } else { bad_cand.erase(x); cand.insert(x); } int q = nxt_cand_both(x); if (q != INF && forward_dist(x, q) < k) { stop[x].push_back(q); cand.erase(q); bad_cand.insert(q); } // cerr << "AA: "; // for (int x : cand) cerr << x << ' '; // cerr << endl; // cerr << "BB: "; // for (int x : bad_cand) cerr << x << ' '; // cerr << endl; }; for (int rep = 0; rep < n; rep++) { int me = *cand.begin(); // cerr << "> " << me << endl; a[me] = n - 1 - rep; done[me] = 1; cand.erase(me); for (int x : stop[me]) if (!done[x]) { add_cand(x); } for (int i = me - k + 1; i < me; i++) { // range add, range min int c = (i + n) % n; if (!has_add[c]) { r[c]--; if (r[c] == 0) { add_cand(c); has_add[c] = 1; } } } // cerr << "A: "; // for (int x : cand) cerr << x << ' '; // cerr << endl; // cerr << "B: "; // for (int x : bad_cand) cerr << x << ' '; // cerr << endl; } for (int rep : {0, 1}) for (int x : a) b.push_back(x); assert(sz(b) == 2 * n); for (int i = 0; i < 2*n; i++) { for (int j = i+1; j < min(2*n, i+k); j++) { // range min if (b[j] > b[i] && (nxt[i] == -1 || b[nxt[i]] > b[j])) nxt[i] = j; } for (int j = max(0, i-k+1); j < i; j++) { // range min if (b[j] > b[i] && (prv[i] == -1 || b[prv[i]] > b[j])) prv[i] = j; } } } bool can_nxt(int x, int y) { int c = x; int need = y < x ? y + n : y; while (nxt[c] != -1 && nxt[c] <= need) { assert(b[nxt[c]] > b[c]); c = nxt[c]; } assert(b[need] == a[y]); return need - c < k && b[need] >= b[c]; } bool can_prv(int x, int y) { int c = x > y ? x : x + n; int need = y; while (prv[c] != -1 && prv[c] >= need) { assert(b[prv[c]] > b[c]); c = prv[c]; } assert(b[need] == a[y]); return c - need < k && b[need] >= b[c]; } bool can_reach(int x, int y) { return can_nxt(x, y) || can_prv(x, y); } int compare_plants(int x, int y) { if (can_reach(x, y)) { // y is greater than x assert(a[y] > a[x]); return -1; } if (can_reach(y, x)) { // x is greater than y assert(a[x] > a[y]); return 1; } return 0; }

Compilation message (stderr)

plants.cpp: In function 'void init(int, std::vector<int>)':
plants.cpp:124:14: warning: unused variable 'rep' [-Wunused-variable]
  124 |     for (int rep : {0, 1}) for (int x : a) b.push_back(x);
      |              ^~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...