Artifact Content
Not logged in

Artifact 4fdd1dfbd4a03197e6909aa335dbb3a05be49020


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

struct UnionFind
{
	vector<int> uf, sz;
	int nc;

	UnionFind(int N): uf(N), sz(N,1), nc(N)
		{ for(int i=0; i<N; ++i) uf[i] = i; }
	int size()
		{ return nc; }
	int size(int a)
		{ return sz[Find(a)]; }
	int Find(int a)
		{ return uf[a]==a ? a : uf[a]=Find(uf[a]); }
	bool Union(int a, int b)
		{
			a = Find(a);
			b = Find(b);
			if( a != b )
			{
				if( sz[a] >= sz[b] ) swap(a, b);
				uf[a] = b;
				sz[b] += sz[a];
				--nc;
			}
			return (a!=b);
		}
};

class Egalitarianism2 { public:
	int N;
	vector<vector<LL>> d2table;

	typedef tuple<LL, int, int> edge;

	double minStdev(vector <int> x, vector <int> y)
	{
		N = x.size();
		d2table.resize(N, vector<LL>(N));
		for(int v=0; v<N; ++v)
		for(int u=0; u<N; ++u)
			d2table[v][u] = (x[v]-x[u])*(x[v]-x[u]) + (y[v]-y[u])*(y[v]-y[u]);

		vector<edge> edges;
		for(int v=0; v<N; ++v)
		for(int u=v+1; u<N; ++u)
			edges.emplace_back(d2table[v][u], v, u);
		sort(edges.begin(), edges.end());

		UnionFind uf(N);
		return sqrt(rec(0, uf, edges, 0, 0));
	}

	double rec(int i, UnionFind& uf, const vector<edge>& edges, double sx, double sx2)
	{
		if(uf.size() == 1)
			return sx2/(N-1) - sx*sx/(N-1)/(N-1);
		if(i==edges.size())
			return 1e+300;

		vector<int> cand;
		int e=i;
		for(; e<edges.size() && get<0>(edges[i])==get<0>(edges[e]); ++e)
			if(uf.Find(get<1>(edges[e])) != uf.Find(get<2>(edges[e])))
				cand.push_back(e);

		if(cand.empty())
			return rec(e, uf, edges, sx, sx2);

		double best = 1e+300;
		for(int e: cand) {
			UnionFind uf2 = uf;
			LL  d = get<0>(edges[e]);
			int u = get<1>(edges[e]);
			int v = get<2>(edges[e]);
			uf2.Union(u, v);
			best = min(best, rec(e+1, uf2, edges, sx+sqrt(d), sx2+d));
		}
		return best;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const double& Expected, const double& Received) {
 bool ok = (abs(Expected - Received) < 1e-9);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, Egalitarianism2().minStdev(x, y));}
int main(){

CASE(0)
	int x_[] = {0,0,1,1};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {0,1,0,1};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	double _ = 0.0; 
END
CASE(1)
	int x_[] = {0,0,0};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {0,9,10};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	double _ = 0.5; 
END
CASE(2)
	int x_[] = {12,46,81,56};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {0,45,2,67};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	double _ = 6.102799971320872; 
END
CASE(3)
	int x_[] = {0,0,0,0,0,0,0};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {0,2,3,9,10,15,16};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	double _ = 0.9428090415820617; 
END
CASE(4)
	int x_[] = {167053, 536770, -590401, 507047, 350178, -274523, -584679, -766795, -664177, 267757, -291856, -765547, 604801, -682922, -404590, 468001, 607925, 503849, -499699, -798637};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {-12396, -66098, -56843, 20270, 81510, -23294, 10423, 24007, -24343, -21587, -6318, -7396, -68622, 56304, -85680, -14890, -38373, -25477, -38240, 11736};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	double _ = 40056.95946451678; 
END
CASE(5)
	int x_[] = {-306880, 169480, -558404, -193925, 654444, -300247, -456420, -119436, -620920, -470018, -914272, -691256, -49418, -21054, 603373, -23656, 891691, 258986, -453793, -782940};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {-77318, -632629, -344942, -361706, 191982, 349424, 676536, 166124, 291342, -268968, 188262, -537953, -70432, 156803, 166174, 345128, 58614, -671747, 508265, 92324};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	double _ = 36879.1512763429; 
END
/*
CASE(6)
	int x_[] = ;
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = ;
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	double _ = ; 
END
CASE(7)
	int x_[] = ;
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = ;
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	double _ = ; 
END
*/
}
// END CUT HERE