Submission #591131

#TimeUsernameProblemLanguageResultExecution timeMemory
591131GusterGoose27The short shank; Redemption (BOI21_prison)C++11
80 / 100
2017 ms66888 KiB
#pragma GCC optimize("Ofast") #pragma GCC target("avx2") #include <bits/stdc++.h> using namespace std; typedef long double ld; typedef pair<ld, int> pdi; typedef pair<int, int> pii; const int MAXN = 2e6; pdi dp[MAXN]; const int inf = 1e9; int n, d, t; int vals[MAXN]; class STree { public: int n; pdi *mn; int *lz; int *lval; int *rval; STree() {} STree(int nn) { n = pow(2, ceil(log2(nn))); mn = new pdi[2*n]; lz = new int[2*n]; lval = new int[2*n]; rval = new int[2*n]; for (int i = 1; i < 2*n; i++) mn[i] = pdi(inf, 0); fill(lz, lz+2*n, 0); for (int i = n; i < 2*n; i++) { lval[i] = i-n; rval[i] = i-n+1; } for (int i = n-1; i > 0; i--) { lval[i] = lval[2*i]; rval[i] = rval[2*i+1]; } } void clean() { for (int i = 1; i < 2*n; i++) mn[i] = pdi(inf, 0); fill(lz, lz+2*n, 0); } void increment(int l, int r, int v, int cur = 1) { if (lval[cur] >= r || rval[cur] <= l) return; if (lval[cur] >= l && rval[cur] <= r) { lz[cur] += v; mn[cur].first += v; return; } increment(l, r, v, 2*cur); increment(l, r, v, 2*cur+1); mn[cur] = min(mn[2*cur], mn[2*cur+1]); mn[cur].first += lz[cur]; } pdi mnval(int l, int r, int cur = 1, int laz = 0) { if (lval[cur] >= r || rval[cur] <= l) return pdi(inf, 0); if (lval[cur] >= l && rval[cur] <= r) return pdi(mn[cur].first+laz, mn[cur].second); laz += lz[cur]; return min(mnval(l, r, 2*cur, laz), mnval(l, r, 2*cur+1, laz)); } void update(int i, pdi v, int cur = 1, int laz = 0) { if (lval[cur] > i || rval[cur] <= i) return; if (cur >= n) { mn[cur] = pdi(v.first-laz, v.second); return; } laz += lz[cur]; update(i, v, 2*cur, laz); update(i, v, 2*cur+1, laz); mn[cur] = min(mn[2*cur], mn[2*cur+1]); mn[cur].first += lz[cur]; } }; STree s; const int iters = 25; //const ld prec = 3e-2; int get_int(int i) { if (vals[i] > t) return i; return i+(t-vals[i])+1; } bool make_iter(ld &mn, ld &mx, bool force = 0) { vector<int> uncolored; //STree s(n); ld cost = (mn+mx)/2; for (int i = n-1; i >= 0; i--) { if (i == n-1) dp[i] = pdi(0, 0); else { pdi mnval = s.mnval(i+1, n); mnval.first += cost; mnval.second++; dp[i] = mnval; } // if (dp[i].second > d) { // mn = cost; // return 0; // } if (d == 1 && dp[i].second > d) s.update(i, pdi(inf, 0)); else s.update(i, dp[i]); uncolored.push_back(i); int r = get_int(i); int pos = uncolored.size()-1; while (pos >= 0 && uncolored[pos] < r) pos--; pos++; int prev = n; int mult = uncolored.size()-pos; for (int i = pos; i < uncolored.size(); i++) { s.increment(uncolored[i], prev, mult); prev = uncolored[i]; mult--; } int num = uncolored.size()-pos; for (int i = 0; i < num; i++) uncolored.pop_back(); } pdi overall = s.mnval(0, n); if (overall.second == d || force) { // we are done cout << round(overall.first-cost*d) << "\n"; return 1; } if (overall.second < d) mx = cost; else mn = cost; return 0; } void spec() { vector<int> uncolored; //STree s(n); //ld cost = (mn+mx)/2; for (int i = n-1; i >= 0; i--) { if (i == n-1) dp[i] = pdi(0, 0); else { pdi mnval = s.mnval(n-1, n); //mnval.first += cost; mnval.second++; dp[i] = mnval; } // if (dp[i].second > d) { // mn = cost; // return 0; // } if (d == 1 && dp[i].second > d) s.update(i, pdi(inf, 0)); else s.update(i, dp[i]); uncolored.push_back(i); int r = get_int(i); int pos = uncolored.size()-1; while (pos >= 0 && uncolored[pos] < r) pos--; pos++; int prev = n; int mult = uncolored.size()-pos; for (int i = pos; i < uncolored.size(); i++) { s.increment(uncolored[i], prev, mult); prev = uncolored[i]; mult--; } int num = uncolored.size()-pos; for (int i = 0; i < num; i++) uncolored.pop_back(); } pdi overall = s.mnval(0, n-1); cout << round(overall.first) << "\n"; return; // if (overall.second == d || force) { // we are done // cout << round(overall.first-cost*d) << "\n"; // //return 1; // } // if (overall.second < d) mx = cost; // else mn = cost; // return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> d >> t; for (int i = 0; i < n; i++) cin >> vals[i]; ld mn = 0; ld mx = ((ld)n)/d+1; s = STree(n); if (d == 1) { spec(); return 0; } for (int t = 0; t < iters; t++) { //while (mx-mn > prec) { bool fin = make_iter(mn, mx); if (fin) return 0; s.clean(); } //assert(false); make_iter(mn, mx, 1); }

Compilation message (stderr)

prison.cpp: In function 'bool make_iter(ld&, ld&, bool)':
prison.cpp:114:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |   for (int i = pos; i < uncolored.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~~~~
prison.cpp: In function 'void spec()':
prison.cpp:157:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  157 |   for (int i = pos; i < uncolored.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~~~~
#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...