Submission #288509

#TimeUsernameProblemLanguageResultExecution timeMemory
288509shayan_pTowns (IOI15_towns)C++17
100 / 100
29 ms896 KiB
#include<bits/stdc++.h> #include "towns.h" #define F first #define S second #define PB push_back #define sz(s) (int(s.size())) #define bit(n, k) (((n)>>(k))&1) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef long double ld; const int maxn = 1e5 + 10, mod = 1e9 + 7, inf = 1e9 + 7; int n; int ds[2][maxn], h[maxn], D[maxn]; int far(int u, int o){ ds[o][u] = 0; for(int i = 0; i < n; i++){ if(i != u) ds[o][i] = getDistance(u, i); } int mx = 0; for(int i = 0; i < n; i++){ if(ds[o][mx] < ds[o][i]) mx = i; } return mx; } bool same(int a, int b){ return getDistance(a, b) < h[a] + h[b]; } int par[maxn]; int fnd(int u){ return par[u] < 0 ? u : par[u] = fnd(par[u]); } void mrg(int a, int b){ if((a = fnd(a)) == (b = fnd(b))) return; par[a]+= par[b]; par[b] = a; } int hubDistance(int n, int sub){ fill(par, par + n, -1); ::n = n; int A = far(0, 0); int B = far(A, 1); int TR = ds[1][B]; int TR2 = ds[0][A]; int R = inf; map<int, int> mp; for(int i = 0; i < n; i++){ h[i] = (ds[0][i] + ds[1][i] - TR2) / 2; D[i] = ds[1][i] - h[i]; R = min(R, max(D[i], TR - D[i])); mp[D[i]]++; } int sm = 0; vector<pii> Ds; for(pii p : mp){ sm+= p.S; if(max(p.F, TR - p.F) == R){ Ds.PB({p.F, sm}); } } assert(!Ds.empty()); assert(sz(Ds) <= 2); int bstD = Ds[0].F; if(sz(Ds) == 2){ int A = Ds[0].S, B = n - Ds[0].S; if(A < B) bstD = Ds[1].F; } sm = 0; for(pii p : mp){ if(p.F < bstD) sm+= p.S; } if(sm + sm > n) return -R; sm = 0; for(pii p : mp){ if(p.F > bstD) sm+= p.S; } if(sm + sm > n) return -R; if(mp[bstD] * 2 <= n) return R; int U = -1, CNT = 0; for(int i = 0; i < n; i++){ if(D[i] == bstD){ if(CNT == 0 || same(U, i)){ if(CNT == 0) U = i; else mrg(U, i); CNT++; } else{ CNT--; } } } if(CNT == 0) return R; CNT = -par[ fnd(U) ]; for(int i = 0; i < n; i++){ if(D[i] == bstD && fnd(i) == i && fnd(i) != fnd(U) && same(U, i)){ CNT+= -par[ fnd(i) ]; } } if(CNT + CNT > n) return -R; return R; }

Compilation message (stderr)

towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:50:31: warning: declaration of 'n' shadows a global declaration [-Wshadow]
   50 | int hubDistance(int n, int sub){
      |                               ^
towns.cpp:18:5: note: shadowed declaration is here
   18 | int n;
      |     ^
towns.cpp:79:6: warning: declaration of 'A' shadows a previous local [-Wshadow]
   79 |  int A = Ds[0].S, B = n - Ds[0].S;
      |      ^
towns.cpp:53:9: note: shadowed declaration is here
   53 |     int A = far(0, 0);
      |         ^
towns.cpp:79:19: warning: declaration of 'B' shadows a previous local [-Wshadow]
   79 |  int A = Ds[0].S, B = n - Ds[0].S;
      |                   ^
towns.cpp:54:9: note: shadowed declaration is here
   54 |     int B = far(A, 1);
      |         ^
towns.cpp:50:28: warning: unused parameter 'sub' [-Wunused-parameter]
   50 | int hubDistance(int n, int sub){
      |                        ~~~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...