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 <functional> 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: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class GreatExpectations { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: double expectedTime(vector <string> doors) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: const int Y = doors.size()/2 +1; 4fd800b3a8 2011-02-23 kinaba: const int X = doors[0].size() +1; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector< vector<double> > e(Y, vector<double>(X, -1.0)); 4fd800b3a8 2011-02-23 kinaba: e[Y-1][X-1] = 0.0; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int k=0; k<X*Y; ++k) 4fd800b3a8 2011-02-23 kinaba: for(int y=0; y<Y; ++y) 4fd800b3a8 2011-02-23 kinaba: for(int x=0; x<X; ++x) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // neibours : (expected_time, door_prob) 4fd800b3a8 2011-02-23 kinaba: vector< pair<double,double> > ep; 4fd800b3a8 2011-02-23 kinaba: if( x+1<X && doors[y*2][x]!='0' && e[y][x+1]>=0 ) 4fd800b3a8 2011-02-23 kinaba: ep.push_back( make_pair(e[y][x+1], (doors[y*2][x]-'0')/8.0) ); 4fd800b3a8 2011-02-23 kinaba: if( 0<x && doors[y*2][x-1]!='0' && e[y][x-1]>=0 ) 4fd800b3a8 2011-02-23 kinaba: ep.push_back( make_pair(e[y][x-1], (doors[y*2][x-1]-'0')/8.0) ); 4fd800b3a8 2011-02-23 kinaba: if( y+1<Y && doors[y*2+1][x]!='0' && e[y+1][x]>=0 ) 4fd800b3a8 2011-02-23 kinaba: ep.push_back( make_pair(e[y+1][x], (doors[y*2+1][x]-'0')/8.0) ); 4fd800b3a8 2011-02-23 kinaba: if( 0<y && doors[y*2-1][x]!='0' && e[y-1][x]>=0 ) 4fd800b3a8 2011-02-23 kinaba: ep.push_back( make_pair(e[y-1][x], (doors[y*2-1][x]-'0')/8.0) ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( ep.empty() ) continue; 4fd800b3a8 2011-02-23 kinaba: sort( ep.begin(), ep.end() ); // better first 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // choose k that minimizes the following E: 4fd800b3a8 2011-02-23 kinaba: // E = (1+e[0])*p[0] 4fd800b3a8 2011-02-23 kinaba: // + (1+e[1])*(1-p[0])*p[1] 4fd800b3a8 2011-02-23 kinaba: // + (1+e[2])*(1-p[0])*(1-p[1])*p[2] 4fd800b3a8 2011-02-23 kinaba: // + ... 4fd800b3a8 2011-02-23 kinaba: // + (1+E) *(1-p[0])*(1-p[1])*...*(1-p[k]) 4fd800b3a8 2011-02-23 kinaba: double r=1.0, s=0.0, E=1e+50; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<ep.size(); ++i) { 4fd800b3a8 2011-02-23 kinaba: double s2 = s + r * ep[i].second * (1 + ep[i].first); 4fd800b3a8 2011-02-23 kinaba: double r2 = r * (1 - ep[i].second); 4fd800b3a8 2011-02-23 kinaba: double E2 = (s2 + r2) / (1-r2); 4fd800b3a8 2011-02-23 kinaba: if( E2 > E ) break; 4fd800b3a8 2011-02-23 kinaba: r=r2, s=s2, E=E2; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: if( e[y][x]<0 || e[y][x]>E ) 4fd800b3a8 2011-02-23 kinaba: e[y][x]=E; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: return e[0][0]; 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: string doors_[] = { "4", 4fd800b3a8 2011-02-23 kinaba: "44", 4fd800b3a8 2011-02-23 kinaba: "4" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> doors(doors_, doors_+sizeof(doors_)/sizeof(*doors_)); 4fd800b3a8 2011-02-23 kinaba: double RetVal = 3.3333333333333335; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, GreatExpectations().expectedTime(doors)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: string doors_[] = { "48", 4fd800b3a8 2011-02-23 kinaba: "440", 4fd800b3a8 2011-02-23 kinaba: "42", 4fd800b3a8 2011-02-23 kinaba: "862", 4fd800b3a8 2011-02-23 kinaba: "06" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> doors(doors_, doors_+sizeof(doors_)/sizeof(*doors_)); 4fd800b3a8 2011-02-23 kinaba: double RetVal = 6.0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, GreatExpectations().expectedTime(doors)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: string doors_[] = { "0808", 4fd800b3a8 2011-02-23 kinaba: "88888", 4fd800b3a8 2011-02-23 kinaba: "0000", 4fd800b3a8 2011-02-23 kinaba: "88888", 4fd800b3a8 2011-02-23 kinaba: "8080" } 4fd800b3a8 2011-02-23 kinaba: ; 4fd800b3a8 2011-02-23 kinaba: vector <string> doors(doors_, doors_+sizeof(doors_)/sizeof(*doors_)); 4fd800b3a8 2011-02-23 kinaba: double RetVal = 14.0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, GreatExpectations().expectedTime(doors)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: string doors_[] = { "2815", 4fd800b3a8 2011-02-23 kinaba: "67686", 4fd800b3a8 2011-02-23 kinaba: "1324", 4fd800b3a8 2011-02-23 kinaba: "75767", 4fd800b3a8 2011-02-23 kinaba: "6051" } 4fd800b3a8 2011-02-23 kinaba: ; 4fd800b3a8 2011-02-23 kinaba: vector <string> doors(doors_, doors_+sizeof(doors_)/sizeof(*doors_)); 4fd800b3a8 2011-02-23 kinaba: double RetVal = 11.852782338798935; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, GreatExpectations().expectedTime(doors)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: string doors_[] = { "823", 4fd800b3a8 2011-02-23 kinaba: "2630", 4fd800b3a8 2011-02-23 kinaba: "130" } 4fd800b3a8 2011-02-23 kinaba: ; 4fd800b3a8 2011-02-23 kinaba: vector <string> doors(doors_, doors_+sizeof(doors_)/sizeof(*doors_)); 4fd800b3a8 2011-02-23 kinaba: double RetVal = -1.0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, GreatExpectations().expectedTime(doors)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<5>) { 4fd800b3a8 2011-02-23 kinaba: string doors_[] = { "0", 4fd800b3a8 2011-02-23 kinaba: "44", 4fd800b3a8 2011-02-23 kinaba: "4" } 4fd800b3a8 2011-02-23 kinaba: ; 4fd800b3a8 2011-02-23 kinaba: vector <string> doors(doors_, doors_+sizeof(doors_)/sizeof(*doors_)); 4fd800b3a8 2011-02-23 kinaba: double RetVal = 4.0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, GreatExpectations().expectedTime(doors)); } 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: