Submission #400255

# Submission time Handle Problem Language Result Execution time Memory
400255 2021-05-07T18:36:01 Z peuch Towns (IOI15_towns) C++17
100 / 100
28 ms 1660 KB
#include "towns.h"
#include<bits/stdc++.h>
using namespace std;
 
const int MAXN = 310;
 
int dist[MAXN][MAXN];
int marc[MAXN][MAXN];
int p1, p2, n;
int cnt; 
 
int getD(int i, int j){
	if(marc[i][j]) return dist[i][j];
	marc[i][j] = marc[j][i] = 1;
	if(i == j) return dist[i][j] = dist[j][i] = 0;
	cnt++;
	return dist[i][j] = dist[j][i] = getDistance(i, j);
}
 
int getSize(vector<int> values, int goal){
	vector<multiset<int> > alive, dead;
	for(int i = 0; i < values.size(); i++){
		multiset<int> aux;
		aux.insert(values[i]);
		alive.push_back(aux);
	}
	int k = -1;
	if(values.size() % 2 == 1){
		k = values.back();
		alive.pop_back();
	}
	while(alive.size() > 1){
		if(alive.size() % 2 == 1) {
			dead.push_back(alive.back());
			alive.pop_back();
		}
		vector<multiset<int> > aux;
		for(int i = 0; i < alive.size(); i += 2){
			multiset<int> g1, g2;
			g1 = alive[i];
			g2 = alive[i + 1];
			int v1 = *g1.begin(), v2 = *g2.begin();
			int x = (getD(p1, v1) + getD(p1, v2) - getD(v1, v2)) / 2;
			if(x != goal){
				while(!g2.empty()){
					g1.insert(*g2.begin());
					g2.erase(g2.begin());
				}
				aux.push_back(g1);
			}
			else{
				dead.push_back(g1);
				dead.push_back(g2);
			}
		}
		alive = aux;
	}
	if(alive.empty()){
		if(k == -1) return 1;
		multiset<int> aux;
		aux.insert(k);
		alive.push_back(aux);
	}
	else{
		if(k != -1){
			multiset<int> aux;
			aux.insert(k);
			dead.push_back(aux);
		}
	}
	for(int i = 0; i < dead.size(); i++){
		multiset<int> g1, g2;
		g1 = alive[0];
		g2 = dead[i];
		int v1 = *g1.begin(), v2 = *g2.begin();
		int x = (getD(p1, v1) + getD(p1, v2) - getD(v1, v2)) / 2;
		if(x != goal){
			while(!g2.empty()){
				g1.insert(*g2.begin());
				g2.erase(g2.begin());
			}
			alive[0] = g1;
		}
	}
	return (alive[0].size() > n / 2) ? -1 : 1;
}
 
int hubDistance(int N, int sub) {
	srand(time(0));
	n = N;
	p1 = 0, p2 = 0;
	int maxi = 0;
	cnt = 0;
	memset(dist, 0, sizeof(dist));
	memset(marc, 0, sizeof(marc));
	
	for(int i = 0; i < N; i++){
		int aux = getD(0, i);
		if(aux > maxi) p1 = i, maxi = aux;
	}
	maxi = 0;
	for(int i = 0; i < N; i++)
		if(getD(p1, i) > maxi) maxi = getD(p1, i), p2 = i;
	
	int diam = getD(p1, p2);
	int cruz0 = (diam + getD(p1, 0) - getD(p2, 0)) / 2;
	int ans = max(cruz0, diam - cruz0);
	for(int i = 0; i < N; i++){
		int x = (getD(p1, 0) + getD(p1, i) - getD(0, i)) / 2;
		ans = min(ans, max(x, diam - x));
	}
	
	if(sub <= 2) return ans;
	int lSize = 0, rSize = 0;
	vector<int> lMid, rMid;
	for(int i = 0; i < N; i++){
		int x = (getD(p1, 0) + getD(p1, i) - getD(0, i)) / 2;
		if(x < min(ans, diam - ans)) lSize++;
		else if(x == min(ans, diam - ans)) lMid.push_back(i); 
		else if(x == max(ans, diam - ans)) rMid.push_back(i);
		else rSize++;
	}
	
	if(lSize > N / 2 || rSize > N / 2) return -ans;
	else if(lMid.size() > N / 2)
		return getSize(lMid, min(ans, diam - ans)) * ans;
	else if(rMid.size() > N / 2)
		return getSize(rMid, max(ans, diam - ans)) * ans;
	return ans;
}

Compilation message

towns.cpp: In function 'int getSize(std::vector<int>, int)':
towns.cpp:22:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |  for(int i = 0; i < values.size(); i++){
      |                 ~~^~~~~~~~~~~~~~~
towns.cpp:38:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::multiset<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |   for(int i = 0; i < alive.size(); i += 2){
      |                  ~~^~~~~~~~~~~~~~
towns.cpp:71:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::multiset<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |  for(int i = 0; i < dead.size(); i++){
      |                 ~~^~~~~~~~~~~~~
towns.cpp:85:26: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   85 |  return (alive[0].size() > n / 2) ? -1 : 1;
      |          ~~~~~~~~~~~~~~~~^~~~~~~
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:89:12: warning: conversion from 'time_t' {aka 'long int'} to 'unsigned int' may change value [-Wconversion]
   89 |  srand(time(0));
      |        ~~~~^~~
towns.cpp:125:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  125 |  else if(lMid.size() > N / 2)
      |          ~~~~~~~~~~~~^~~~~~~
towns.cpp:127:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  127 |  else if(rMid.size() > N / 2)
      |          ~~~~~~~~~~~~^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 20 ms 1100 KB Output is correct
2 Correct 16 ms 1096 KB Output is correct
3 Correct 1 ms 972 KB Output is correct
4 Correct 22 ms 1096 KB Output is correct
5 Correct 23 ms 1104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 1092 KB Output is correct
2 Correct 16 ms 1100 KB Output is correct
3 Correct 22 ms 1100 KB Output is correct
4 Correct 22 ms 1100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 1136 KB Output is correct
2 Correct 24 ms 1660 KB Output is correct
3 Correct 1 ms 972 KB Output is correct
4 Correct 24 ms 1612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 1224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 1124 KB Output is correct
2 Correct 25 ms 1624 KB Output is correct
3 Correct 24 ms 1648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 1108 KB Output is correct
2 Correct 25 ms 1228 KB Output is correct
3 Correct 28 ms 1640 KB Output is correct