Artifact Content
Not logged in

Artifact b6921f196282e5d1ed25d3f735d81943c3ef7bca


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;

class ThreeBuses {
public:
	double getProbability(vector <int> wait, vector <int> travel, int timeLeft) 
	{
		double t = timeLeft - accumulate(travel.begin(), travel.end(), 0);
		sort(wait.begin(), wait.end());
		double w1 = wait[0];
		double w2 = wait[1];
		double w3 = wait[2];
		// [0,w1] + [0,w2] + [0,w3] <= t ??
		if( t >= w1+w2+w3 ) return 1;
		if( t <= 0 )        return 0;

		if( w2==0 ) return t / w3;
		if( w1==0 ) {
			double vol = t*t/2;
			if( t > w2 ) vol -= (t-w2)*(t-w2)/2;
			if( t > w3 ) vol -= (t-w3)*(t-w3)/2;
			return vol / (w2*w3);
		}

		double vol = t*t*t/6;
		if( t > w1 )    vol -= (t-w1)*(t-w1)*(t-w1)/6;
		if( t > w2 )    vol -= (t-w2)*(t-w2)*(t-w2)/6;
		if( t > w3 )    vol -= (t-w3)*(t-w3)*(t-w3)/6;
		if( t > w1+w2 ) vol += (t-w1-w2)*(t-w1-w2)*(t-w1-w2)/6;
		if( t > w2+w3 ) vol += (t-w2-w3)*(t-w2-w3)*(t-w2-w3)/6;
		if( t > w3+w1 ) vol += (t-w3-w1)*(t-w3-w1)*(t-w3-w1)/6;
		return vol / (w1*w2*w3);
	}
};

// 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 wait_[] = {0, 0, 0};
	  vector <int> wait(wait_, wait_+sizeof(wait_)/sizeof(*wait_)); 
	int travel_[] = {10, 15, 10};
	  vector <int> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); 
	int timeLeft = 47; 
	double RetVal = 1.0; 
	return verify_case(RetVal, ThreeBuses().getProbability(wait, travel, timeLeft)); }
int Test_(Case_<1>) {
	int wait_[] = {0, 0, 0};
	  vector <int> wait(wait_, wait_+sizeof(wait_)/sizeof(*wait_)); 
	int travel_[] = {10, 15, 10};
	  vector <int> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); 
	int timeLeft = 35; 
	double RetVal = 1.0; 
	return verify_case(RetVal, ThreeBuses().getProbability(wait, travel, timeLeft)); }
int Test_(Case_<2>) {
	int wait_[] = {1, 100, 1};
	  vector <int> wait(wait_, wait_+sizeof(wait_)/sizeof(*wait_)); 
	int travel_[] = {10, 10, 10};
	  vector <int> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); 
	int timeLeft = 52; 
	double RetVal = 0.21; 
	return verify_case(RetVal, ThreeBuses().getProbability(wait, travel, timeLeft)); }
int Test_(Case_<3>) {
	int wait_[] = {100, 100, 70};
	  vector <int> wait(wait_, wait_+sizeof(wait_)/sizeof(*wait_)); 
	int travel_[] = {1, 1, 1};
	  vector <int> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); 
	int timeLeft = 47; 
	double RetVal = 0.020281904761904737; 
	return verify_case(RetVal, ThreeBuses().getProbability(wait, travel, timeLeft)); }
int Test_(Case_<4>) {
	int wait_[] = {43210, 31730, 0};
	  vector <int> wait(wait_, wait_+sizeof(wait_)/sizeof(*wait_)); 
	int travel_[] = {1, 1, 1};
	  vector <int> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); 
	int timeLeft = 43212; 
	double RetVal = 0.6328164776672066; 
	return verify_case(RetVal, ThreeBuses().getProbability(wait, travel, timeLeft)); }

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