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 <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: 4fd800b3a8 2011-02-23 kinaba: class ThreeBuses { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: double getProbability(vector <int> wait, vector <int> travel, int timeLeft) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: double t = timeLeft - accumulate(travel.begin(), travel.end(), 0); 4fd800b3a8 2011-02-23 kinaba: sort(wait.begin(), wait.end()); 4fd800b3a8 2011-02-23 kinaba: double w1 = wait[0]; 4fd800b3a8 2011-02-23 kinaba: double w2 = wait[1]; 4fd800b3a8 2011-02-23 kinaba: double w3 = wait[2]; 4fd800b3a8 2011-02-23 kinaba: // [0,w1] + [0,w2] + [0,w3] <= t ?? 4fd800b3a8 2011-02-23 kinaba: if( t >= w1+w2+w3 ) return 1; 4fd800b3a8 2011-02-23 kinaba: if( t <= 0 ) return 0; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( w2==0 ) return t / w3; 4fd800b3a8 2011-02-23 kinaba: if( w1==0 ) { 4fd800b3a8 2011-02-23 kinaba: double vol = t*t/2; 4fd800b3a8 2011-02-23 kinaba: if( t > w2 ) vol -= (t-w2)*(t-w2)/2; 4fd800b3a8 2011-02-23 kinaba: if( t > w3 ) vol -= (t-w3)*(t-w3)/2; 4fd800b3a8 2011-02-23 kinaba: return vol / (w2*w3); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: double vol = t*t*t/6; 4fd800b3a8 2011-02-23 kinaba: if( t > w1 ) vol -= (t-w1)*(t-w1)*(t-w1)/6; 4fd800b3a8 2011-02-23 kinaba: if( t > w2 ) vol -= (t-w2)*(t-w2)*(t-w2)/6; 4fd800b3a8 2011-02-23 kinaba: if( t > w3 ) vol -= (t-w3)*(t-w3)*(t-w3)/6; 4fd800b3a8 2011-02-23 kinaba: if( t > w1+w2 ) vol += (t-w1-w2)*(t-w1-w2)*(t-w1-w2)/6; 4fd800b3a8 2011-02-23 kinaba: if( t > w2+w3 ) vol += (t-w2-w3)*(t-w2-w3)*(t-w2-w3)/6; 4fd800b3a8 2011-02-23 kinaba: if( t > w3+w1 ) vol += (t-w3-w1)*(t-w3-w1)*(t-w3-w1)/6; 4fd800b3a8 2011-02-23 kinaba: return vol / (w1*w2*w3); 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 wait_[] = {0, 0, 0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> wait(wait_, wait_+sizeof(wait_)/sizeof(*wait_)); 4fd800b3a8 2011-02-23 kinaba: int travel_[] = {10, 15, 10}; 4fd800b3a8 2011-02-23 kinaba: vector <int> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); 4fd800b3a8 2011-02-23 kinaba: int timeLeft = 47; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 1.0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ThreeBuses().getProbability(wait, travel, timeLeft)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: int wait_[] = {0, 0, 0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> wait(wait_, wait_+sizeof(wait_)/sizeof(*wait_)); 4fd800b3a8 2011-02-23 kinaba: int travel_[] = {10, 15, 10}; 4fd800b3a8 2011-02-23 kinaba: vector <int> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); 4fd800b3a8 2011-02-23 kinaba: int timeLeft = 35; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 1.0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ThreeBuses().getProbability(wait, travel, timeLeft)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: int wait_[] = {1, 100, 1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> wait(wait_, wait_+sizeof(wait_)/sizeof(*wait_)); 4fd800b3a8 2011-02-23 kinaba: int travel_[] = {10, 10, 10}; 4fd800b3a8 2011-02-23 kinaba: vector <int> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); 4fd800b3a8 2011-02-23 kinaba: int timeLeft = 52; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 0.21; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ThreeBuses().getProbability(wait, travel, timeLeft)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: int wait_[] = {100, 100, 70}; 4fd800b3a8 2011-02-23 kinaba: vector <int> wait(wait_, wait_+sizeof(wait_)/sizeof(*wait_)); 4fd800b3a8 2011-02-23 kinaba: int travel_[] = {1, 1, 1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); 4fd800b3a8 2011-02-23 kinaba: int timeLeft = 47; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 0.020281904761904737; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ThreeBuses().getProbability(wait, travel, timeLeft)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: int wait_[] = {43210, 31730, 0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> wait(wait_, wait_+sizeof(wait_)/sizeof(*wait_)); 4fd800b3a8 2011-02-23 kinaba: int travel_[] = {1, 1, 1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); 4fd800b3a8 2011-02-23 kinaba: int timeLeft = 43212; 4fd800b3a8 2011-02-23 kinaba: double RetVal = 0.6328164776672066; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ThreeBuses().getProbability(wait, travel, timeLeft)); } 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: