Submission #296885

# Submission time Handle Problem Language Result Execution time Memory
296885 2020-09-11T03:42:20 Z tmwilliamlin168 Towns (IOI15_towns) C++14
100 / 100
20 ms 512 KB
#include "towns.h"
#include <bits/stdc++.h>
using namespace std;

const int mxN=110;
int d[mxN][mxN], b[mxN], c[mxN];

int qry(int i, int j) {
	if(i^j&&!d[i][j])
		d[i][j]=d[j][i]=getDistance(i, j);
	return d[i][j];
}

int hubDistance(int n, int sub) {
	memset(d, 0, sizeof(d));
	array<int, 2> a{};
	for(int i=1; i<n; ++i)
		a=max(array<int, 2>{qry(0, i), i}, a);
	int di=0, ans=1e6, sl=0, sr=n;
	for(int i=0; i<n; ++i)
		di=max(qry(a[1], i), di);
	map<int, vector<int>> mp;
	for(int i=0, c; i<n; ++i) {
		b[i]=(qry(0, i)+qry(a[1], i)-qry(0, a[1]))/2;
		c=qry(a[1], i)-b[i];
		mp[c].push_back(i);
		ans=min(max(c, di-c), ans);
	}
	for(auto p : mp) {
		sr-=p.second.size();
		if(max(p.first, di-p.first)==ans&&sl<=n/2&&sr<=n/2) {
			vector<vector<int>> u, v;
			for(int i : p.second) {
				if(u.size()&&u.back().size()>v.back().size()) {
					if(qry(u.back()[0], i)<b[u.back()[0]]+b[i])
						u.back().push_back(i);
					else
						v.back().push_back(i);
				} else {
					u.push_back({i});
					v.push_back({});
				}
			}
			int m=u.back().size();
			for(int i=0; i<u.size()-1; ++i) {
				if(qry(u.back()[0], u[i][0])<b[u.back()[0]]+b[u[i][0]])
					m+=u[i].size();
				else
					for(int j : v[i])
						m+=qry(u.back()[0], j)<b[u.back()[0]]+b[j];
			}
			if(m<=n/2)
				ans=-ans;
		}
		sl+=p.second.size();
	}
	return -ans;
}

Compilation message

towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:23:15: warning: declaration of 'c' shadows a global declaration [-Wshadow]
   23 |  for(int i=0, c; i<n; ++i) {
      |               ^
towns.cpp:6:26: note: shadowed declaration is here
    6 | int d[mxN][mxN], b[mxN], c[mxN];
      |                          ^
towns.cpp:30:21: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   30 |   sr-=p.second.size();
      |                     ^
towns.cpp:44:23: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   44 |    int m=u.back().size();
      |          ~~~~~~~~~~~~~^~
towns.cpp:45:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |    for(int i=0; i<u.size()-1; ++i) {
      |                 ~^~~~~~~~~~~
towns.cpp:47:19: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   47 |      m+=u[i].size();
      |                   ^
towns.cpp:55:21: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   55 |   sl+=p.second.size();
      |                     ^
towns.cpp:14:28: warning: unused parameter 'sub' [-Wunused-parameter]
   14 | int hubDistance(int n, int sub) {
      |                        ~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 18 ms 384 KB Output is correct
2 Correct 15 ms 512 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 20 ms 384 KB Output is correct
5 Correct 20 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 384 KB Output is correct
2 Correct 15 ms 384 KB Output is correct
3 Correct 20 ms 384 KB Output is correct
4 Correct 20 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 384 KB Output is correct
2 Correct 20 ms 512 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 20 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 384 KB Output is correct
2 Correct 20 ms 384 KB Output is correct
3 Correct 20 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 384 KB Output is correct
2 Correct 20 ms 384 KB Output is correct
3 Correct 20 ms 512 KB Output is correct