Submission #785562

#TimeUsernameProblemLanguageResultExecution timeMemory
785562fatemetmhr도시들 (IOI15_towns)C++17
35 / 100
13 ms672 KiB
//  ~ Be Name Khoda ~  //

#include "towns.h"
#include <bits/stdc++.h>
//#pragma GCC optimize ("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops,Ofast")

using namespace std;

typedef long long ll;

#define pb       push_back
#define mp       make_pair
#define all(x)   x.begin(), x.end()
#define fi       first
#define se       second

const int maxn  =  1e6   + 10;
const int maxn5 =  200;
const int maxnt =  1.2e6 + 10;
const int maxn3 =  1e3   + 10;
const int mod   =  1e9   + 7;
const ll  inf   =  1e18;


ll dis1[maxn5], dis2[maxn5], val[maxn5], keep[maxn5][maxn5];
ll len[maxn5];
vector <pair<int, int>> av;

ll gdis(int a, int b){
	if(a == b)
		return 0;
	if(keep[a][b] != -1)
		return keep[a][b];
	return keep[a][b] = keep[b][a] = getDistance(a, b);
}

int hubDistance(int n, int sub){
	memset(keep, -1, sizeof keep);
	int v = 0;
	dis1[0] = 0;
	for(int i = 1; i < n; i++){
		dis1[i] = gdis(0, i);
		if(dis1[i] >= dis1[v])
			v = i;
	}
	dis2[v] = 0;
	int u = v;
	for(int i = 0; i < n; i++){
		dis2[i] = gdis(v, i);
		if(dis2[i] >= dis2[u])
			u = i;
	}
	for(int i = 0; i < n; i++){
		dis1[i] = gdis(u, i);
	}
	int mnid = 0;
	for(int i = 0; i < n; i++){
		val[i] = abs(dis1[i] - dis2[i]);
		if(val[i] <= val[mnid])
			mnid = i;
	}
	for(int i = 0; i < n; i++)
		len[i] = (dis1[i] + dis2[i] - dis1[v]) / 2;
	ll R = max(dis1[mnid], dis2[mnid]) - len[mnid];
	if(sub <= 2)
		return R;
	ll x = R;
	int c = dis1[mnid] > dis2[mnid] ? u : v;
	if(c == v){
		for(int i = 0; i < n; i++)
			swap(dis1[i], dis2[i]);
		swap(u, v);
	}
	if(sub == 3){
		int num[3] = {0, 0, 0};
		vector <int> av, tmp;
		for(int i = 0; i < n; i++){
			if(dis1[i] - len[i] == x){
				num[0]++;
				av.pb(i);
			}
			else if(dis1[i] - len[i] > x)
				num[1]++;
			else if(dis1[i] - len[i] < x)
				num[2]++;
		}
		if(max({num[1], num[2]}) <= n / 2){
			while(true){
				if(av.size() <= n / 2)
					return R;
				num[0] = num[1] = 0;
				tmp.clear();
				int x = av.back();
				for(auto u : av){
					if(gdis(u, x) == len[u] + len[x])
						tmp.pb(u);
					else
						num[1]++;
				}
				if(num[1] > n / 2)
					break;
				av.clear();
				for(auto u : tmp)
					av.pb(u);
			}
		}
		bool re = false;
		for(int i = 0; i < n; i++){
			if(dis2[i] - len[i] == x && dis1[i] - len[i] > x)
				re = true;
		}
		if(re){
			int num[3] = {0, 0, 0};
			av.clear();
			tmp.clear();
			for(int i = 0; i < n; i++){
				if(dis2[i] - len[i] == x){
					num[0]++;
					av.pb(i);
				}
				else if(dis2[i] - len[i] > x)
					num[1]++;
				else if(dis2[i] - len[i] < x)
					num[2]++;
			}
			if(max({num[1], num[2]}) <= n / 2){
				while(true){
					if(av.size() <= n / 2)
						return R;
					num[0] = num[1] = 0;
					tmp.clear();
					int x = av.back();
					for(auto u : av){
						if(gdis(u, x) == len[u] + len[x])
							tmp.pb(u);
						else
							num[1]++;
					}
					if(num[1] > n / 2)
						break;
					av.clear();
					for(auto u : tmp)
						av.pb(u);
				}
			}
		}
		return -R;
	}
	int num[3] = {0, 0, 0};
	for(int i = 0; i < n; i++){
		ll len = (dis1[i] + dis2[i] - dis1[v]) / 2;
		if(dis1[i] - len == x)
			num[0]++;
		else if(dis1[i] - len > x)
			num[1]++;
		else if(dis1[i] - len < x)
			num[2]++;
	}
	if(max({num[0], num[1], num[2]}) <= n / 2)
		return R;
	bool re = false;
	for(int i = 0; i < n; i++){
		ll len = (dis1[i] + dis2[i] - dis1[v]) / 2;
		if(dis2[i] - len == x && dis1[i] - len > x)
			re = true;
	}
	if(re){
		int num[3] = {0, 0, 0};
		for(int i = 0; i < n; i++){
			ll len = (dis1[i] + dis2[i] - dis1[v]) / 2;
			if(dis2[i] - len == x)
				num[0]++;
			else if(dis2[i] - len > x)
				num[1]++;
			else if(dis2[i] - len < x)
				num[2]++;
		}
		if(max({num[0], num[1], num[2]}) <= n / 2)
			return R;
	}
	return -R;
}

Compilation message (stderr)

towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:68:10: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   68 |   return R;
      |          ^
towns.cpp:78:16: warning: declaration of 'av' shadows a global declaration [-Wshadow]
   78 |   vector <int> av, tmp;
      |                ^~
towns.cpp:29:25: note: shadowed declaration is here
   29 | vector <pair<int, int>> av;
      |                         ^~
towns.cpp:91:18: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   91 |     if(av.size() <= n / 2)
      |        ~~~~~~~~~~^~~~~~~~
towns.cpp:92:13: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   92 |      return R;
      |             ^
towns.cpp:95:9: warning: declaration of 'x' shadows a previous local [-Wshadow]
   95 |     int x = av.back();
      |         ^
towns.cpp:69:5: note: shadowed declaration is here
   69 |  ll x = R;
      |     ^
towns.cpp:96:14: warning: declaration of 'u' shadows a previous local [-Wshadow]
   96 |     for(auto u : av){
      |              ^
towns.cpp:49:6: note: shadowed declaration is here
   49 |  int u = v;
      |      ^
towns.cpp:105:14: warning: declaration of 'u' shadows a previous local [-Wshadow]
  105 |     for(auto u : tmp)
      |              ^
towns.cpp:49:6: note: shadowed declaration is here
   49 |  int u = v;
      |      ^
towns.cpp:115:8: warning: declaration of 'num' shadows a previous local [-Wshadow]
  115 |    int num[3] = {0, 0, 0};
      |        ^~~
towns.cpp:77:7: note: shadowed declaration is here
   77 |   int num[3] = {0, 0, 0};
      |       ^~~
towns.cpp:130:19: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  130 |      if(av.size() <= n / 2)
      |         ~~~~~~~~~~^~~~~~~~
towns.cpp:131:14: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  131 |       return R;
      |              ^
towns.cpp:134:10: warning: declaration of 'x' shadows a previous local [-Wshadow]
  134 |      int x = av.back();
      |          ^
towns.cpp:69:5: note: shadowed declaration is here
   69 |  ll x = R;
      |     ^
towns.cpp:135:15: warning: declaration of 'u' shadows a previous local [-Wshadow]
  135 |      for(auto u : av){
      |               ^
towns.cpp:49:6: note: shadowed declaration is here
   49 |  int u = v;
      |      ^
towns.cpp:144:15: warning: declaration of 'u' shadows a previous local [-Wshadow]
  144 |      for(auto u : tmp)
      |               ^
towns.cpp:49:6: note: shadowed declaration is here
   49 |  int u = v;
      |      ^
towns.cpp:149:10: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  149 |   return -R;
      |          ^~
towns.cpp:153:6: warning: declaration of 'len' shadows a global declaration [-Wshadow]
  153 |   ll len = (dis1[i] + dis2[i] - dis1[v]) / 2;
      |      ^~~
towns.cpp:28:4: note: shadowed declaration is here
   28 | ll len[maxn5];
      |    ^~~
towns.cpp:162:10: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  162 |   return R;
      |          ^
towns.cpp:165:6: warning: declaration of 'len' shadows a global declaration [-Wshadow]
  165 |   ll len = (dis1[i] + dis2[i] - dis1[v]) / 2;
      |      ^~~
towns.cpp:28:4: note: shadowed declaration is here
   28 | ll len[maxn5];
      |    ^~~
towns.cpp:170:7: warning: declaration of 'num' shadows a previous local [-Wshadow]
  170 |   int num[3] = {0, 0, 0};
      |       ^~~
towns.cpp:151:6: note: shadowed declaration is here
  151 |  int num[3] = {0, 0, 0};
      |      ^~~
towns.cpp:172:7: warning: declaration of 'len' shadows a global declaration [-Wshadow]
  172 |    ll len = (dis1[i] + dis2[i] - dis1[v]) / 2;
      |       ^~~
towns.cpp:28:4: note: shadowed declaration is here
   28 | ll len[maxn5];
      |    ^~~
towns.cpp:181:11: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  181 |    return R;
      |           ^
towns.cpp:183:9: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  183 |  return -R;
      |         ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...