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 "towns.h"
#include <bits/stdc++.h>
using namespace std;
// macros
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> lll;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<lll> vlll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define RE1(a,c) REP(a,1,c+1)
#define REI(a,b,c) REP(a,b,c+1)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define FOR(a,b) for(auto& a : b)
#define all(a) a.begin(), a.end()
#define INF 1e9
#define EPS 1e-9
#define pb push_back
#define popb pop_back
#define fi first
#define se second
#define sz size()
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// output
template<class T1, class T2> void OUT(const pair<T1,T2>& x);
template<class T> void OUT(const vector<T>& x);
template<class T> void OUT(const T& x) {cerr << x;}
template<class H, class... T> void OUT(const H& h, const T&... t) {OUT(h); OUT(t...); }
template<class T1, class T2> void OUT(const pair<T1,T2>& x) {OUT(x.fi,' ',x.se);}
template<class T> void OUT(const vector<T>& x) {RE(i,x.size()) OUT(i==0?"":" ",x[i]);}
template<class... T> void OUTL(const T&... t) {OUT(t..., "\n"); }
template<class H> void OUTLS(const H& h) {OUTL(h); }
template<class H, class... T> void OUTLS(const H& h, const T&... t) {OUT(h,' '); OUTLS(t...); }
const int MX = 120;
static int mem[MX][MX];
int n;
int getD(int u, int v) {
if(mem[u][v] != -1)
return mem[u][v];
return mem[u][v] = mem[v][u] = getDistance(u,v);
}
int hubDistance(int N, int sub) {
n = N;
RE(i,n) RE(j,n) mem[i][j] = -1;
RE(i,n) mem[i][i] = 0;
int u = 0, v=0;
RE(i,n)
if(getD(u,i) > getD(u,v))
v = i;
RE(i,n)
if(getD(v,i) > getD(u,v))
u = i;
int res = getD(u,v);
RE(i,n) {
int nres = getD(0,i) + getD(v,i) - getD(0,v);
nres /= 2;
nres = getD(v,i) - nres;
nres = max(nres, getD(u,v)-nres);
res = min(res, nres);
}
int cnt1=0, cnt2=0;
RE(i,n) {
int nres = getD(0,i) + getD(v,i) - getD(0,v);
nres /= 2;
nres = getD(v,i) - nres;
if(res != max(nres, getD(u,v)-nres)) {
(nres < res ? cnt1 : cnt2)++;
}
}
auto sameColor = [&](int x, int y) {
int res = getD(0,x) + getD(v,y) - getD(x,y) - getD(0,v);
return res > 0;
};
if(cnt1 > n/2 || cnt2 > n/2)
return -res;
vi seq = {0};
vi buck;
REP(i,1,n) {
if(sameColor(seq.back(), i)) {
buck.pb(i);
} else {
seq.pb(i);
if(!buck.empty())
seq.pb((buck.back())), buck.pop_back();
}
}
int comp = seq.back();
while(!seq.empty()) {
if(sameColor(seq.back(), comp)) {
if(seq.size() > 1)
seq.pop_back(), seq.pop_back();
else
buck.pb(seq.back()), seq.pop_back();
} else {
if(buck.empty())
return res;
buck.pop_back();
seq.pop_back();
}
}
if(buck.empty())
return res;
return -res;
}
Compilation message (stderr)
towns.cpp: In lambda function:
towns.cpp:88:13: warning: declaration of 'res' shadows a previous local [-Wshadow]
88 | int res = getD(0,x) + getD(v,y) - getD(x,y) - getD(0,v);
| ^~~
towns.cpp:68:9: note: shadowed declaration is here
68 | int res = getD(u,v);
| ^~~
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:55:28: warning: unused parameter 'sub' [-Wunused-parameter]
55 | int hubDistance(int N, int sub) {
| ~~~~^~~
# | 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... |