Submission #640461

#TimeUsernameProblemLanguageResultExecution timeMemory
640461piOOETowns (IOI15_towns)C++17
25 / 100
21 ms476 KiB
#include <bits/stdc++.h>
#include "towns.h"

using namespace std;

int hubDistance(int n, int sub) {
    if (sub <= 0) {
        vector<int> dist(n);
        for (int i = 0; i < n; ++i) {
            dist[i] = getDistance(i, 0);
        }
        int v = max_element(dist.begin(), dist.end()) - dist.begin();
        for (int i = 0; i < n; ++i) {
            dist[i] = getDistance(i, v);
        }
        int u = max_element(dist.begin(), dist.end()) - dist.begin();
        vector<int> dist2(n);
        for (int i = 0; i < n; ++i) {
            dist2[i] = getDistance(i, u);
        }
        int ans = numeric_limits<int>::max();
        for (int i = 0; i < n; ++i) {
            int e = dist[i] + dist2[i] - dist[u] >> 1;
            ans = min(ans, max(dist[i] - e, dist2[i] - e));
        }
        return ans;
    } else {
        vector<int> dist(n);
        for (int i = 0; i < n; ++i) {
            dist[i] = getDistance(i, 0);
        }
        int v = max_element(dist.begin(), dist.end()) - dist.begin();
        for (int i = 0; i < n; ++i) {
            dist[i] = getDistance(i, v);
        }
        int u = max_element(dist.begin(), dist.end()) - dist.begin();
        vector<int> dist2(n);
        for (int i = 0; i < n; ++i) {
            dist2[i] = getDistance(i, u);
        }
        int ans = numeric_limits<int>::max();
        for (int i = 0; i < n; ++i) {
            int e = dist[i] + dist2[i] - dist[u] >> 1;
            ans = min(ans, max(dist[i] - e, dist2[i] - e));
        }
        vector<int> mega;
        for (int i = 0; i < n; ++i) {
            if (i != u && i != v) {
                int e = dist[i] + dist2[i] - dist[u] >> 1;
                mega.push_back(dist[i] - e);
            }
        }
        sort(mega.begin(), mega.end());
        mega.resize(unique(mega.begin(), mega.end()) - mega.begin());
        const int m = mega.size();
        vector<int> adj(n), cnt(m);
        for (int i = 0; i < n; ++i) {
            if (i != u && i != v) {
                int e = dist[i] + dist2[i] - dist[u] >> 1;
                adj[i] = lower_bound(mega.begin(), mega.end(), dist[i] - e) - mega.begin();
                assert(mega[adj[i]] == dist[i] - e);
                ++cnt[adj[i]];
            }
        }
        for (int i = 0; i < m; ++i) {
            if (max(mega[i], dist[u] - mega[i]) == ans) {
//                cout << "yay: " << i << endl;
                int L = 1, R = 1;
                for (int j = 0; j < i; ++j) {
                    L += cnt[j];
                }
                for (int j = i + 1; j < m; ++j) {
                    R += cnt[j];
                }
                if (max({L, R, cnt[i]}) <= n / 2) {
                    return -ans;
                }
            }
        }
        return ans;
    }
}

Compilation message (stderr)

towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:12:55: warning: conversion from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
   12 |         int v = max_element(dist.begin(), dist.end()) - dist.begin();
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
towns.cpp:16:55: warning: conversion from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
   16 |         int u = max_element(dist.begin(), dist.end()) - dist.begin();
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
towns.cpp:23:40: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
   23 |             int e = dist[i] + dist2[i] - dist[u] >> 1;
towns.cpp:32:55: warning: conversion from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
   32 |         int v = max_element(dist.begin(), dist.end()) - dist.begin();
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
towns.cpp:36:55: warning: conversion from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
   36 |         int u = max_element(dist.begin(), dist.end()) - dist.begin();
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
towns.cpp:43:40: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
   43 |             int e = dist[i] + dist2[i] - dist[u] >> 1;
towns.cpp:49:44: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
   49 |                 int e = dist[i] + dist2[i] - dist[u] >> 1;
towns.cpp:55:32: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   55 |         const int m = mega.size();
      |                       ~~~~~~~~~^~
towns.cpp:59:44: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
   59 |                 int e = dist[i] + dist2[i] - dist[u] >> 1;
towns.cpp:60:77: warning: conversion from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} may change value [-Wconversion]
   60 |                 adj[i] = lower_bound(mega.begin(), mega.end(), dist[i] - e) - mega.begin();
      |                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
#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...