Artifact Content
Not logged in

Artifact f52c1a0b3c125396ddc55eec60e47239c51869bc


#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 MuddyRoad { public:
	double getExpectedValue(vector <int> road) 
	{
		double dp[50][2];
		for(size_t g=0; g<road.size(); ++g)
			for(int leftmud=0; leftmud<2; ++leftmud)
				if( g >= 2 )
				{
					const double p = road[g-2] / 100.0;
					const double q = (leftmud==1 ? 1.0 : road[g-1] / 100.0);
					double e = 0;
					e += (1-p) * (1-q) * dp[g-2][0];
					e += (1-p) * q * dp[g-2][0];
					e += p * (1-q) * dp[g-1][1];
					e += p * q * (1 + dp[g-2][0]);
					dp[g][leftmud] = e;
				}
				else if( g == 1 )
				{
					dp[g][leftmud] = 0;
				}
				else
				{
					dp[g][leftmud] = 0;
				}
		return dp[road.size()-1][0];
	}
};

// 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> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const double& Expected, const double& Received) {
 bool ok = (abs(Expected - Received) < 1e-9);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, MuddyRoad().getExpectedValue(road));}
int main(){

CASE(0)
	int road_[] = {0, 60, 60, 0};
	  vector <int> road(road_, road_+sizeof(road_)/sizeof(*road_)); 
	double _ = 0.36; 
END
CASE(1)
	int road_[] = {0, 50, 50, 50, 50, 0};
	  vector <int> road(road_, road_+sizeof(road_)/sizeof(*road_)); 
	double _ = 0.5625; 
END
CASE(2)
	int road_[] = {0, 0, 100, 100, 100, 100, 100, 100, 0, 0, 100, 0};
	  vector <int> road(road_, road_+sizeof(road_)/sizeof(*road_)); 
	double _ = 3.0; 
END
CASE(3)
	int road_[] = {0, 12, 34, 56, 78, 91, 23, 45, 67, 89, 0};
	  vector <int> road(road_, road_+sizeof(road_)/sizeof(*road_)); 
	double _ = 1.7352539420031923; 
END
CASE(4)
	int road_[] = {0, 50, 50, 100, 50, 100, 50, 50, 100, 66, 0};
	  vector <int> road(road_, road_+sizeof(road_)/sizeof(*road_)); 
	double _ = 2.288125; 
END
CASE(5)
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};
	  vector <int> road(road_, road_+sizeof(road_)/sizeof(*road_)); 
	double _ = 1.0; 
END
CASE(6)
	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};
	  vector <int> road(road_, road_+sizeof(road_)/sizeof(*road_)); 
	double _ = -1; 
END

}
// END CUT HERE