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);
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++) {
int goal = mm - c - d[i] - d[j];
int zavg = l[j], ymin = l[i] - goal, ymax = l[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 = l[i] + l[j], Y = l[i] - l[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() {
cout << find_shortcut(4, {10, 20, 20}, {0, 40, 0, 30}, 10) << "\n";
cout << find_shortcut(9, {10, 10, 10, 10, 10, 10, 10, 10}, {20, 0, 30, 0, 0, 40, 0, 40, 0}, 30) << "\n";
cout << find_shortcut(4, {2, 2, 2}, {1, 10, 10, 1}, 1) << "\n";
cout << find_shortcut(3, {1, 1}, {1, 1, 1}, 3) << "\n";
cout << find_shortcut(2, {10}, {1, 1}, 1) << "\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... |