4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <functional> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class IncredibleMachine { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: double gravitationalAcceleration(vector <int> x, vector <int> y, int T) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: double gL = 0.0, gR = 1e+9; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<100; ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: double gC = (gL + gR) / 2; 4fd800b3a8 2011-02-23 kinaba: double tC = est(x, y, gC); 4fd800b3a8 2011-02-23 kinaba: (tC < T ? gR : gL) = gC; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return gL; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: double est(const vector<int>& x, const vector<int>& y, double g) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: double v = 0.0; 4fd800b3a8 2011-02-23 kinaba: double tot_t = 0.0; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i+1<x.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: double v0 = v; 4fd800b3a8 2011-02-23 kinaba: double len = sqrt(double((x[i+1]-x[i])*(x[i+1]-x[i]) + (y[i+1]-y[i])*(y[i+1]-y[i]))); 4fd800b3a8 2011-02-23 kinaba: double sin_alpha = (y[i]-y[i+1]) / len; 4fd800b3a8 2011-02-23 kinaba: double a = g * sin_alpha; 4fd800b3a8 2011-02-23 kinaba: // len = v0*t + 0.5a t^2 4fd800b3a8 2011-02-23 kinaba: // 0.5a t^2 + v0 t - len = 0 4fd800b3a8 2011-02-23 kinaba: double t = (-v0 + sqrt(v0*v0 + 2*a*len)) / a; 4fd800b3a8 2011-02-23 kinaba: v = v0 + a*t; 4fd800b3a8 2011-02-23 kinaba: tot_t += t; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return tot_t; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: int verify_case(const double &Expected, const double &Received) { double diff = Expected - Received; if (diff < 0) diff = -diff; if (diff < 1e-9) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} }; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: int x_[] = {0,6}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 4fd800b3a8 2011-02-23 kinaba: int y_[] = {100,22}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 4fd800b3a8 2011-02-23 kinaba: int T = 4; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 9.807692307692307; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, IncredibleMachine().gravitationalAcceleration(x, y, T)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: int x_[] = {0,26,100}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 4fd800b3a8 2011-02-23 kinaba: int y_[] = {50,26,24}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 4fd800b3a8 2011-02-23 kinaba: int T = 4; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 26.743031720603582; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, IncredibleMachine().gravitationalAcceleration(x, y, T)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: int x_[] = {0,7,8}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 4fd800b3a8 2011-02-23 kinaba: int y_[] = {10,6,0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 4fd800b3a8 2011-02-23 kinaba: int T = 7; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 1.1076837407708007; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, IncredibleMachine().gravitationalAcceleration(x, y, T)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: int x_[] = {0,100}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 4fd800b3a8 2011-02-23 kinaba: int y_[] = {1,0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 4fd800b3a8 2011-02-23 kinaba: int T = 1; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 20002; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, IncredibleMachine().gravitationalAcceleration(x, y, T)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: int x_[] = {0,1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 4fd800b3a8 2011-02-23 kinaba: int y_[] = {100,0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 4fd800b3a8 2011-02-23 kinaba: int T = 100; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 0.020002; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, IncredibleMachine().gravitationalAcceleration(x, y, T)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: