9b65a86a4f 2011-05-03 kinaba: #include <iostream> 9b65a86a4f 2011-05-03 kinaba: #include <sstream> 9b65a86a4f 2011-05-03 kinaba: #include <iomanip> 9b65a86a4f 2011-05-03 kinaba: #include <vector> 9b65a86a4f 2011-05-03 kinaba: #include <string> 9b65a86a4f 2011-05-03 kinaba: #include <map> 9b65a86a4f 2011-05-03 kinaba: #include <set> 9b65a86a4f 2011-05-03 kinaba: #include <algorithm> 9b65a86a4f 2011-05-03 kinaba: #include <numeric> 9b65a86a4f 2011-05-03 kinaba: #include <iterator> 9b65a86a4f 2011-05-03 kinaba: #include <functional> 9b65a86a4f 2011-05-03 kinaba: #include <complex> 9b65a86a4f 2011-05-03 kinaba: #include <queue> 9b65a86a4f 2011-05-03 kinaba: #include <stack> 9b65a86a4f 2011-05-03 kinaba: #include <cmath> 9b65a86a4f 2011-05-03 kinaba: #include <cassert> 9b65a86a4f 2011-05-03 kinaba: #include <cstring> 9b65a86a4f 2011-05-03 kinaba: using namespace std; 9b65a86a4f 2011-05-03 kinaba: typedef long long LL; 9b65a86a4f 2011-05-03 kinaba: typedef complex<double> CMP; 9b65a86a4f 2011-05-03 kinaba: 9b65a86a4f 2011-05-03 kinaba: class KingdomXCitiesandVillages { public: 9b65a86a4f 2011-05-03 kinaba: double determineLength(vector <int> cityX, vector <int> cityY, vector <int> villageX, vector <int> villageY) 9b65a86a4f 2011-05-03 kinaba: { 9b65a86a4f 2011-05-03 kinaba: const int C = cityX.size(); 9b65a86a4f 2011-05-03 kinaba: const int V = villageX.size(); 9b65a86a4f 2011-05-03 kinaba: double answer = 0.0; 9b65a86a4f 2011-05-03 kinaba: 9b65a86a4f 2011-05-03 kinaba: for(int i=0; i<V; ++i) 9b65a86a4f 2011-05-03 kinaba: { 9b65a86a4f 2011-05-03 kinaba: double nc = 1e+30; 9b65a86a4f 2011-05-03 kinaba: for(int j=0; j<C; ++j) 9b65a86a4f 2011-05-03 kinaba: nc = min(nc, distance(villageX[i], villageY[i], cityX[j], cityY[j])); 9b65a86a4f 2011-05-03 kinaba: 9b65a86a4f 2011-05-03 kinaba: vector<double> d; 9b65a86a4f 2011-05-03 kinaba: for(int j=0; j<V; ++j) 9b65a86a4f 2011-05-03 kinaba: if( j != i ) 9b65a86a4f 2011-05-03 kinaba: d.push_back( distance(villageX[i], villageY[i], villageX[j], villageY[j]) ); 9b65a86a4f 2011-05-03 kinaba: sort(d.begin(), d.end()); 9b65a86a4f 2011-05-03 kinaba: 9b65a86a4f 2011-05-03 kinaba: double prob = 1.0; 9b65a86a4f 2011-05-03 kinaba: for(int j=0; j<d.size() && d[j]<nc; ++j) 9b65a86a4f 2011-05-03 kinaba: { 9b65a86a4f 2011-05-03 kinaba: double p = 1.0 / (j+1) / (j+2); 9b65a86a4f 2011-05-03 kinaba: answer += p * d[j]; 9b65a86a4f 2011-05-03 kinaba: prob -= p; 9b65a86a4f 2011-05-03 kinaba: } 9b65a86a4f 2011-05-03 kinaba: answer += prob * nc; 9b65a86a4f 2011-05-03 kinaba: } 9b65a86a4f 2011-05-03 kinaba: return answer; 9b65a86a4f 2011-05-03 kinaba: } 9b65a86a4f 2011-05-03 kinaba: 9b65a86a4f 2011-05-03 kinaba: static double distance(double x, double y, double X, double Y) 9b65a86a4f 2011-05-03 kinaba: { 9b65a86a4f 2011-05-03 kinaba: return hypot(x-X, y-Y); 9b65a86a4f 2011-05-03 kinaba: } 9b65a86a4f 2011-05-03 kinaba: }; 9b65a86a4f 2011-05-03 kinaba: 9b65a86a4f 2011-05-03 kinaba: // BEGIN CUT HERE 9b65a86a4f 2011-05-03 kinaba: #include <ctime> 9b65a86a4f 2011-05-03 kinaba: double start_time; string timer() 9b65a86a4f 2011-05-03 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 9b65a86a4f 2011-05-03 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 9b65a86a4f 2011-05-03 kinaba: { os << "{ "; 9b65a86a4f 2011-05-03 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 9b65a86a4f 2011-05-03 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 9b65a86a4f 2011-05-03 kinaba: void verify_case(const double& Expected, const double& Received) { 9b65a86a4f 2011-05-03 kinaba: bool ok = (abs(Expected - Received) < 1e-9); 9b65a86a4f 2011-05-03 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 9b65a86a4f 2011-05-03 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 9b65a86a4f 2011-05-03 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 9b65a86a4f 2011-05-03 kinaba: #define END verify_case(_, KingdomXCitiesandVillages().determineLength(cityX, cityY, villageX, villageY));} 9b65a86a4f 2011-05-03 kinaba: int main(){ 9b65a86a4f 2011-05-03 kinaba: 9b65a86a4f 2011-05-03 kinaba: CASE(0) 9b65a86a4f 2011-05-03 kinaba: int cityX_[] = {3}; 9b65a86a4f 2011-05-03 kinaba: vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); 9b65a86a4f 2011-05-03 kinaba: int cityY_[] = {0}; 9b65a86a4f 2011-05-03 kinaba: vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); 9b65a86a4f 2011-05-03 kinaba: int villageX_[] = {3,3}; 9b65a86a4f 2011-05-03 kinaba: vector <int> villageX(villageX_, villageX_+sizeof(villageX_)/sizeof(*villageX_)); 9b65a86a4f 2011-05-03 kinaba: int villageY_[] = {2,1}; 9b65a86a4f 2011-05-03 kinaba: vector <int> villageY(villageY_, villageY_+sizeof(villageY_)/sizeof(*villageY_)); 9b65a86a4f 2011-05-03 kinaba: double _ = 2.5; 9b65a86a4f 2011-05-03 kinaba: END 9b65a86a4f 2011-05-03 kinaba: CASE(1) 9b65a86a4f 2011-05-03 kinaba: int cityX_[] = {1,4,7,10}; 9b65a86a4f 2011-05-03 kinaba: vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); 9b65a86a4f 2011-05-03 kinaba: int cityY_[] = {5,5,5,5}; 9b65a86a4f 2011-05-03 kinaba: vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); 9b65a86a4f 2011-05-03 kinaba: int villageX_[] = {1,4,7,10}; 9b65a86a4f 2011-05-03 kinaba: vector <int> villageX(villageX_, villageX_+sizeof(villageX_)/sizeof(*villageX_)); 9b65a86a4f 2011-05-03 kinaba: int villageY_[] = {4,4,4,4}; 9b65a86a4f 2011-05-03 kinaba: vector <int> villageY(villageY_, villageY_+sizeof(villageY_)/sizeof(*villageY_)); 9b65a86a4f 2011-05-03 kinaba: double _ = 4.0; 9b65a86a4f 2011-05-03 kinaba: END 9b65a86a4f 2011-05-03 kinaba: CASE(2) 9b65a86a4f 2011-05-03 kinaba: int cityX_[] = {1,2,3}; 9b65a86a4f 2011-05-03 kinaba: vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); 9b65a86a4f 2011-05-03 kinaba: int cityY_[] = {4,4,4}; 9b65a86a4f 2011-05-03 kinaba: vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); 9b65a86a4f 2011-05-03 kinaba: int villageX_[] = {4,5,6}; 9b65a86a4f 2011-05-03 kinaba: vector <int> villageX(villageX_, villageX_+sizeof(villageX_)/sizeof(*villageX_)); 9b65a86a4f 2011-05-03 kinaba: int villageY_[] = {4,4,4}; 9b65a86a4f 2011-05-03 kinaba: vector <int> villageY(villageY_, villageY_+sizeof(villageY_)/sizeof(*villageY_)); 9b65a86a4f 2011-05-03 kinaba: double _ = 4.166666666666667; 9b65a86a4f 2011-05-03 kinaba: END 9b65a86a4f 2011-05-03 kinaba: CASE(3) 9b65a86a4f 2011-05-03 kinaba: int cityX_[] = {1}; 9b65a86a4f 2011-05-03 kinaba: vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); 9b65a86a4f 2011-05-03 kinaba: int cityY_[] = {1}; 9b65a86a4f 2011-05-03 kinaba: vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); 9b65a86a4f 2011-05-03 kinaba: int villageX_[] = {2}; 9b65a86a4f 2011-05-03 kinaba: vector <int> villageX(villageX_, villageX_+sizeof(villageX_)/sizeof(*villageX_)); 9b65a86a4f 2011-05-03 kinaba: int villageY_[] = {1}; 9b65a86a4f 2011-05-03 kinaba: vector <int> villageY(villageY_, villageY_+sizeof(villageY_)/sizeof(*villageY_)); 9b65a86a4f 2011-05-03 kinaba: double _ = 1; 9b65a86a4f 2011-05-03 kinaba: END 9b65a86a4f 2011-05-03 kinaba: /* 9b65a86a4f 2011-05-03 kinaba: CASE(4) 9b65a86a4f 2011-05-03 kinaba: int cityX_[] = ; 9b65a86a4f 2011-05-03 kinaba: vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); 9b65a86a4f 2011-05-03 kinaba: int cityY_[] = ; 9b65a86a4f 2011-05-03 kinaba: vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); 9b65a86a4f 2011-05-03 kinaba: int villageX_[] = ; 9b65a86a4f 2011-05-03 kinaba: vector <int> villageX(villageX_, villageX_+sizeof(villageX_)/sizeof(*villageX_)); 9b65a86a4f 2011-05-03 kinaba: int villageY_[] = ; 9b65a86a4f 2011-05-03 kinaba: vector <int> villageY(villageY_, villageY_+sizeof(villageY_)/sizeof(*villageY_)); 9b65a86a4f 2011-05-03 kinaba: double _ = ; 9b65a86a4f 2011-05-03 kinaba: END 9b65a86a4f 2011-05-03 kinaba: */ 9b65a86a4f 2011-05-03 kinaba: } 9b65a86a4f 2011-05-03 kinaba: // END CUT HERE