#include "shortcut.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
typedef vector<bool> vb;
typedef vector<ll> vi;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
typedef vector<vi> vvi;
typedef tuple<int, int, int> tiii;
typedef vector<tiii> vtiii;
#define sz(x) ((int)(x).size())
#define all(x) begin(x), end(x)
bool possible(int n, ll K, vi &X, vector<int> &d, ll c)
{
// Is it possible to have a shortcut from y to z.
vi ineq(4, 1LL << 40); // (-1)^b_1 y + (-1)^b_0 z <= ineq[b_1 b_0], where y < z is a shortcut from continous points y and z
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
if (d[i] + d[j] + X[j] - X[i] <= K)
continue;
ineq[0] = min(ineq[0], K - c - d[i] - d[j] + X[i] + X[j]);
ineq[1] = min(ineq[1], K - c - d[i] - d[j] + X[i] - X[j]);
ineq[2] = min(ineq[2], K - c - d[i] - d[j] - X[i] + X[j]);
ineq[3] = min(ineq[3], K - c - d[i] - d[j] - X[i] - X[j]);
}
}
bool poss = false;
for (int i = 0; i < n && !poss; ++i)
{
for (int j = i + 1; j < n && !poss; ++j)
{
poss = true;
for (int cnt = 0; cnt < 4; ++cnt)
{
int coeff_i = (cnt & 2) ? -1 : 1;
int coeff_j = (cnt & 1) ? -1 : 1;
poss = poss && ((coeff_i * X[i] + coeff_j * X[j]) <= ineq[cnt]);
}
}
}
return poss;
}
long long find_shortcut(int n, std::vector<int> l, std::vector<int> d, int c)
{
vi X(n);
partial_sum(all(l), X.begin() + 1);
ll max_diam = 0;
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
max_diam = max(max_diam, d[i] + d[j] + X[j] - X[i]);
}
}
ll lo = 0, up = max_diam;
while (lo < up)
{
ll m = (lo + up) / 2;
if (possible(n, m, X, d, c))
up = m;
else
lo = m + 1;
}
return lo;
}
Compilation message (stderr)
shortcut.h:1:9: warning: #pragma once in main file
1 | #pragma once
| ^~~~
shortcut_c.h:1:9: warning: #pragma once in main file
1 | #pragma once
| ^~~~| # | 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... |