제출 #623937

#제출 시각아이디문제언어결과실행 시간메모리
6239378e7Towns (IOI15_towns)C++17
13 / 100
13 ms452 KiB
//Challenge: Accepted
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ...b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
	while (l != r) cout << *l << " ", l++;
	cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 155
#define pii pair<int, int>
#define ff first
#define ss second
#include "towns.h"


int n;
int save[maxn][maxn];
bool done[maxn][maxn];
int query(int u, int v) {
	if (u == v || u >= n || v >= n) return 0;
	if (done[u][v]) return save[u][v];
	save[u][v] = save[v][u] = getDistance(u, v);
	done[u][v] = done[v][u] = 1;
	return save[u][v];
}
int hubDistance(int N, int sub) {
	n = N;
	for (int i = 0;i < n;i++) {
		for (int j = 0;j < n;j++) done[i][j] = 0;
	}
	vector<int> da, db, l(n), w(n), ws;
	int b = 0, ma = 0;
	for (int i = 0;i < n;i++) {
		da.push_back(query(0, i));	
		if (da.back() > ma) {
			ma = da.back();
			b = i;
		}
	}
	int len = ma;
	for (int i = 0;i < n;i++) {
		db.push_back(query(b, i));
		ma = max(ma, db.back());
	}
	//debug(ma);
	
	int R = 1<<30;
	for (int i = 0;i < n;i++) {
		l[i] = (da[i] + db[i] - len) / 2;	
		w[i] = db[i] - l[i];
		//debug(l[i], w[i]);
		ws.push_back(w[i]);
		R = min(R, max(w[i], ma - w[i]));
	}
	sort(ws.begin(), ws.end());
	ws.resize(int(unique(ws.begin(), ws.end()) - ws.begin()));
	int siz = ws.size();
	vector<vector<int> > col(siz, vector<int>());
	vector<int> pref(siz, 0), suf(siz, 0);
	for (int i = 0;i < n;i++) {
		int ind = lower_bound(ws.begin(), ws.end(), w[i]) - ws.begin();	
		col[ind].push_back(i);
		debug(i, ind);
	}
	pary(ws.begin(), ws.end());
	for (int i = 0;i < siz;i++) pref[i] = col[i].size() + (i ? pref[i-1] : 0);	
	for (int i = siz-1;i >= 0;i--) suf[i] = col[i].size() + (i < siz-1 ? suf[i+1] : 0);

	vector<int> vec, tmp;
	for (int i = 0;i < siz;i++) {
		int v = max(ws[i], ma - ws[i]);
		if (v == R) {
			if ((i == 0 || pref[i-1] <= n/2) && (i == siz-1 || suf[i+1] <= n/2) && col[i].size() <= n/2) {
				return R;
			} else if (col[i].size() > n/2) {
				vec = col[i];
				for (int j = 0;j < n - col[i].size();j++) vec.push_back(n+j);
			}
		}
	}
	tmp = vec;
	//pary(vec.begin(), vec.end());

	auto same = [&] (int i, int j) {
		int v = query(i, j);
		if (v == 0) return 0;
		int d = l[i] + l[j] - v;
		return d ? 1 : 0;
	};
	pary(vec.begin(), vec.end());
	while (vec.size() > 1) {
		vector<int> nxt;
		for (int i = 0;i < vec.size();i += 2) {
			if (i == (int)vec.size()-1) {
				if (nxt.size() == 0) nxt.push_back(vec[i]);
			} else {
				if (same(vec[i], vec[i+1])) nxt.push_back(vec[i]); 
			}
		}
		pary(nxt.begin(), nxt.end());
		vec = nxt;
	}
	if (vec.size() == 0) return R;
	else {
		int c = vec[0];
		int cnt = 0;
		for (int i = 0;i < n;i++) {
			if (tmp[i] == c) {
				cnt++;
			} else {
				if (same(c, tmp[i])) cnt++;
			}
		}
		//debug(c, cnt);
		if (cnt > n/2) return -R;
		else return R;
	}
}

컴파일 시 표준 에러 (stderr) 메시지

towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:64:19: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   64 |  int siz = ws.size();
      |            ~~~~~~~^~
towns.cpp:68:53: warning: conversion from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
   68 |   int ind = lower_bound(ws.begin(), ws.end(), w[i]) - ws.begin();
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
towns.cpp:12:20: warning: statement has no effect [-Wunused-value]
   12 | #define debug(...) 0
      |                    ^
towns.cpp:70:3: note: in expansion of macro 'debug'
   70 |   debug(i, ind);
      |   ^~~~~
towns.cpp:13:19: warning: statement has no effect [-Wunused-value]
   13 | #define pary(...) 0
      |                   ^
towns.cpp:72:2: note: in expansion of macro 'pary'
   72 |  pary(ws.begin(), ws.end());
      |  ^~~~
towns.cpp:73:54: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} may change value [-Wconversion]
   73 |  for (int i = 0;i < siz;i++) pref[i] = col[i].size() + (i ? pref[i-1] : 0);
      |                                        ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
towns.cpp:74:56: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} may change value [-Wconversion]
   74 |  for (int i = siz-1;i >= 0;i--) suf[i] = col[i].size() + (i < siz-1 ? suf[i+1] : 0);
      |                                          ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
towns.cpp:80:89: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   80 |    if ((i == 0 || pref[i-1] <= n/2) && (i == siz-1 || suf[i+1] <= n/2) && col[i].size() <= n/2) {
      |                                                                           ~~~~~~~~~~~~~~^~~~~~
towns.cpp:82:29: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   82 |    } else if (col[i].size() > n/2) {
      |               ~~~~~~~~~~~~~~^~~~~
towns.cpp:84:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |     for (int j = 0;j < n - col[i].size();j++) vec.push_back(n+j);
      |                    ~~^~~~~~~~~~~~~~~~~~~
towns.cpp:13:19: warning: statement has no effect [-Wunused-value]
   13 | #define pary(...) 0
      |                   ^
towns.cpp:97:2: note: in expansion of macro 'pary'
   97 |  pary(vec.begin(), vec.end());
      |  ^~~~
towns.cpp:100:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |   for (int i = 0;i < vec.size();i += 2) {
      |                  ~~^~~~~~~~~~~~
towns.cpp:13:19: warning: statement has no effect [-Wunused-value]
   13 | #define pary(...) 0
      |                   ^
towns.cpp:107:3: note: in expansion of macro 'pary'
  107 |   pary(nxt.begin(), nxt.end());
      |   ^~~~
towns.cpp:33:28: warning: unused parameter 'sub' [-Wunused-parameter]
   33 | int hubDistance(int N, int sub) {
      |                        ~~~~^~~
#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...