Artifact Content
Not logged in

Artifact 40b011870ac59713a08ed157f50953749c2652fa


#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 IncredibleMachine {
public:
	double gravitationalAcceleration(vector <int> x, vector <int> y, int T) 
	{
		double gL = 0.0, gR = 1e+9;
		for(int i=0; i<100; ++i)
		{
			double gC = (gL + gR) / 2;
			double tC = est(x, y, gC);
			(tC < T ? gR : gL) = gC;
		}
		return gL;
	}

	double est(const vector<int>& x, const vector<int>& y, double g)
	{
		double v = 0.0;
		double tot_t = 0.0;
		for(int i=0; i+1<x.size(); ++i)
		{
			double v0 = v;
			double len = sqrt(double((x[i+1]-x[i])*(x[i+1]-x[i]) + (y[i+1]-y[i])*(y[i+1]-y[i])));
			double sin_alpha = (y[i]-y[i+1]) / len;
			double a = g * sin_alpha;
			// len = v0*t + 0.5a t^2
			// 0.5a t^2 + v0 t - len = 0
			double t = (-v0 + sqrt(v0*v0 + 2*a*len)) / a;
			v = v0 + a*t;
			tot_t += t;
		}
		return tot_t;
	}
};

// 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> 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(); }
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;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	int x_[] = {0,6};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {100,22};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int T = 4; 
	double RetVal = 9.807692307692307; 
	return verify_case(RetVal, IncredibleMachine().gravitationalAcceleration(x, y, T)); }
int Test_(Case_<1>) {
	int x_[] = {0,26,100};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {50,26,24};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int T = 4; 
	double RetVal = 26.743031720603582; 
	return verify_case(RetVal, IncredibleMachine().gravitationalAcceleration(x, y, T)); }
int Test_(Case_<2>) {
	int x_[] = {0,7,8};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {10,6,0};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int T = 7; 
	double RetVal = 1.1076837407708007; 
	return verify_case(RetVal, IncredibleMachine().gravitationalAcceleration(x, y, T)); }
int Test_(Case_<3>) {
	int x_[] = {0,100};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {1,0};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int T = 1; 
	double RetVal = 20002; 
	return verify_case(RetVal, IncredibleMachine().gravitationalAcceleration(x, y, T)); }
int Test_(Case_<4>) {
	int x_[] = {0,1};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {100,0};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int T = 100; 
	double RetVal = 0.020002; 
	return verify_case(RetVal, IncredibleMachine().gravitationalAcceleration(x, y, T)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE