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 "rail.h"
#include <bits/stdc++.h>
using namespace std;
using i32 = int;
using i64 = long long;
template <typename T>
using Vec = vector<T>;
#define OVERRIDE4(a, b, c, d, ...) d
#define REP2(i, n) for (i32 i = 0; i < (i32)(n); ++i)
#define REP3(i, m, n) for (i32 i = (i32)(m); i < (i32)(n); ++i)
#define REP(...) OVERRIDE4(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define ALL(x) begin(x), end(x)
#define PER(i, n) for (i32 i = (i32)(n) - 1; i >= 0; --i)
template <typename T>
bool chmin(T &x, const T &y) {
if (x > y) {
x = y;
return true;
} else {
return false;
}
}
template <typename T>
bool chmax(T &x, const T &y) {
if (x < y) {
x = y;
return true;
} else {
return false;
}
}
constexpr i32 INF = 1001001001;
constexpr i64 INF64 = 3003003003003003003;
void findLocation(int n, int first, int location[], int stype[]) {
Vec<i32> dist_0(n, 0);
REP(i, 1, n) {
dist_0[i] = getDistance(0, i);
}
i32 y = 0;
{
i32 mn = INF;
REP(i, 1, n) {
if (chmin(mn, dist_0[i])) {
y = i;
}
}
}
Vec<i32> dist_y(n, 0);
REP(i, n) {
if (i != y) {
dist_y[i] = getDistance(i, y);
}
}
i32 x = 0;
{
i32 mn = INF;
REP(i, 0, n) {
if (i != y && chmin(mn, dist_y[i])) {
x = i;
}
}
}
Vec<i32> dist_x(n, 0);
REP(i, n) {
if (i != x) {
dist_x[i] = getDistance(i, x);
}
}
location[y] = first + dist_0[y];
stype[y] = 2;
location[x] = location[y] - dist_y[x];
stype[x] = 1;
{
Vec<i32> idx;
REP(i, n) {
if (i != x && i != y && dist_x[i] > dist_y[i]) {
idx.push_back(i);
}
}
sort(ALL(idx), [&](i32 i, i32 j) -> bool {
return dist_y[i] < dist_y[j];
});
i32 last = x;
Vec<i32> ones;
for (i32 i : idx) {
if (last == x) {
location[i] = location[y] - dist_y[i];
stype[i] = 1;
last = i;
ones.push_back(location[i]);
continue;
}
i32 d = getDistance(i, last);
i32 ph = location[last] + d;
if (ph >= location[x]) {
location[i] = location[y] - dist_y[i];
stype[i] = 1;
last = i;
ones.push_back(location[i]);
continue;
}
i32 mx = -INF;
for (i32 pos : ones) {
if (pos < ph) {
chmax(mx, pos);
}
}
if (ph - mx + location[y] - mx == dist_y[i]) {
location[i] = ph;
stype[i] = 2;
} else {
location[i] = location[y] - dist_y[i];
stype[i] = 1;
last = i;
ones.push_back(location[i]);
}
}
}
{
Vec<i32> idx;
REP(i, n) {
if (i != x && i != y && dist_x[i] < dist_y[i]) {
idx.push_back(i);
}
}
sort(ALL(idx), [&](i32 i, i32 j) -> bool {
return dist_x[i] < dist_x[j];
});
i32 last = y;
Vec<i32> twos;
for (i32 i : idx) {
if (last == y) {
location[i] = location[x] + dist_x[i];
stype[i] = 2;
last = i;
twos.push_back(location[i]);
continue;
}
i32 d = getDistance(i, last);
i32 ph = location[last] - d;
if (ph <= location[y]) {
location[i] = location[x] + dist_x[i];
stype[i] = 2;
last = i;
twos.push_back(location[i]);
continue;
}
i32 mn = INF;
for (i32 pos : twos) {
if (pos > ph) {
chmin(mn, pos);
}
}
if (mn - ph + mn - location[x] == dist_x[i]) {
location[i] = ph;
stype[i] = 1;
} else {
location[i] = location[x] + dist_x[i];
stype[i] = 2;
last = i;
twos.push_back(location[i]);
}
}
}
/*Vec<Vec<i32>> dist(n, Vec<i32>(n, 0));
REP(i, n) REP(j, i) {
dist[i][j] = dist[j][i] = getDistance(i, j);
}
Vec<i32> mn(n, INF), mnrg(n, 0);
REP(i, n) {
REP(j, n) {
if (i != j && chmin(mn[i], dist[i][j])) {
mnrg[i] = j;
}
}
}
i32 y = mnrg[0];
i32 x = mnrg[y];
stype[x] = 1;
location[x] = first + mn[0] - mn[y];
stype[y] = 2;
location[y] = first + mn[0];
REP(i, n) {
if (i == x || i == y) {
continue;
}
if (dist[i][x] < dist[i][y]) {
continue;
}
if (mnrg[i] != y && dist[i][mnrg[i]] + dist[mnrg[i]][y] == dist[i][y]) {
continue;
}
stype[i] = 1;
location[i] = location[y] - dist[i][y];
}
REP(i, n) {
if (i == x || i == y) {
continue;
}
if (dist[i][x] < dist[i][y]) {
continue;
}
if (mnrg[i] != y && dist[i][mnrg[i]] + dist[mnrg[i]][y] == dist[i][y]) {
stype[i] = 2;
location[i] = location[mnrg[i]] + mn[i];
}
}
REP(i, n) {
if (i == x || i == y) {
continue;
}
if (dist[i][x] > dist[i][y]) {
continue;
}
if (mnrg[i] != x && dist[i][mnrg[i]] + dist[mnrg[i]][x] == dist[i][x]) {
continue;
}
stype[i] = 2;
location[i] = location[x] + dist[i][x];
}
REP(i, n) {
if (i == x || i == y) {
continue;
}
if (dist[i][x] > dist[i][y]) {
continue;
}
if (mnrg[i] != x && dist[i][mnrg[i]] + dist[mnrg[i]][x] == dist[i][x]) {
stype[i] = 1;
location[i] = location[mnrg[i]] - mn[i];
}
}*/
}
# | 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... |