Submission #1021139

#TimeUsernameProblemLanguageResultExecution timeMemory
1021139AmirAli_H1Towns (IOI15_towns)C++17
100 / 100
23 ms1116 KiB
// In the name of Allah #include <bits/stdc++.h> #include "towns.h" using namespace std; typedef long long int ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef complex<ld> cld; #define all(x) (x).begin(),(x).end() #define len(x) ((ll) (x).size()) #define F first #define S second #define pb push_back #define sep ' ' #define endl '\n' #define Mp make_pair #define kill(x) cout << x << '\n', exit(0) #define set_dec(x) cout << fixed << setprecision(x); #define file_io(x,y) freopen(x, "r", stdin); freopen(y, "w", stdout); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n; int v1, v2, u1, u2; const int maxn = 110 + 4; ll dis[maxn][maxn], R; int p[maxn], sz[maxn]; int get(int a) { return (p[a] == a) ? a : p[a] = get(p[a]); } void merge(int a, int b) { a = get(a); b = get(b); if (a == b) return ; if (sz[a] > sz[b]) swap(a, b); p[a] = b; sz[b] += sz[a]; sz[a] = 0; } ll get_dis(int u, int v) { if (dis[u][v] == -1) { dis[u][v] = dis[v][u] = getDistance(u, v); } return dis[u][v]; } bool ok(ll R) { bool flag = 0; ll dx = get_dis(v1, v2); for (int i = 0; i < n; i++) { ll d1 = get_dis(v1, i), d2 = get_dis(v2, i); ll x = (d1 + d2 - dx) / 2; if (d2 - x == R) { flag = 1; break; } } if (!flag) return 0; int t1 = 0, t2 = 0; for (int i = 0; i < n; i++) { ll d1 = get_dis(v1, i), d2 = get_dis(v2, i); ll x = (d1 + d2 - dx) / 2; if (d2 - x < R) t1++; else if (d2 - x > R) t2++; } return (max(t1, t2) * 2 <= n); } void check(int i, int j) { ll dx = get_dis(v1, v2); ll d1 = get_dis(v1, i), d2 = get_dis(v2, i); ll x1 = (d1 + d2 - dx) / 2; d1 = get_dis(v1, j), d2 = get_dis(v2, j); ll x2 = (d1 + d2 - dx) / 2; ll valx = (x1 + x2 - get_dis(i, j)) / 2; if (valx > 0) merge(i, j); return ; } int hubDistance(int N, int sub) { n = N; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) dis[i][j] = 0; else dis[i][j] = -1; } } v1 = 0, u1 = v1; for (int i = 0; i < n; i++) { if (get_dis(v1, i) > get_dis(v1, u1)) u1 = i; } v2 = u1, u2 = v2; for (int i = 0; i < n; i++) { if (get_dis(v2, i) > get_dis(v2, u2)) u2 = i; } ll dx = get_dis(v1, v2); ll D = get_dis(u1, u2); R = D; for (int i = 0; i < n; i++) { ll d1 = get_dis(v1, i), d2 = get_dis(v2, i); ll x = (d1 + d2 - dx) / 2; R = min(R, max(d2 - x, D - (d2 - x))); } if (!ok(R) && !ok(D - R)) return -R; iota(p, p + n, 0); fill(sz, sz + n, 1); int v = 0, cnt = 1; for (int i = 1; i < n; i++) { if (cnt == 0) { v = i; cnt = 1; } else { check(i, v); if (get(i) == get(v)) cnt++; else cnt--; } } for (int i = 0; i < n; i++) { if (get(i) != i || get(i) == get(v)) continue; check(i, v); } if (sz[get(v)] * 2 <= n) return R; else return -R; }

Compilation message (stderr)

towns.cpp: In function 'bool ok(ll)':
towns.cpp:50:12: warning: declaration of 'R' shadows a global declaration [-Wshadow]
   50 | bool ok(ll R) {
      |         ~~~^
towns.cpp:29:21: note: shadowed declaration is here
   29 | ll dis[maxn][maxn], R;
      |                     ^
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:112:35: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  112 |  if (!ok(R) && !ok(D - R)) return -R;
      |                                   ^~
towns.cpp:132:34: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  132 |  if (sz[get(v)] * 2 <= n) return R;
      |                                  ^
towns.cpp:133:14: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  133 |  else return -R;
      |              ^~
towns.cpp:84:28: warning: unused parameter 'sub' [-Wunused-parameter]
   84 | 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...