Submission #119520

# Submission time Handle Problem Language Result Execution time Memory
119520 2019-06-21T10:50:15 Z PeppaPig Towns (IOI15_towns) C++14
100 / 100
25 ms 664 KB
#include "towns.h"
#include <bits/stdc++.h>

using namespace std;

const int N = 205;

int hubDistance(int n, int sub) {
	vector<vector<int> > d(N, vector<int>(N, 0));
	auto get_dist = [&](int a, int b) {
		if(a == b) return 0;
		else if(d[a][b]) return d[a][b];
		else return d[a][b] = d[b][a] = getDistance(a, b);
	};	

	int a, b;
	for(int i = 0, mx = -1; i < n; i++) if(get_dist(0, i) > mx) a = i, mx = get_dist(0, i);
	for(int i = 0, mx = -1; i < n; i++) if(get_dist(a, i) > mx) b = i, mx = get_dist(a, i);

	int root1 = -1, root2 = -1, ans = INT_MAX;
	for(int i = 0; i < n; i++) {
		int pos = (get_dist(a, i) + get_dist(a, 0) - get_dist(i, 0)) / 2;
		int now = max(pos, get_dist(a, b) - pos);
		if(now <= ans) {
			if(now < ans) root1 = pos, root2 = -1;
			else {
				if(root1 == -1) root1 = pos;
				else if(pos != root1) root2 = pos;
			}
			ans = now;
		}
	}

	auto chk = [&](int root) {
		int l = 0, r = 0;
		for(int i = 0; i < n; i++) {
			int pos = (get_dist(a, i) + get_dist(a, 0) - get_dist(i, 0)) / 2;
			if(pos < root) ++l;
			if(pos > root) ++r;
		}
		return (l <= n / 2 && r <= n / 2);
	};
	if(!chk(root1)) root1 = -1;
	if(root2 == -1 || !chk(root2)) root2 = -1;
	if(root1 == -1 && root2 == -1) return -ans;

	if(root1 < root2) swap(root1, root2);

	auto same = [&](int x, int y) {
		if(x == y) return true;
		int d = get_dist(a, x) + get_dist(0, y) - get_dist(a, 0);
		return d != get_dist(x, y);
	};

	auto vote = [&](int root) {
		int last = -1, cnt = 0, ret, sz = 0;
		bool sm = false;
		vector<int> v;
		for(int i = 0; i < n; i++) {
			int pos = (get_dist(a, i) + get_dist(a, 0) - get_dist(i, 0)) / 2;
			if(pos == root) v.emplace_back(i);
		}
		for(int i = 0; i < v.size(); i++) {
			if(last == -1) last = v[i], ++cnt;
			else if(same(v[i], last)) ++cnt;
			else if(--cnt == 0) last = -1;
		}
		if(!cnt) return true;
		ret = last, last = -1, cnt = 0;
		for(int i = 0; i < v.size(); i++) {
			if(last == -1) {
				if(same(v[i], ret)) sm = true, ++sz;
				last = v[i], ++cnt;
			} else if(same(v[i], last)) sz += sm, ++cnt;
			else {
				if(!sm && same(v[i], ret)) ++sz;
				if(--cnt == 0) last = -1, sm = false;
			}
		}
		return sz <= n / 2;
	};

	if(!vote(root1) && (root2 == -1 || !vote(root2))) return -ans;
	else return ans;
}

Compilation message

towns.cpp: In lambda function:
towns.cpp:51:7: warning: declaration of 'd' shadows a previous local [-Wshadow]
   int d = get_dist(a, x) + get_dist(0, y) - get_dist(a, 0);
       ^
towns.cpp:9:23: note: shadowed declaration is here
  vector<vector<int> > d(N, vector<int>(N, 0));
                       ^
towns.cpp: In lambda function:
towns.cpp:63:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < v.size(); i++) {
                  ~~^~~~~~~~~~
towns.cpp:70:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < v.size(); i++) {
                  ~~^~~~~~~~~~
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:8:28: warning: unused parameter 'sub' [-Wunused-parameter]
 int hubDistance(int n, int sub) {
                            ^~~
towns.cpp:11:3: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
   if(a == b) return 0;
   ^~
# Verdict Execution time Memory Grader output
1 Correct 17 ms 664 KB Output is correct
2 Correct 15 ms 580 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 24 ms 584 KB Output is correct
5 Correct 25 ms 580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 580 KB Output is correct
2 Correct 16 ms 560 KB Output is correct
3 Correct 18 ms 608 KB Output is correct
4 Correct 18 ms 616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 624 KB Output is correct
2 Correct 19 ms 616 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 18 ms 580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 580 KB Output is correct
2 Correct 18 ms 576 KB Output is correct
3 Correct 18 ms 580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 608 KB Output is correct
2 Correct 18 ms 616 KB Output is correct
3 Correct 18 ms 576 KB Output is correct