Artifact Content
Not logged in

Artifact fe5a5960384579121892ecee2190b9945c116b3d


#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 <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class KingdomXCitiesandVillages { public:
	double determineLength(vector <int> cityX, vector <int> cityY, vector <int> villageX, vector <int> villageY) 
	{
		const int C = cityX.size();
		const int V = villageX.size();
		double answer = 0.0;

		for(int i=0; i<V; ++i)
		{
			double nc = 1e+30;
			for(int j=0; j<C; ++j)
				nc = min(nc, distance(villageX[i], villageY[i], cityX[j], cityY[j]));

			vector<double> d;
			for(int j=0; j<V; ++j)
				if( j != i )
					d.push_back( distance(villageX[i], villageY[i], villageX[j], villageY[j]) );
			sort(d.begin(), d.end());

			double prob = 1.0;
			for(int j=0; j<d.size() && d[j]<nc; ++j)
			{
				double p = 1.0 / (j+1) / (j+2);
				answer += p * d[j];
				prob -= p;
			}
			answer += prob * nc;
		}
		return answer;
	}

	static double distance(double x, double y, double X, double Y)
	{
		return hypot(x-X, y-Y);
	}
};

// 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(_, KingdomXCitiesandVillages().determineLength(cityX, cityY, villageX, villageY));}
int main(){

CASE(0)
	int cityX_[] = {3};
	  vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); 
	int cityY_[] = {0};
	  vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); 
	int villageX_[] = {3,3};
	  vector <int> villageX(villageX_, villageX_+sizeof(villageX_)/sizeof(*villageX_)); 
	int villageY_[] = {2,1};
	  vector <int> villageY(villageY_, villageY_+sizeof(villageY_)/sizeof(*villageY_)); 
	double _ = 2.5; 
END
CASE(1)
	int cityX_[] = {1,4,7,10};
	  vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); 
	int cityY_[] = {5,5,5,5};
	  vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); 
	int villageX_[] = {1,4,7,10};
	  vector <int> villageX(villageX_, villageX_+sizeof(villageX_)/sizeof(*villageX_)); 
	int villageY_[] = {4,4,4,4};
	  vector <int> villageY(villageY_, villageY_+sizeof(villageY_)/sizeof(*villageY_)); 
	double _ = 4.0; 
END
CASE(2)
	int cityX_[] = {1,2,3};
	  vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); 
	int cityY_[] = {4,4,4};
	  vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); 
	int villageX_[] = {4,5,6};
	  vector <int> villageX(villageX_, villageX_+sizeof(villageX_)/sizeof(*villageX_)); 
	int villageY_[] = {4,4,4};
	  vector <int> villageY(villageY_, villageY_+sizeof(villageY_)/sizeof(*villageY_)); 
	double _ = 4.166666666666667; 
END
CASE(3)
int cityX_[] = {1};
	  vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); 
	  int cityY_[] = {1};
	  vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); 
	  int villageX_[] = {2};
	  vector <int> villageX(villageX_, villageX_+sizeof(villageX_)/sizeof(*villageX_)); 
	  int villageY_[] = {1};
	  vector <int> villageY(villageY_, villageY_+sizeof(villageY_)/sizeof(*villageY_)); 
	double _ = 1; 
END
/*
CASE(4)
	int cityX_[] = ;
	  vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); 
	int cityY_[] = ;
	  vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); 
	int villageX_[] = ;
	  vector <int> villageX(villageX_, villageX_+sizeof(villageX_)/sizeof(*villageX_)); 
	int villageY_[] = ;
	  vector <int> villageY(villageY_, villageY_+sizeof(villageY_)/sizeof(*villageY_)); 
	double _ = ; 
END
*/
}
// END CUT HERE