Submission #168793

#TimeUsernameProblemLanguageResultExecution timeMemory
168793popovicirobertShortcut (IOI16_shortcut)C++14
100 / 100
1841 ms35724 KiB
#include "shortcut.h" #include <bits/stdc++.h> #define ll long long using namespace std; const int MAXN = (int) 1e6; ll x[MAXN + 1], d[MAXN + 1]; int deq[MAXN + 1]; inline bool check(ll k, int n, int c) { vector <int> sgn = {1, -1}; int pos[2][2]; ll vals[2][2]; memset(pos, 0, sizeof(pos)); for(int conf = 0; conf < 2; conf++) { vals[conf][0] = -1e18; vals[conf][1] = 1e18; } auto upd = [&](int p, int conf) { int b = (conf >> 1) % 2, c = conf % 2; ll v = x[p] + d[p] * sgn[b]; if(c == 0) { // mx if(vals[b][c] < v) { pos[b][c] = p; vals[b][c] = v; } } else { // mn if(vals[b][c] > v) { pos[b][c] = p; vals[b][c] = v; } } }; int i = 0; ll A = -1e18, B = 1e18, a = -1e18, b = 1e18; auto get = [&](int b, int c, int p) { return vals[b][c]; }; int beg = 0, end = -1; for(int j = 1; j <= n; j++) { while(beg <= end && x[j] - x[deq[beg]] + d[deq[beg]] + d[j] > k) { upd(deq[beg], 3); upd(deq[beg], 0); beg++; } B = min(B, (k - c) + x[j] - d[j] + get(1, 1, j)); A = max(A, x[j] + d[j] - (k - c) + get(0, 0, j)); b = min(b, (k - c) + x[j] - d[j] - get(0, 0, j)); a = max(a, x[j] + d[j] - (k - c) - get(1, 1, j)); while(end >= beg && x[deq[end]] - d[deq[end]] > x[j] - d[j]) { end--; } deq[++end] = j; } //cerr << k << " " << a << " " << b << " " << A << " " << B << "\n"; // A <= y + z <= B // a <= z - y <= b if(A > B || a > b) { return 0; } int pa = 1, pb = 1; int pc = n, pd = n; for(i = 1; i <= n; i++) { while(pd > 0 && x[i] + x[pd] > B) { pd--; } pc = min(pc, n); while(pc > 0 && x[i] + x[pc] >= A) { pc--; } pc++; pa = max(pa, 1); while(pa <= n && x[i] - x[pa] >= a) { pa++; } pa--; while(pb <= n && x[i] - x[pb] > b) { pb++; } if(max(pc, pb) <= min(pa, pd) && max(pc, pb) <= i) { return 1; } } return 0; } long long find_shortcut(int n, vector<int> l, vector<int> D, int c) { int i; for(i = 2; i <= n; i++) { x[i] = x[i - 1] + l[i - 2]; } for(i = n; i >= 1; i--) { d[i] = D[i - 1]; } ll res = 0; for(ll step = 1LL << 50; step; step >>= 1) { if(check(res + step, n, c) == 0) { res += step; } } return res + 1; }
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...