Submission #883764

# Submission time Handle Problem Language Result Execution time Memory
883764 2023-12-06T00:25:45 Z MilosMilutinovic Towns (IOI15_towns) C++14
23 / 100
14 ms 512 KB
#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]);
        }
      }
      swap(ids, new_ids);
    }
    int e = max_element(sz.begin(), sz.end()) - sz.begin();
    iota(par.begin(), par.end(), 0);
    sz = vector<int>(n, 1);
    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

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 lambda function:
towns.cpp:114:47: warning: conversion from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
  114 |     int e = max_element(sz.begin(), sz.end()) - sz.begin();
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
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 time Memory Grader output
1 Correct 11 ms 344 KB Output is correct
2 Correct 9 ms 500 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 14 ms 348 KB Output is correct
5 Correct 14 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 348 KB Output is correct
2 Correct 9 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 348 KB Output is correct
2 Incorrect 14 ms 504 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 348 KB Output is correct
2 Incorrect 14 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 512 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -