Submission #640802

#TimeUsernameProblemLanguageResultExecution timeMemory
640802piOOETowns (IOI15_towns)C++17
100 / 100
15 ms360 KiB
#include <bits/stdc++.h> #include "towns.h" using namespace std; int hubDistance(int n, int sub) { vector<int> dist0(n); for (int i = 1; i < n; ++i) { dist0[i] = getDistance(0, i); } int a = max_element(dist0.begin(), dist0.end()) - dist0.begin(); vector<int> dist(n); dist[0] = dist0[a]; for (int i = 1; i < n; ++i) { dist[i] = i == a ? 0 : getDistance(i, a); } int b = max_element(dist.begin(), dist.end()) - dist.begin(); vector<int> pos(n); pos[0] = (dist0[a] - dist0[b] + dist[b]) >> 1; for (int i = 1; i < n; ++i) { pos[i] = min(pos[0], (dist0[a] - dist0[i] + dist[i]) >> 1); } int ans = numeric_limits<int>::max(); map<int, vector<int>> mp; for (int i = 0; i < n; ++i) { ans = min(ans, max(pos[i], dist[b] - pos[i])); mp[pos[i]].push_back(i); } int L = 0, R = n; for (auto [len, adj]: mp) { R -= adj.size(); if (max(len, dist[b] - len) == ans && max(L, R) <= n / 2) { if (adj.size() <= n / 2) { return ans; } vector<int> p(n); int bal = 0, x; for (int y: adj) { if (bal == 0) { x = y; } if (x == y || getDistance(x, y) < dist[x] + dist[y] - 2 * len) { p[y] = x, ++bal; } else { p[y] = y, --bal; } } int last = -1, cnt = 0; for (int y: adj) { if (p[y] == y && getDistance(x, y) < dist[x] + dist[y] - 2 * len) { last = y; } cnt += p[y] == last; } if (cnt <= n / 2) { return ans; } else { return -ans; } } L += adj.size(); } return -ans; }

Compilation message (stderr)

towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:12:53: warning: conversion from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
   12 |     int a = max_element(dist0.begin(), dist0.end()) - dist0.begin();
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
towns.cpp:20:51: warning: conversion from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
   20 |     int b = max_element(dist.begin(), dist.end()) - dist.begin();
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
towns.cpp:39:23: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   39 |         R -= adj.size();
      |                       ^
towns.cpp:42:28: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   42 |             if (adj.size() <= n / 2) {
      |                 ~~~~~~~~~~~^~~~~~~~
towns.cpp:73:23: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   73 |         L += adj.size();
      |                       ^
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...