Submission #883766

#TimeUsernameProblemLanguageResultExecution timeMemory
883766MilosMilutinovicTowns (IOI15_towns)C++14
100 / 100
15 ms860 KiB
#include "towns.h" #include <bits/stdc++.h> using namespace std; int hubDistance(int n, int sub) { map<pair<int, int>, int> mp; auto Ask = [&](int l, int r) { if (l > r) { swap(l, r); } if (l == r) { return 0; } if (mp.count({l, r})) { return mp[{l, r}]; } mp[{l, r}] = getDistance(l, r); return mp[{l, r}]; }; int x = 0, y = 0; for (int i = 1; i < n; i++) { if (Ask(0, i) > Ask(0, x)) { x = i; } } for (int i = 0; i < n; i++) { if (Ask(x, i) > Ask(x, y)) { y = i; } } vector<int> d; vector<int> my(n); for (int i = 0; i < n; i++) { my[i] = (Ask(i, x) - Ask(i, 0) + Ask(0, x)) / 2; d.push_back(my[i]); } sort(d.begin(), d.end()); d.erase(unique(d.begin(), d.end()), d.end()); int sz = (int) d.size(); vector<int> r(sz); for (int i = 0; i < sz; i++) { r[i] = max(d[i], Ask(x, y) - d[i]); } int mn = *min_element(r.begin(), r.end()); vector<int> cnt(sz); for (int i = 0; i < n; i++) { int idx = (int) (lower_bound(d.begin(), d.end(), my[i]) - d.begin()); cnt[idx] += 1; } vector<int> pref(sz); for (int i = 0; i < sz; i++) { if (i > 0) { pref[i] = pref[i - 1]; } pref[i] += cnt[i]; } vector<int> cands; for (int i = 1; i + 1 < sz; i++) { if (r[i] == mn && max((i == 0 ? 0 : pref[i - 1]), pref[sz - 1] - pref[i]) <= n / 2) { cands.push_back(i); } } if ((int) cands.size() == 0) { return -mn; } if ((int) cands.size() == 2) { return mn; } auto Solve = [&](int h) { vector<int> ids(n); iota(ids.begin(), ids.end(), 0); vector<int> par(n); iota(par.begin(), par.end(), 0); vector<int> sz(n, 1); function<int(int)> Get = [&](int x) { return par[x] == x ? x : par[x] = Get(par[x]); }; auto Merge = [&](int a, int b) { a = Get(a); b = Get(b); if (a == b) { return; } par[b] = a; sz[a] += sz[b]; }; auto Diff = [&](int a, int b) { if ((my[a] == d[h]) != (my[b] == d[h])) { return true; } if (my[a] != d[h]) { return (my[a] < d[h]) != (my[b] < d[h]); } int d_a = Ask(a, x) - my[a]; int d_b = Ask(b, x) - my[b]; assert(d_a >= 0 && d_b >= 0); assert(d_a + d_b >= Ask(a, b)); return d_a + d_b == Ask(a, b); }; vector<int> vec; while (ids.size() > 1) { vector<int> new_ids; for (int i = 0; i + 1 < (int) ids.size(); i += 2) { if (Diff(ids[i], ids[i + 1])) { // ... } else { Merge(ids[i], ids[i + 1]); new_ids.push_back(ids[i]); } } if ((int) ids.size() % 2 == 1) { vec.push_back(ids.back()); ids.pop_back(); } swap(ids, new_ids); } int e = 0; if (!ids.empty()) { e = ids[0]; } else { if (!vec.empty()) { e = vec.back(); } } for (int i = 0; i < n; i++) { if (i != Get(i) || i == Get(e)) { continue; } if (!Diff(i, e)) { Merge(i, e); } } return *max_element(sz.begin(), sz.end()) <= n / 2; }; if (Solve(cands[0])) { return mn; } else { return -mn; } }

Compilation message (stderr)

towns.cpp: In lambda function:
towns.cpp:75:17: warning: declaration of 'sz' shadows a previous local [-Wshadow]
   75 |     vector<int> sz(n, 1);
      |                 ^~
towns.cpp:40:7: note: shadowed declaration is here
   40 |   int sz = (int) d.size();
      |       ^~
towns.cpp: In lambda function:
towns.cpp:76:38: warning: declaration of 'x' shadows a previous local [-Wshadow]
   76 |     function<int(int)> Get = [&](int x) {
      |                                  ~~~~^
towns.cpp:21:7: note: shadowed declaration is here
   21 |   int x = 0, y = 0;
      |       ^
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:6:28: warning: unused parameter 'sub' [-Wunused-parameter]
    6 | 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...