ba35b3a9cf 2011-06-25 kinaba: #include <iostream> ba35b3a9cf 2011-06-25 kinaba: #include <sstream> ba35b3a9cf 2011-06-25 kinaba: #include <iomanip> ba35b3a9cf 2011-06-25 kinaba: #include <vector> ba35b3a9cf 2011-06-25 kinaba: #include <string> ba35b3a9cf 2011-06-25 kinaba: #include <map> ba35b3a9cf 2011-06-25 kinaba: #include <set> ba35b3a9cf 2011-06-25 kinaba: #include <algorithm> ba35b3a9cf 2011-06-25 kinaba: #include <numeric> ba35b3a9cf 2011-06-25 kinaba: #include <iterator> ba35b3a9cf 2011-06-25 kinaba: #include <functional> ba35b3a9cf 2011-06-25 kinaba: #include <complex> ba35b3a9cf 2011-06-25 kinaba: #include <queue> ba35b3a9cf 2011-06-25 kinaba: #include <stack> ba35b3a9cf 2011-06-25 kinaba: #include <cmath> ba35b3a9cf 2011-06-25 kinaba: #include <cassert> ba35b3a9cf 2011-06-25 kinaba: #include <cstring> ba35b3a9cf 2011-06-25 kinaba: using namespace std; ba35b3a9cf 2011-06-25 kinaba: typedef long long LL; ba35b3a9cf 2011-06-25 kinaba: typedef complex<double> CMP; ba35b3a9cf 2011-06-25 kinaba: ba35b3a9cf 2011-06-25 kinaba: class MuddyRoad { public: ba35b3a9cf 2011-06-25 kinaba: double getExpectedValue(vector <int> road) ba35b3a9cf 2011-06-25 kinaba: { ba35b3a9cf 2011-06-25 kinaba: double dp[50][2]; ba35b3a9cf 2011-06-25 kinaba: for(size_t g=0; g<road.size(); ++g) ba35b3a9cf 2011-06-25 kinaba: for(int leftmud=0; leftmud<2; ++leftmud) ba35b3a9cf 2011-06-25 kinaba: if( g >= 2 ) ba35b3a9cf 2011-06-25 kinaba: { ba35b3a9cf 2011-06-25 kinaba: const double p = road[g-2] / 100.0; ba35b3a9cf 2011-06-25 kinaba: const double q = (leftmud==1 ? 1.0 : road[g-1] / 100.0); ba35b3a9cf 2011-06-25 kinaba: double e = 0; ba35b3a9cf 2011-06-25 kinaba: e += (1-p) * (1-q) * dp[g-2][0]; ba35b3a9cf 2011-06-25 kinaba: e += (1-p) * q * dp[g-2][0]; ba35b3a9cf 2011-06-25 kinaba: e += p * (1-q) * dp[g-1][1]; ba35b3a9cf 2011-06-25 kinaba: e += p * q * (1 + dp[g-2][0]); ba35b3a9cf 2011-06-25 kinaba: dp[g][leftmud] = e; ba35b3a9cf 2011-06-25 kinaba: } ba35b3a9cf 2011-06-25 kinaba: else if( g == 1 ) ba35b3a9cf 2011-06-25 kinaba: { ba35b3a9cf 2011-06-25 kinaba: dp[g][leftmud] = 0; ba35b3a9cf 2011-06-25 kinaba: } ba35b3a9cf 2011-06-25 kinaba: else ba35b3a9cf 2011-06-25 kinaba: { ba35b3a9cf 2011-06-25 kinaba: dp[g][leftmud] = 0; ba35b3a9cf 2011-06-25 kinaba: } ba35b3a9cf 2011-06-25 kinaba: return dp[road.size()-1][0]; ba35b3a9cf 2011-06-25 kinaba: } ba35b3a9cf 2011-06-25 kinaba: }; ba35b3a9cf 2011-06-25 kinaba: ba35b3a9cf 2011-06-25 kinaba: // BEGIN CUT HERE ba35b3a9cf 2011-06-25 kinaba: #include <ctime> ba35b3a9cf 2011-06-25 kinaba: double start_time; string timer() ba35b3a9cf 2011-06-25 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } ba35b3a9cf 2011-06-25 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) ba35b3a9cf 2011-06-25 kinaba: { os << "{ "; ba35b3a9cf 2011-06-25 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) ba35b3a9cf 2011-06-25 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } ba35b3a9cf 2011-06-25 kinaba: void verify_case(const double& Expected, const double& Received) { ba35b3a9cf 2011-06-25 kinaba: bool ok = (abs(Expected - Received) < 1e-9); ba35b3a9cf 2011-06-25 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; ba35b3a9cf 2011-06-25 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } ba35b3a9cf 2011-06-25 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); ba35b3a9cf 2011-06-25 kinaba: #define END verify_case(_, MuddyRoad().getExpectedValue(road));} ba35b3a9cf 2011-06-25 kinaba: int main(){ ba35b3a9cf 2011-06-25 kinaba: ba35b3a9cf 2011-06-25 kinaba: CASE(0) ba35b3a9cf 2011-06-25 kinaba: int road_[] = {0, 60, 60, 0}; ba35b3a9cf 2011-06-25 kinaba: vector <int> road(road_, road_+sizeof(road_)/sizeof(*road_)); ba35b3a9cf 2011-06-25 kinaba: double _ = 0.36; ba35b3a9cf 2011-06-25 kinaba: END ba35b3a9cf 2011-06-25 kinaba: CASE(1) ba35b3a9cf 2011-06-25 kinaba: int road_[] = {0, 50, 50, 50, 50, 0}; ba35b3a9cf 2011-06-25 kinaba: vector <int> road(road_, road_+sizeof(road_)/sizeof(*road_)); ba35b3a9cf 2011-06-25 kinaba: double _ = 0.5625; ba35b3a9cf 2011-06-25 kinaba: END ba35b3a9cf 2011-06-25 kinaba: CASE(2) ba35b3a9cf 2011-06-25 kinaba: int road_[] = {0, 0, 100, 100, 100, 100, 100, 100, 0, 0, 100, 0}; ba35b3a9cf 2011-06-25 kinaba: vector <int> road(road_, road_+sizeof(road_)/sizeof(*road_)); ba35b3a9cf 2011-06-25 kinaba: double _ = 3.0; ba35b3a9cf 2011-06-25 kinaba: END ba35b3a9cf 2011-06-25 kinaba: CASE(3) ba35b3a9cf 2011-06-25 kinaba: int road_[] = {0, 12, 34, 56, 78, 91, 23, 45, 67, 89, 0}; ba35b3a9cf 2011-06-25 kinaba: vector <int> road(road_, road_+sizeof(road_)/sizeof(*road_)); ba35b3a9cf 2011-06-25 kinaba: double _ = 1.7352539420031923; ba35b3a9cf 2011-06-25 kinaba: END ba35b3a9cf 2011-06-25 kinaba: CASE(4) ba35b3a9cf 2011-06-25 kinaba: int road_[] = {0, 50, 50, 100, 50, 100, 50, 50, 100, 66, 0}; ba35b3a9cf 2011-06-25 kinaba: vector <int> road(road_, road_+sizeof(road_)/sizeof(*road_)); ba35b3a9cf 2011-06-25 kinaba: double _ = 2.288125; ba35b3a9cf 2011-06-25 kinaba: END ba35b3a9cf 2011-06-25 kinaba: CASE(5) ba35b3a9cf 2011-06-25 kinaba: int road_[] = {0,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,0}; ba35b3a9cf 2011-06-25 kinaba: vector <int> road(road_, road_+sizeof(road_)/sizeof(*road_)); ba35b3a9cf 2011-06-25 kinaba: double _ = 1.0; ba35b3a9cf 2011-06-25 kinaba: END ba35b3a9cf 2011-06-25 kinaba: CASE(6) ba35b3a9cf 2011-06-25 kinaba: int road_[] = {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0}; ba35b3a9cf 2011-06-25 kinaba: vector <int> road(road_, road_+sizeof(road_)/sizeof(*road_)); ba35b3a9cf 2011-06-25 kinaba: double _ = -1; ba35b3a9cf 2011-06-25 kinaba: END ba35b3a9cf 2011-06-25 kinaba: ba35b3a9cf 2011-06-25 kinaba: } ba35b3a9cf 2011-06-25 kinaba: // END CUT HERE