This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<long long, long long>
int dfs(int n, vector<vector<pii>> &g, int v, int p) {
int ans = 0;
for (auto [u, du] : g[v]) {
if (u != p) {
ans = max(ans, dfs(u, g, u, v) + du);
}
}
return ans;
}
int diam(int n, vector<signed> l, vector<signed> d) {
vector<vector<pii>> g(2 * n);
for (int i = 0; i < n - 1; i++) {
g[i].push_back({i + 1, l[i]});
g[i + 1].push_back({i, l[i]});
}
for (int i = 0; i < n; i++) {
g[i].push_back({n + i, d[i]});
g[n + i].push_back({i, d[i]});
}
int ans = 0;
for (int i = 0; i < 2 * n; i++) ans = max(ans, dfs(n, g, i, i));
return ans;
}
int find_shortcut(signed n, vector<signed> l, vector<signed> d, signed c) {
int noshort = diam(n, l, d);
vector<int> x(n);
for (int i = 0; i < n - 1; i++) x[i + 1] = l[i] + x[i];
int ll = 0, rr = 1e17;
while (rr - ll > 1) {
int mm = (ll + rr) / 2;
int Xmn = -1e17, Ymn = -1e17, Xmx = 1e17, Ymx = 1e17; // X = y + z, Y = y - z
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (x[j] - x[i] + (int) d[i] + (int) d[j] <= mm) continue;
int goal = mm - c - (int) d[i] - (int) d[j];
int zavg = x[j], ymin = x[i] - goal, ymax = x[i] + goal;
Xmn = max(Xmn, ymin + zavg);
Xmx = min(Xmx, ymax + zavg);
Ymn = max(Ymn, ymin - zavg);
Ymx = min(Ymx, ymax - zavg);
}
}
bool ok = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int X = x[i] + x[j], Y = x[i] - x[j];
ok = ok || (Xmn <= X && X <= Xmx && Ymn <= Y && Y <= Ymx);
}
}
if (ok) rr = mm;
else ll = mm;
}
return min(rr, noshort);
}
#ifdef LOCAL
signed main() {
int n, c;
cin >> n >> c;
vector<signed> l(n - 1), 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";
}
#endif
Compilation message (stderr)
shortcut.cpp: In function 'long long int dfs(long long int, std::vector<std::vector<std::pair<long long int, long long int> > >&, long long int, long long int)':
shortcut.cpp:10:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
10 | for (auto [u, du] : g[v]) {
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |