Submission #623956

#TimeUsernameProblemLanguageResultExecution timeMemory
6239568e7Towns (IOI15_towns)C++17
49 / 100
18 ms460 KiB
//Challenge: Accepted #include <bits/stdc++.h> using namespace std; #ifdef zisk void debug(){cout << endl;} template<class T, class ... U> void debug(T a, U ...b){cout << a << " ", debug(b...);} template<class T> void pary(T l, T r) { while (l != r) cout << *l << " ", l++; cout << endl; } #else #define debug(...) 0 #define pary(...) 0 #endif #define ll long long #define maxn 155 #define pii pair<int, int> #define ff first #define ss second #include "towns.h" int n; int save[maxn][maxn]; bool done[maxn][maxn]; int query(int u, int v) { if (u == v || u >= n || v >= n) return 0; if (done[u][v]) return save[u][v]; save[u][v] = save[v][u] = getDistance(u, v); done[u][v] = done[v][u] = 1; return save[u][v]; } bool check[2*maxn]; vector<int> eq[maxn], dif[maxn]; int hubDistance(int N, int sub) { n = N; for (int i = 0;i <= n;i++) { for (int j = 0;j <= n;j++) done[i][j] = 0; eq[i].clear(); dif[i].clear(); } for (int i = 0;i <= 2*n;i++) check[i] = 0; vector<int> da, db, l(n), w(n), ws; int b = 0, ma = 0; for (int i = 0;i < n;i++) { da.push_back(query(0, i)); if (da.back() > ma) { ma = da.back(); b = i; } } int len = ma; for (int i = 0;i < n;i++) { db.push_back(query(b, i)); ma = max(ma, db.back()); } //debug(ma); int R = 1<<30; for (int i = 0;i < n;i++) { l[i] = (da[i] + db[i] - len) / 2; w[i] = db[i] - l[i]; //debug(l[i], w[i]); ws.push_back(w[i]); R = min(R, max(w[i], ma - w[i])); } sort(ws.begin(), ws.end()); ws.resize(int(unique(ws.begin(), ws.end()) - ws.begin())); int siz = ws.size(); vector<vector<int> > col(siz, vector<int>()); vector<int> pref(siz, 0), suf(siz, 0); for (int i = 0;i < n;i++) { int ind = lower_bound(ws.begin(), ws.end(), w[i]) - ws.begin(); col[ind].push_back(i); debug(i, ind); } for (int i = 0;i < siz;i++) pref[i] = col[i].size() + (i ? pref[i-1] : 0); for (int i = siz-1;i >= 0;i--) suf[i] = col[i].size() + (i < siz-1 ? suf[i+1] : 0); vector<int> vec, tmp; for (int i = 0;i < siz;i++) { int v = max(ws[i], ma - ws[i]); if (v == R) { if ((i == 0 || pref[i-1] <= n/2) && (i == siz-1 || suf[i+1] <= n/2) && col[i].size() <= n/2) { return R; } else if (col[i].size() > n/2) { vec = col[i]; for (int j = 0;j < n - col[i].size();j++) vec.push_back(n+j); break; } } } if (vec.size() == 0) return -R; tmp = vec; //pary(vec.begin(), vec.end()); auto same = [&] (int i, int j) { int v = query(i, j); if (v == 0) return 0; int d = l[i] + l[j] - v; //debug("same", i, j, d ? 1 : 0); return d ? 1 : 0; }; //pary(vec.begin(), vec.end()); int surv = 0, hp = 0; for (int i:vec) { if (hp == 0) surv = i, hp = 1; else if (same(surv, i)) { eq[surv].push_back(i); hp++; } else { dif[surv].push_back(i); hp--; } } if (hp == 0) return R; int good = 1, bad = 0; for (int i:eq[surv]) good++, check[i] = 1; check[surv] = 1; for (int i:vec) { if (check[i]) continue; if (i == surv || same(surv, i)) { good++; for (int j:eq[i]) good++, check[j] = 1; for (int j:dif[i]) bad++, check[j] = 1; } else { bad++; } if (good > n/2) return -R; if (bad > n/2) return R; } if (good > n/2) return -R; else return R; } /* 1 1 6 0 20 12 12 12 12 20 0 12 12 12 12 12 12 0 2 2 2 12 12 2 0 2 2 12 12 2 2 0 2 12 12 2 2 2 0 */

Compilation message (stderr)

towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:69:19: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   69 |  int siz = ws.size();
      |            ~~~~~~~^~
towns.cpp:73:53: warning: conversion from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
   73 |   int ind = lower_bound(ws.begin(), ws.end(), w[i]) - ws.begin();
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
towns.cpp:12:20: warning: statement has no effect [-Wunused-value]
   12 | #define debug(...) 0
      |                    ^
towns.cpp:75:3: note: in expansion of macro 'debug'
   75 |   debug(i, ind);
      |   ^~~~~
towns.cpp:77:54: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} may change value [-Wconversion]
   77 |  for (int i = 0;i < siz;i++) pref[i] = col[i].size() + (i ? pref[i-1] : 0);
      |                                        ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
towns.cpp:78:56: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} may change value [-Wconversion]
   78 |  for (int i = siz-1;i >= 0;i--) suf[i] = col[i].size() + (i < siz-1 ? suf[i+1] : 0);
      |                                          ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
towns.cpp:84:89: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   84 |    if ((i == 0 || pref[i-1] <= n/2) && (i == siz-1 || suf[i+1] <= n/2) && col[i].size() <= n/2) {
      |                                                                           ~~~~~~~~~~~~~~^~~~~~
towns.cpp:86:29: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   86 |    } else if (col[i].size() > n/2) {
      |               ~~~~~~~~~~~~~~^~~~~
towns.cpp:88:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |     for (int j = 0;j < n - col[i].size();j++) vec.push_back(n+j);
      |                    ~~^~~~~~~~~~~~~~~~~~~
towns.cpp:35:28: warning: unused parameter 'sub' [-Wunused-parameter]
   35 | 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...