#include "shortcut.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3010;
const ll inf = 1e18;
int n;
ll cp[maxn], cs[maxn], pref[maxn], bef[maxn], aft[maxn];
ll l[maxn], d[maxn], c;
ll dist[maxn];
struct edge
{
int to;
ll w;
edge(int _to = 0, ll _w = 0)
{
to = _to;
w = _w;
}
bool operator < (const edge &e) const
{
return w > e.w;
}
};
int used[maxn];
ll get_dist(int l, int r)
{
if (l > r)
swap(l, r);
return pref[r - 1] - pref[l - 1];
}
const int maxlog = 21;
struct sparse_table
{
ll dp[maxlog][maxn];
int lg[maxn];
void build(vector < ll > values)
{
int len = values.size();
for (int i = 1; i <= len; i ++)
{
lg[i] = lg[i / 2] + 1;
dp[0][i] = values[i - 1];
}
for (int j = 1; j < lg[len]; j ++)
{
for (int i = 1; i <= len - (1 << j) + 1; i ++)
{
dp[j][i] = dp[j - 1][i + (1 << (j - 1))];
if (dp[j - 1][i] > dp[j][i])
dp[j][i] = dp[j - 1][i];
}
}
}
ll query(int l, int r)
{
if (l > r)
return 0;
int mlog = lg[r - l + 1] - 1;
ll ans = dp[mlog][r - (1 << mlog) + 1];
if (dp[mlog][l] > ans)
ans = dp[mlog][l];
return ans;
}
};
ll find_shortcut(int N, vector<int> L, vector<int> D, int C)
{
n = N;
c = C;
for (int i = 0; i < n - 1; i ++)
l[i + 1] = L[i];
for (int i = 0; i < n; i ++)
d[i + 1] = D[i];
for (int i = 1; i <= n; i ++)
{
pref[i] = pref[i - 1] + l[i];
}
for (int i = 1; i <= n; i ++)
{
cp[i] = cp[i - 1] + l[i - 1];
cp[i] = max(cp[i], d[i]);
bef[i] = bef[i - 1];
for (int j = 1; j < i; j ++)
{
bef[i] = max(bef[i], get_dist(i, j) + d[i] + d[j]);
}
}
for (int i = n; i > 0; i --)
{
cs[i] = cs[i + 1] + l[i];
cs[i] = max(cs[i], d[i]);
aft[i] = aft[i + 1];
for (int j = i + 1; j <= n; j ++)
{
aft[i] = max(aft[i], get_dist(i, j) + d[i] + d[j]);
}
}
vector < ll > dir_values;
for (int i = 1; i <= n; i ++)
{
dir_values.push_back({pref[i - 1] + d[i]});
}
sparse_table direct;
direct.build(dir_values);
vector < ll > ar_values;
for (int i = 1; i <= n; i ++)
{
ar_values.push_back({-pref[i - 1] + d[i]});
}
sparse_table around;
around.build(ar_values);
ll ans = inf;
for (int l = 1; l <= n; l ++)
{ ll last = inf;
for (int r = l + 1; r <= n; r ++)
{
ll res = max(bef[l], aft[r]), len = pref[r - 1] - pref[l - 1] + c;
int rb = l - 1;
for (int i = l; i <= r; i ++)
{
/**int lf = i, rf = r;
while(lf <= rf)
{
int mf = (lf + rf) / 2;
if (get_dist(i, mf) <= len - get_dist(i, mf))
lf = mf + 1;
else
rf = mf - 1;
}*/
while(rb < r)
{
if (get_dist(i, rb + 1) <= len - get_dist(i, rb + 1))
{
rb ++;
}
else
break;
}
if (rb > i)
{
/**cout << "vergleich " << direct.query(i + 1, rb) << " " << *act.rbegin() << endl;
for (int p = i + 1; p <= rb; p ++)
cout << dir_values[p - 1] << " ";
cout << endl;*/
ll path = direct.query(i + 1, rb) - pref[i - 1] + d[i];
if (i == l)
path = path - d[i] + cp[i];
res = max(res, path);
if (rb >= r)
{
path = dir_values[r - 1] - pref[i - 1] + d[i] - d[r] + cs[r];
if (i == l)
path = path - d[i] + cp[i];
res = max(res, path);
}
}
/**for (int j = i + 1; j <= rb; j ++)
{
ll path = get_dist(i, j) + d[i] + d[j];
if (i == l)
path = path - d[i] + cp[i];
if (j == r)
path = path - d[j] + cs[j];
res = max(res, path);
}*/
//cout << "line " << r - i << " " << act.size() + rev.size() << endl;
if (rb < r)
{
ll path = around.query(rb + 1, r) + pref[i - 1] + d[i] + len;
if (i == l)
path = path - d[i] + cp[i];
res = max(res,path);
path = ar_values[r - 1] + pref[i - 1] + d[i] + len - d[r] + cs[r];
if (i == l)
path = path - d[i] + cp[i];
res = max(res, path);
}
/**cout << "line " << res << endl;
for (int j = rb + 1; j <= r; j ++)
{
ll path = len - get_dist(i, j) + d[i] + d[j];
if (i == l)
path = path - d[i] + cp[i];
if (j == r)
path = path - d[j] + cs[j];
res = max(res, path);
}
cout << "aft " << res << endl;*/
/**for (int j = i + 1; j <= r; j ++)
{
ll path = min(len - get_dist(i, j), get_dist(i, j)) + d[i] + d[j];
if (i == l)
path = path - d[i] + cp[i];
if (j == r)
path = path - d[j] + cs[j];
res = max(res, path);
}*/
}
if (res == last)
break;
last = res;
///cout << "range " << l << " " << r << " "
ans = min(ans, res);
}
}
return ans;
}
Compilation message
shortcut.cpp: In function 'll find_shortcut(int, std::vector<int>, std::vector<int>, int)':
shortcut.cpp:56:35: warning: 'direct.sparse_table::lg[0]' may be used uninitialized in this function [-Wmaybe-uninitialized]
56 | for (int j = 1; j < lg[len]; j ++)
| ~~~~~~^
shortcut.cpp:56:35: warning: 'direct.sparse_table::lg[0]' may be used uninitialized in this function [-Wmaybe-uninitialized]
shortcut.cpp:56:35: warning: 'around.sparse_table::lg[<unknown>]' may be used uninitialized in this function [-Wmaybe-uninitialized]
56 | for (int j = 1; j < lg[len]; j ++)
| ~~~~~~^
shortcut.cpp:56:35: warning: 'around.sparse_table::lg[0]' may be used uninitialized in this function [-Wmaybe-uninitialized]
shortcut.cpp:56:35: warning: 'direct.sparse_table::lg[<unknown>]' may be used uninitialized in this function [-Wmaybe-uninitialized]
56 | for (int j = 1; j < lg[len]; j ++)
| ~~~~~~^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1368 KB |
n = 4, 80 is a correct answer |
2 |
Incorrect |
1 ms |
1368 KB |
n = 9, incorrect answer: jury 110 vs contestant 130 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1368 KB |
n = 4, 80 is a correct answer |
2 |
Incorrect |
1 ms |
1368 KB |
n = 9, incorrect answer: jury 110 vs contestant 130 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1368 KB |
n = 4, 80 is a correct answer |
2 |
Incorrect |
1 ms |
1368 KB |
n = 9, incorrect answer: jury 110 vs contestant 130 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1368 KB |
n = 4, 80 is a correct answer |
2 |
Incorrect |
1 ms |
1368 KB |
n = 9, incorrect answer: jury 110 vs contestant 130 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1368 KB |
n = 4, 80 is a correct answer |
2 |
Incorrect |
1 ms |
1368 KB |
n = 9, incorrect answer: jury 110 vs contestant 130 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1368 KB |
n = 4, 80 is a correct answer |
2 |
Incorrect |
1 ms |
1368 KB |
n = 9, incorrect answer: jury 110 vs contestant 130 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1368 KB |
n = 4, 80 is a correct answer |
2 |
Incorrect |
1 ms |
1368 KB |
n = 9, incorrect answer: jury 110 vs contestant 130 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1368 KB |
n = 4, 80 is a correct answer |
2 |
Incorrect |
1 ms |
1368 KB |
n = 9, incorrect answer: jury 110 vs contestant 130 |
3 |
Halted |
0 ms |
0 KB |
- |