Submission #805625

# Submission time Handle Problem Language Result Execution time Memory
805625 2023-08-03T19:02:36 Z FatihSolak Towns (IOI15_towns) C++17
0 / 100
2 ms 468 KB
#include "towns.h"
#include <bits/stdc++.h>
using namespace std;
int hubDistance(int N, int sub) {
	map<int,int> d[N];
	auto get = [&](int a,int b){
		if(a > b)
			swap(a,b);
		if(d[a].count(b))
			return d[a][b];
		if(a == b)
			return 0;
		return d[a][b] = getDistance(a,b);
	};
	int v = 0;
	for(int i = 0;i<N;i++){
		if(get(0,i) > get(0,v)){
			v = i;
		}
	}
	int x = 0;
	for(int i = 0;i<N;i++){
		if(get(v,i) > get(v,x)){
			x = i;
		}
	}
	int len = get(v,x);
	// cout << v << ' ' << x << ' ' << len << endl;
	map<int,vector<int>> mp;
	for(int i = 0;i<N;i++){
		int val = (get(0,i) + get(0,v) - get(v,i))/2;
		mp[val].push_back(i);
	}
	int val1 = 0;
	int val2 = 2e9;
	int ans;
	for(auto u:mp){
		int tmp = get(0,v) - u.first;
		// cout << tmp << endl;
		if(tmp <= len/2){
			val1 = max(val1,tmp);
		}
		if((len + 1)/2 <= tmp){
			val2 = min(val2,tmp);
		}
	}
	// cout << val1 << ' ' << val2 << endl;
	ans = len - val1;
	if(val2 != len - val1){
		if(val2 < len - val1){
			ans = val2;
			val1 = -1;
		}
		else val2 = -1;
	}
	int cnt = 0;
	for(auto u:mp){
		int tmp = get(0,v) - u.first;
		if(tmp == val1){
			if(cnt > N/2 || (N-cnt-u.second.size()) > N/2){
				val1 = -1;
			}
		}
		if(tmp == val2){
			if(cnt > N/2 || (N-cnt-u.second.size()) > N/2){
				val2 = -1;
			}
		}
		cnt += u.second.size();
	}
	if(val1 == -1 && val2 == -1)
		return -ans;
	if(val1 == -1)
		val1 = val2;
	vector<int> a = mp[-(val1 - get(0,v))];
	vector<int> v1,v2;
	for(auto u:a){
		if(v1.empty() || get(u,v1.back()) == get(v,u) + get(v,v1.back()) - 2 * val1){
			v1.push_back(u);
			if(v2.size()){
				v1.push_back(v2.back());
				v2.pop_back();
			}
		}
		else v2.push_back(u);
	}
	if(v2.empty())
		return -ans;
	cnt = 0;
	for(auto u:a){
		if(get(u,v2[0]) != get(v,u) + get(v,v2[0]) - 2 * val1){
			cnt++;
		}
	}
	// return ans;
	if(cnt <= N/2)
		return ans;
}

Compilation message

towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:60:44: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   60 |    if(cnt > N/2 || (N-cnt-u.second.size()) > N/2){
      |                    ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
towns.cpp:65:44: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   65 |    if(cnt > N/2 || (N-cnt-u.second.size()) > N/2){
      |                    ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
towns.cpp:69:24: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   69 |   cnt += u.second.size();
      |                        ^
towns.cpp:4:28: warning: unused parameter 'sub' [-Wunused-parameter]
    4 | int hubDistance(int N, int sub) {
      |                        ~~~~^~~
towns.cpp:98:1: warning: control reaches end of non-void function [-Wreturn-type]
   98 | }
      | ^
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 468 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -