Submission #397799

#TimeUsernameProblemLanguageResultExecution timeMemory
397799yuto1115Painting Walls (APIO20_paint)C++17
100 / 100
1479 ms406468 KiB
#include "paint.h" #include <bits/stdc++.h> #define rep(i, n) for(ll i = 0; i < ll(n); i++) #define rep2(i, s, n) for(ll i = ll(s); i < ll(n); i++) #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define pb push_back #define eb emplace_back using namespace std; using ll = long long; using P = pair<int, int>; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; using vp = vector<P>; using vvp = vector<vp>; using vb = vector<bool>; using vvb = vector<vb>; template<class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } const int inf = 1001001001; //class RMQ { // int n; // vi val; //public: // RMQ(int _n) { // n = 1; // while (n < _n) n *= 2; // val.assign(n * 2, 0); // } // // void update(int i, int x) { // assert(0 <= i and i < n); // i += n; // val[i] += x; // while (i > 1) { // i /= 2; // val[i] = max(val[i * 2], val[i * 2 + 1]); // } // } // // int fold(int l, int r) { // assert(0 <= l and l <= r and r <= n); // l += n, r += n; // int fl = 0, fr = 0; // while (l < r) { // if (l & 1) fl = max(fl, val[l++]); // if (r & 1) fr = max(val[--r], fr); // l >>= 1, r >>= 1; // } // return max(fl, fr); // } //}; int minimumInstructions(int n, int m, int k, vi c, vi a, vvi b) { vvi f(k); rep(i, m) for (int j : b[i]) f[j].pb(i); vvi match(n); rep(i, n) for (int j : f[c[i]]) match[i].pb(j = ((j - i) % m + m) % m); vi v(m); int cnt = 0; rep(i, m - 1) for (int j : match[i]) if (++v[j] == m) cnt++; vi ok; rep(i, n - m + 1) { for (int j : match[i + m - 1]) if (++v[j] == m) cnt++; if (cnt) ok.pb(i); for (int j : match[i]) if (v[j]-- == m) cnt--; } int now = -1, ans = 0; while (now < n - 1) { auto it = upper_bound(all(ok), now + 1); if (it == ok.begin()) return -1; it--; if (*it + m - 1 <= now) return -1; now = *it + m - 1; ans++; } 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...