/*
ID: varunra2
LANG: C++
TASK: shortcut
*/
#include <bits/stdc++.h>
#include "shortcut.h"
using namespace std;
#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif
#define int long long
#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e16
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second
const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions
VI l;
VI x;
VI d;
VII use;
int n;
int c;
void cop(VI& a, vector<int32_t> b) {
a.resize(sz(b));
for (int i = 0; i < sz(a); i++) {
a[i] = b[i];
}
}
void genPref() {
x.resize(n);
x[0] = 0ll;
for (int i = 1; i < n; i++) {
x[i] = x[i - 1] + l[i - 1];
}
}
// void genDom() {
// // here we need to generate statinos that are not dominated by anybody
// // then x[dom[i]] + d[dom[i]] is non decreasing
// dom.assign(n, true);
// // to do this, we need to generate prefix and suffix maxima of stuff lol
// int mx;
// mx = -INF;
// for (int i = 0; i < n; i++) {
// int cur = d[i] + x[i];
// mx = max(mx, cur);
// if (cur < mx) dom[i] = false;
// }
// mx = -INF;
// for (int i = n - 1; i >= 0; i--) {
// int cur = d[i] - x[i];
// mx = max(mx, cur);
// if (cur < mx) dom[i] = false;
// }
// for (int i = 0; i < n; i++) {
// if (dom[i]) doms.PB(i);
// }
// assert(!doms.empty());
// }
// void genOrd() {
// // we also need to generate a sorted vector of pairs that contains when the
// // ith element is introduced, and its index
// ord.resize(n);
// for (int i = 0; i < n; i++) {
// ord[i] = MP(x[i] - d[i], i);
// }
// sort(all(ord));
// }
int mnsum, mxsum, mndif, mxdif;
void upd(int i) {
int x = ::x[i], d = ::d[i];
int sum = x + d, dif = x - d;
mnsum = min(mnsum, sum);
mxsum = max(mxsum, sum);
mndif = min(mndif, dif);
mxdif = max(mxdif, dif);
}
const int bad = 92;
void initsumdif() {
mnsum = INF;
mxsum = -INF;
mndif = INF;
mxdif = -INF;
}
// bool domin(int i, int j) {
// // does i dominate j?
// // return true;
// if (i == j) return false;
// return d[i] > d[j] + abs(x[i] - x[j]);
// }
int K;
bool optimize(int i, int j) {
// do we need to optimize pair i and j?
return x[j] + d[j] - (x[i] - d[i]) > K;
}
bool works(int k) {
use.assign(n, MP(0, 0));
K = k;
VI vals = {INF, -INF, INF, -INF};
initsumdif();
int p1 = 0, p2 = 0;
for (int i = 0; i < n; i++) {
while (p1 < p2 and optimize(use[p1].y, i)) {
upd(use[p1++].y);
}
int sum = x[i] + d[i], dif = x[i] - d[i];
vals[0] = min(vals[0], (k - c) + dif + mndif);
vals[1] = max(vals[1], sum + mxsum - (k - c));
vals[2] = min(vals[2], (k - c) + dif - mxsum);
vals[3] = max(vals[3], sum - mndif - (k - c));
while (p2 and use[p2 - 1].x > x[i] - d[i]) p2--;
use[p2++] = MP(x[i] - d[i], i);
}
// now we need to find two points in the rectangle defined by vals
int maxsum, minsum, maxdif, mindif;
maxsum = vals[0];
minsum = vals[1];
maxdif = vals[2];
mindif = vals[3];
debug(maxsum, minsum, maxdif, mindif, k);
int curdif = 0;
int cursum = n;
if (maxsum < minsum or maxdif < mindif) return 0;
for (int i = 0; i < n; i++) {
while (curdif < n and x[curdif] - x[i] < mindif) curdif++;
while (cursum > 0 and x[cursum - 1] + x[i] >= minsum) cursum--;
int cur = max({cursum, curdif, i + 1});
if (cur < n and x[cur] + x[i] <= maxsum and x[cur] - x[i] <= maxdif)
return true;
}
// for(int i = 0; i < n; i++) {
// for(int j = 0; j < i; j++) {
// int sum = x[i] + x[j];
// int dif = x[i] - x[j];
// if(sum >= minsum and sum <= maxsum and dif >= mindif and dif <= maxdif)
// return true;
// }
// }
return false;
}
ll find_shortcut(int32_t _n, vector<int32_t> _l, vector<int32_t> _d,
int32_t _c) {
n = _n;
c = _c;
cop(l, _l);
cop(d, _d);
genPref();
// genDom();
// genOrd();
// debug(dom);
// debug(doms);
// debug(ord);
int xx = -1;
int z = INF;
for (int b = z; b >= 1; b /= 2) {
while (!works(xx + b)) xx += b;
}
return xx + 1;
}
// int32_t main() {
// #ifndef ONLINE_JUDGE
// freopen("shortcut.in", "r", stdin);
// freopen("shortcut.out", "w", stdout);
// #endif
// cin.sync_with_stdio(0);
// cin.tie(0);
// int n, c;
// cin >> n >> c;
// vector<int32_t> l(n - 1);
// vector<int32_t> d(n);
// for (int i = 0; i < n - 1; i++) {
// cin >> l[i];
// }
// for (int i = 0; i < n; i++) {
// cin >> d[i];
// }
// cout << find_shortcut(n, l, d, c) << '\n';
// return 0;
// }
Compilation message
shortcut.cpp: In function 'bool works(long long int)':
shortcut.cpp:20:20: warning: statement has no effect [-Wunused-value]
20 | #define debug(...) 42
| ^~
shortcut.cpp:176:3: note: in expansion of macro 'debug'
176 | debug(maxsum, minsum, maxdif, mindif, k);
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
364 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
364 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
364 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
364 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
0 ms |
364 KB |
n = 2, 3 is a correct answer |
7 |
Incorrect |
1 ms |
364 KB |
n = 3, incorrect answer: jury 29 vs contestant 20 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
364 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
364 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
364 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
364 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
0 ms |
364 KB |
n = 2, 3 is a correct answer |
7 |
Incorrect |
1 ms |
364 KB |
n = 3, incorrect answer: jury 29 vs contestant 20 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
364 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
364 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
364 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
364 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
0 ms |
364 KB |
n = 2, 3 is a correct answer |
7 |
Incorrect |
1 ms |
364 KB |
n = 3, incorrect answer: jury 29 vs contestant 20 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
364 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
364 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
364 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
364 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
0 ms |
364 KB |
n = 2, 3 is a correct answer |
7 |
Incorrect |
1 ms |
364 KB |
n = 3, incorrect answer: jury 29 vs contestant 20 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
364 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
364 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
364 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
364 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
0 ms |
364 KB |
n = 2, 3 is a correct answer |
7 |
Incorrect |
1 ms |
364 KB |
n = 3, incorrect answer: jury 29 vs contestant 20 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
364 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
364 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
364 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
364 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
0 ms |
364 KB |
n = 2, 3 is a correct answer |
7 |
Incorrect |
1 ms |
364 KB |
n = 3, incorrect answer: jury 29 vs contestant 20 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
364 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
364 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
364 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
364 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
0 ms |
364 KB |
n = 2, 3 is a correct answer |
7 |
Incorrect |
1 ms |
364 KB |
n = 3, incorrect answer: jury 29 vs contestant 20 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
364 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
364 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
364 KB |
n = 3, 4 is a correct answer |
5 |
Correct |
1 ms |
364 KB |
n = 2, 62 is a correct answer |
6 |
Correct |
0 ms |
364 KB |
n = 2, 3 is a correct answer |
7 |
Incorrect |
1 ms |
364 KB |
n = 3, incorrect answer: jury 29 vs contestant 20 |
8 |
Halted |
0 ms |
0 KB |
- |