Submission #553903

# Submission time Handle Problem Language Result Execution time Memory
553903 2022-04-27T09:34:55 Z keta_tsimakuridze Towns (IOI15_towns) C++14
100 / 100
16 ms 960 KB
#include "towns.h"
#include<bits/stdc++.h>
using namespace std;
#define ll long long				
#define pii pair<int,int>
#define f first
#define s second
const int N = 505;
ll d[2][N], dist[N][N];
map<int,int> cnt;
map<int,vector<int> > c;
int hubDistance(int n, int sub) {
	int U = 0, V = 0;
	for(int i = 0; i < n; i++) {
		d[0][i] = getDistance(0, i); 
		if(d[0][i] >= d[0][U]) U = i;
	}
	for(int i = 0; i < n; i++) {
		d[1][i] = getDistance(U, i);
		
		if(d[1][i] >= d[1][V]) V = i;
	}
	vector<ll> v;
	ll Distance = 0;
	cnt.clear();
	vector<int> f(n + 5);
	for(int i = 0; i < n; i++) {
		ll D = (d[1][i] + d[0][i] - d[0][U]) / 2;
		D = d[1][i] - D;
		v.push_back(D);
		cnt[D]++;
		f[i] = D;
		if(i == V) Distance = D;
	}
	sort(v.begin(), v.end());
	v.erase(unique(v.begin(), v.end()), v.end());
	ll R = 1e18, cnR = 0;
	for(int i = 0; i < v.size(); i++) {
		if(v[i] > Distance) break;
		ll x = (d[1][V] + d[0][V] - d[0][U]) / 2 + Distance - v[i];
		R = min(R, max(v[i], x));
	}
	for(int i = 0; i < v.size(); i++) {
		if(v[i] > Distance) break;
		ll x = (d[1][V] + d[0][V] - d[0][U]) / 2 + Distance - v[i];
		if(R == max(v[i], x)) {
			if(cnR <= n / 2 && cnt[v[i]] <= n / 2 && n - cnR - cnt[v[i]] <= n / 2) return R;
			
			if(cnR <= n / 2 && n - cnR - cnt[v[i]] <= n / 2) {
				vector<int> a, b;
				
				for(int j = 0; j < n; j++) {
					if(!a.size()) a.push_back(j);
					else {
						int u = a.back(), V = j;
						
						if(f[u] == v[i] && f[V] == v[i] && getDistance(u, V) < d[1][u] + d[1][V] - 2 * v[i]) {
							b.push_back(j);
						}
						else {
							a.push_back(j);
							if(b.size()) a.push_back(b.back()), b.pop_back();
						}
					}
				}
				
				int V = a.back(), F = 0;
				while(a.size() > 1) {
					int u = a.back();
					if(f[u] == v[i] && f[V] == v[i] && getDistance(u, V)  < d[1][u] + d[1][V] - 2 * v[i]) {
						a.pop_back();
						a.pop_back();
					} else if(b.size()) {
						a.pop_back(); 
						b.pop_back();
					}	
					else {
						F = 1;
						break;
					}
				}
				if(b.size()) return -R;
				if(F || !a.size()) return R;
				return -R;
			}
		}
		cnR += cnt[v[i]];
	}
	return -R;
}

Compilation message

towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:31:7: warning: conversion from 'long long int' to 'std::map<int, int>::key_type' {aka 'int'} may change value [-Wconversion]
   31 |   cnt[D]++;
      |       ^
towns.cpp:32:10: warning: conversion from 'long long int' to '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} may change value [-Wconversion]
   32 |   f[i] = D;
      |          ^
towns.cpp:38:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |  for(int i = 0; i < v.size(); i++) {
      |                 ~~^~~~~~~~~~
towns.cpp:43:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |  for(int i = 0; i < v.size(); i++) {
      |                 ~~^~~~~~~~~~
towns.cpp:47:31: warning: conversion from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'std::map<int, int>::key_type' {aka 'int'} may change value [-Wconversion]
   47 |    if(cnR <= n / 2 && cnt[v[i]] <= n / 2 && n - cnR - cnt[v[i]] <= n / 2) return R;
      |                               ^
towns.cpp:47:63: warning: conversion from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'std::map<int, int>::key_type' {aka 'int'} may change value [-Wconversion]
   47 |    if(cnR <= n / 2 && cnt[v[i]] <= n / 2 && n - cnR - cnt[v[i]] <= n / 2) return R;
      |                                                               ^
towns.cpp:47:82: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   47 |    if(cnR <= n / 2 && cnt[v[i]] <= n / 2 && n - cnR - cnt[v[i]] <= n / 2) return R;
      |                                                                                  ^
towns.cpp:49:41: warning: conversion from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'std::map<int, int>::key_type' {aka 'int'} may change value [-Wconversion]
   49 |    if(cnR <= n / 2 && n - cnR - cnt[v[i]] <= n / 2) {
      |                                         ^
towns.cpp:55:25: warning: declaration of 'V' shadows a previous local [-Wshadow]
   55 |       int u = a.back(), V = j;
      |                         ^
towns.cpp:13:13: note: shadowed declaration is here
   13 |  int U = 0, V = 0;
      |             ^
towns.cpp:67:9: warning: declaration of 'V' shadows a previous local [-Wshadow]
   67 |     int V = a.back(), F = 0;
      |         ^
towns.cpp:13:13: note: shadowed declaration is here
   13 |  int U = 0, V = 0;
      |             ^
towns.cpp:82:25: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   82 |     if(b.size()) return -R;
      |                         ^~
towns.cpp:83:31: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   83 |     if(F || !a.size()) return R;
      |                               ^
towns.cpp:84:12: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   84 |     return -R;
      |            ^~
towns.cpp:87:18: warning: conversion from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'std::map<int, int>::key_type' {aka 'int'} may change value [-Wconversion]
   87 |   cnR += cnt[v[i]];
      |                  ^
towns.cpp:89:9: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   89 |  return -R;
      |         ^~
towns.cpp:12:28: warning: unused parameter 'sub' [-Wunused-parameter]
   12 | int hubDistance(int n, int sub) {
      |                        ~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 852 KB Output is correct
2 Correct 10 ms 724 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 14 ms 868 KB Output is correct
5 Correct 13 ms 832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 324 KB Output is correct
2 Correct 10 ms 736 KB Output is correct
3 Correct 13 ms 484 KB Output is correct
4 Correct 14 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 740 KB Output is correct
2 Correct 13 ms 860 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 15 ms 864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 412 KB Output is correct
2 Correct 14 ms 960 KB Output is correct
3 Correct 15 ms 832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 340 KB Output is correct
2 Correct 16 ms 900 KB Output is correct
3 Correct 14 ms 832 KB Output is correct