3c4124479f 2014-04-04 kinaba: #include <iostream> 3c4124479f 2014-04-04 kinaba: #include <sstream> 3c4124479f 2014-04-04 kinaba: #include <iomanip> 3c4124479f 2014-04-04 kinaba: #include <vector> 3c4124479f 2014-04-04 kinaba: #include <string> 3c4124479f 2014-04-04 kinaba: #include <map> 3c4124479f 2014-04-04 kinaba: #include <set> 3c4124479f 2014-04-04 kinaba: #include <algorithm> 3c4124479f 2014-04-04 kinaba: #include <numeric> 3c4124479f 2014-04-04 kinaba: #include <iterator> 3c4124479f 2014-04-04 kinaba: #include <functional> 3c4124479f 2014-04-04 kinaba: #include <complex> 3c4124479f 2014-04-04 kinaba: #include <queue> 3c4124479f 2014-04-04 kinaba: #include <stack> 3c4124479f 2014-04-04 kinaba: #include <cmath> 3c4124479f 2014-04-04 kinaba: #include <cassert> 3c4124479f 2014-04-04 kinaba: #include <tuple> 3c4124479f 2014-04-04 kinaba: using namespace std; 3c4124479f 2014-04-04 kinaba: typedef long long LL; 3c4124479f 2014-04-04 kinaba: typedef complex<double> CMP; 3c4124479f 2014-04-04 kinaba: 3c4124479f 2014-04-04 kinaba: struct UnionFind 3c4124479f 2014-04-04 kinaba: { 3c4124479f 2014-04-04 kinaba: vector<int> uf, sz; 3c4124479f 2014-04-04 kinaba: int nc; 3c4124479f 2014-04-04 kinaba: 3c4124479f 2014-04-04 kinaba: UnionFind(int N): uf(N), sz(N,1), nc(N) 3c4124479f 2014-04-04 kinaba: { for(int i=0; i<N; ++i) uf[i] = i; } 3c4124479f 2014-04-04 kinaba: int size() 3c4124479f 2014-04-04 kinaba: { return nc; } 3c4124479f 2014-04-04 kinaba: int size(int a) 3c4124479f 2014-04-04 kinaba: { return sz[Find(a)]; } 3c4124479f 2014-04-04 kinaba: int Find(int a) 3c4124479f 2014-04-04 kinaba: { return uf[a]==a ? a : uf[a]=Find(uf[a]); } 3c4124479f 2014-04-04 kinaba: bool Union(int a, int b) 3c4124479f 2014-04-04 kinaba: { 3c4124479f 2014-04-04 kinaba: a = Find(a); 3c4124479f 2014-04-04 kinaba: b = Find(b); 3c4124479f 2014-04-04 kinaba: if( a != b ) 3c4124479f 2014-04-04 kinaba: { 3c4124479f 2014-04-04 kinaba: if( sz[a] >= sz[b] ) swap(a, b); 3c4124479f 2014-04-04 kinaba: uf[a] = b; 3c4124479f 2014-04-04 kinaba: sz[b] += sz[a]; 3c4124479f 2014-04-04 kinaba: --nc; 3c4124479f 2014-04-04 kinaba: } 3c4124479f 2014-04-04 kinaba: return (a!=b); 3c4124479f 2014-04-04 kinaba: } 3c4124479f 2014-04-04 kinaba: }; 3c4124479f 2014-04-04 kinaba: 3c4124479f 2014-04-04 kinaba: class Egalitarianism2 { public: 3c4124479f 2014-04-04 kinaba: int N; 3c4124479f 2014-04-04 kinaba: vector<vector<LL>> d2table; 3c4124479f 2014-04-04 kinaba: 3c4124479f 2014-04-04 kinaba: typedef tuple<LL, int, int> edge; 3c4124479f 2014-04-04 kinaba: 3c4124479f 2014-04-04 kinaba: double minStdev(vector <int> x, vector <int> y) 3c4124479f 2014-04-04 kinaba: { 3c4124479f 2014-04-04 kinaba: N = x.size(); 3c4124479f 2014-04-04 kinaba: d2table.resize(N, vector<LL>(N)); 3c4124479f 2014-04-04 kinaba: for(int v=0; v<N; ++v) 3c4124479f 2014-04-04 kinaba: for(int u=0; u<N; ++u) 3c4124479f 2014-04-04 kinaba: d2table[v][u] = (x[v]-x[u])*(x[v]-x[u]) + (y[v]-y[u])*(y[v]-y[u]); 3c4124479f 2014-04-04 kinaba: 3c4124479f 2014-04-04 kinaba: vector<edge> edges; 3c4124479f 2014-04-04 kinaba: for(int v=0; v<N; ++v) 3c4124479f 2014-04-04 kinaba: for(int u=v+1; u<N; ++u) 3c4124479f 2014-04-04 kinaba: edges.emplace_back(d2table[v][u], v, u); 3c4124479f 2014-04-04 kinaba: sort(edges.begin(), edges.end()); 3c4124479f 2014-04-04 kinaba: 3c4124479f 2014-04-04 kinaba: UnionFind uf(N); 3c4124479f 2014-04-04 kinaba: return sqrt(rec(0, uf, edges, 0, 0)); 3c4124479f 2014-04-04 kinaba: } 3c4124479f 2014-04-04 kinaba: 3c4124479f 2014-04-04 kinaba: double rec(int i, UnionFind& uf, const vector<edge>& edges, double sx, double sx2) 3c4124479f 2014-04-04 kinaba: { 3c4124479f 2014-04-04 kinaba: if(uf.size() == 1) 3c4124479f 2014-04-04 kinaba: return sx2/(N-1) - sx*sx/(N-1)/(N-1); 3c4124479f 2014-04-04 kinaba: if(i==edges.size()) 3c4124479f 2014-04-04 kinaba: return 1e+300; 3c4124479f 2014-04-04 kinaba: 3c4124479f 2014-04-04 kinaba: vector<int> cand; 3c4124479f 2014-04-04 kinaba: int e=i; 3c4124479f 2014-04-04 kinaba: for(; e<edges.size() && get<0>(edges[i])==get<0>(edges[e]); ++e) 3c4124479f 2014-04-04 kinaba: if(uf.Find(get<1>(edges[e])) != uf.Find(get<2>(edges[e]))) 3c4124479f 2014-04-04 kinaba: cand.push_back(e); 3c4124479f 2014-04-04 kinaba: 3c4124479f 2014-04-04 kinaba: if(cand.empty()) 3c4124479f 2014-04-04 kinaba: return rec(e, uf, edges, sx, sx2); 3c4124479f 2014-04-04 kinaba: 3c4124479f 2014-04-04 kinaba: double best = 1e+300; 3c4124479f 2014-04-04 kinaba: for(int e: cand) { 3c4124479f 2014-04-04 kinaba: UnionFind uf2 = uf; 3c4124479f 2014-04-04 kinaba: LL d = get<0>(edges[e]); 3c4124479f 2014-04-04 kinaba: int u = get<1>(edges[e]); 3c4124479f 2014-04-04 kinaba: int v = get<2>(edges[e]); 3c4124479f 2014-04-04 kinaba: uf2.Union(u, v); 3c4124479f 2014-04-04 kinaba: best = min(best, rec(e+1, uf2, edges, sx+sqrt(d), sx2+d)); 3c4124479f 2014-04-04 kinaba: } 3c4124479f 2014-04-04 kinaba: return best; 3c4124479f 2014-04-04 kinaba: } 3c4124479f 2014-04-04 kinaba: }; 3c4124479f 2014-04-04 kinaba: 3c4124479f 2014-04-04 kinaba: // BEGIN CUT HERE 3c4124479f 2014-04-04 kinaba: #include <ctime> 3c4124479f 2014-04-04 kinaba: double start_time; string timer() 3c4124479f 2014-04-04 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 3c4124479f 2014-04-04 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 3c4124479f 2014-04-04 kinaba: { os << "{ "; 3c4124479f 2014-04-04 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 3c4124479f 2014-04-04 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 3c4124479f 2014-04-04 kinaba: void verify_case(const double& Expected, const double& Received) { 3c4124479f 2014-04-04 kinaba: bool ok = (abs(Expected - Received) < 1e-9); 3c4124479f 2014-04-04 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 3c4124479f 2014-04-04 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 3c4124479f 2014-04-04 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 3c4124479f 2014-04-04 kinaba: #define END verify_case(_, Egalitarianism2().minStdev(x, y));} 3c4124479f 2014-04-04 kinaba: int main(){ 3c4124479f 2014-04-04 kinaba: 3c4124479f 2014-04-04 kinaba: CASE(0) 3c4124479f 2014-04-04 kinaba: int x_[] = {0,0,1,1}; 3c4124479f 2014-04-04 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 3c4124479f 2014-04-04 kinaba: int y_[] = {0,1,0,1}; 3c4124479f 2014-04-04 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 3c4124479f 2014-04-04 kinaba: double _ = 0.0; 3c4124479f 2014-04-04 kinaba: END 3c4124479f 2014-04-04 kinaba: CASE(1) 3c4124479f 2014-04-04 kinaba: int x_[] = {0,0,0}; 3c4124479f 2014-04-04 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 3c4124479f 2014-04-04 kinaba: int y_[] = {0,9,10}; 3c4124479f 2014-04-04 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 3c4124479f 2014-04-04 kinaba: double _ = 0.5; 3c4124479f 2014-04-04 kinaba: END 3c4124479f 2014-04-04 kinaba: CASE(2) 3c4124479f 2014-04-04 kinaba: int x_[] = {12,46,81,56}; 3c4124479f 2014-04-04 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 3c4124479f 2014-04-04 kinaba: int y_[] = {0,45,2,67}; 3c4124479f 2014-04-04 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 3c4124479f 2014-04-04 kinaba: double _ = 6.102799971320872; 3c4124479f 2014-04-04 kinaba: END 3c4124479f 2014-04-04 kinaba: CASE(3) 3c4124479f 2014-04-04 kinaba: int x_[] = {0,0,0,0,0,0,0}; 3c4124479f 2014-04-04 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 3c4124479f 2014-04-04 kinaba: int y_[] = {0,2,3,9,10,15,16}; 3c4124479f 2014-04-04 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 3c4124479f 2014-04-04 kinaba: double _ = 0.9428090415820617; 3c4124479f 2014-04-04 kinaba: END 3c4124479f 2014-04-04 kinaba: CASE(4) 3c4124479f 2014-04-04 kinaba: int x_[] = {167053, 536770, -590401, 507047, 350178, -274523, -584679, -766795, -664177, 267757, -291856, -765547, 604801, -682922, -404590, 468001, 607925, 503849, -499699, -798637}; 3c4124479f 2014-04-04 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 3c4124479f 2014-04-04 kinaba: int y_[] = {-12396, -66098, -56843, 20270, 81510, -23294, 10423, 24007, -24343, -21587, -6318, -7396, -68622, 56304, -85680, -14890, -38373, -25477, -38240, 11736}; 3c4124479f 2014-04-04 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 3c4124479f 2014-04-04 kinaba: double _ = 40056.95946451678; 3c4124479f 2014-04-04 kinaba: END 3c4124479f 2014-04-04 kinaba: CASE(5) 3c4124479f 2014-04-04 kinaba: int x_[] = {-306880, 169480, -558404, -193925, 654444, -300247, -456420, -119436, -620920, -470018, -914272, -691256, -49418, -21054, 603373, -23656, 891691, 258986, -453793, -782940}; 3c4124479f 2014-04-04 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 3c4124479f 2014-04-04 kinaba: int y_[] = {-77318, -632629, -344942, -361706, 191982, 349424, 676536, 166124, 291342, -268968, 188262, -537953, -70432, 156803, 166174, 345128, 58614, -671747, 508265, 92324}; 3c4124479f 2014-04-04 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 3c4124479f 2014-04-04 kinaba: double _ = 36879.1512763429; 3c4124479f 2014-04-04 kinaba: END 3c4124479f 2014-04-04 kinaba: /* 3c4124479f 2014-04-04 kinaba: CASE(6) 3c4124479f 2014-04-04 kinaba: int x_[] = ; 3c4124479f 2014-04-04 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 3c4124479f 2014-04-04 kinaba: int y_[] = ; 3c4124479f 2014-04-04 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 3c4124479f 2014-04-04 kinaba: double _ = ; 3c4124479f 2014-04-04 kinaba: END 3c4124479f 2014-04-04 kinaba: CASE(7) 3c4124479f 2014-04-04 kinaba: int x_[] = ; 3c4124479f 2014-04-04 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 3c4124479f 2014-04-04 kinaba: int y_[] = ; 3c4124479f 2014-04-04 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 3c4124479f 2014-04-04 kinaba: double _ = ; 3c4124479f 2014-04-04 kinaba: END 3c4124479f 2014-04-04 kinaba: */ 3c4124479f 2014-04-04 kinaba: } 3c4124479f 2014-04-04 kinaba: // END CUT HERE