#include "shortcut.h"
#include <bits/stdc++.h>
using namespace std;
#define n N
#define c C
#define int long long
const int inf = 1e16 + 42;
const int maxn = 500; // !!!!!
int n, c;
vector<int> v, nxt_dst, prv_dst;
vector<vector<int> > dist;
int ans;
int pref_max[maxn + 2];
int suff_max[maxn + 2];
int pref_d[maxn + 2];
int suff_d[maxn + 2];
int bet_d[maxn + 2][maxn + 2];
int bb(int a, int b) {
int res = 0;
int l = b, r = b;
while(l >= a) {
while(dist[l][r] > dist[l][a] + c + dist[b][r]) {
res = max(res, v[l] + v[r] + dist[l][a] + c + dist[b][r]);
r--;
}
res = max(res, bet_d[l][r]);
l--;
}
return res;
}
int solve_for(int a, int b) {
int res = 0;
// aa
res = max(res, pref_d[a]);
// ab
for(int i = a; i <= b; i++) {
res = max(res, v[i] + pref_max[a] + min(dist[i][a], dist[i][b] + c));
}
// ac
res = max(res, pref_max[a] + suff_max[b] + min(c, dist[a][b]));
// bb
res = max(res, bb(a, b));
// bc
for(int i = a; i <= b; i++) {
res = max(res, v[i] + suff_max[b] + min(dist[i][b], dist[i][a] + c));
}
// cc
res = max(res, suff_d[b]);
//cout << a << " " << b << ": " << res << "\n";
return res;
}
int solve() {
dist.assign(1 + n + 1, vector<int> (1 + n + 1, 0));
for(int i = 1; i <= n; i++) {
int sum = 0;
for(int j = i; j <= n; j++) {
dist[i][j] = dist[j][i] = sum;
sum += nxt_dst[j];
}
}
pref_max[0] = -inf;
pref_d[0] = -inf;
for(int i = 1; i <= n; i++) {
pref_d[i] = max(pref_d[i - 1], pref_max[i - 1] + v[i]);
pref_max[i] = max(v[i], pref_max[i - 1] + prv_dst[i]);
}
suff_max[n + 1] = -inf;
suff_d[n + 1] = -inf;
for(int i = n; i >= 1; i--) {
suff_d[i] = max(suff_d[i + 1], suff_max[i + 1] + v[i]);
suff_max[i] = max(v[i], suff_max[i + 1] + nxt_dst[i]);
}
for(int i = 1; i <= n; i++) {
int maxi = v[i] + nxt_dst[i];
for(int j = i + 1; j <= n; j++) {
bet_d[i][j] = maxi + v[j];
maxi = max(maxi, v[j]);
maxi += nxt_dst[j];
}
}
ans = inf;
for(int i = 1; i <= n; i++) {
for(int j = i + 1; j <= n; j++) {
ans = min(ans, solve_for(i, j));
}
}
return ans;
}
#undef n
#undef c
int find_shortcut(signed n, vector<signed> l, vector<signed> d, signed c) {
N = n;
C = c;
v.assign(1 + N + 1, 0);
nxt_dst.assign(1 + N + 1, 0);
prv_dst.assign(1 + N + 1, 0);
for(int i = 0; i < N; i++) {
v[i + 1] = d[i];
if(i != N - 1) {
nxt_dst[i + 1] = l[i];
prv_dst[i + 2] = l[i];
}
}
return solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
340 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
340 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
212 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
1 ms |
212 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
340 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
340 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
212 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
1 ms |
212 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
340 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
340 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
212 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
1 ms |
212 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
340 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
340 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
212 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
1 ms |
212 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
340 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
340 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
212 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
1 ms |
212 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
340 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
340 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
212 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
1 ms |
212 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
340 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
340 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
212 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
1 ms |
212 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
340 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
340 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
0 ms |
212 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
1 ms |
212 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |