ADDED SRM/642-U/1A.cpp Index: SRM/642-U/1A.cpp ================================================================== --- SRM/642-U/1A.cpp +++ SRM/642-U/1A.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class WaitingForBus { public: + double whenWillBusArrive(vector time, vector prob, int s) + { + const int WAIT_TIME_MAX = 100000; + + deque p(WAIT_TIME_MAX+1, 0.0); + p[0] = 1.0; + for(int t=1; t<=s; ++t) { + double p0 = p[0]; + p.pop_front(); + p.push_back(0.0); + for(int i=0; i +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::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(_, WaitingForBus().whenWillBusArrive(time, prob, s));} +int main(){ + +CASE(0) + int time_[] = {5,100}; + vector time(time_, time_+sizeof(time_)/sizeof(*time_)); + int prob_[] = {90,10}; + vector prob(prob_, prob_+sizeof(prob_)/sizeof(*prob_)); + int s = 5; + double _ = 9.5; +END +CASE(1) + int time_[] = {5}; + vector time(time_, time_+sizeof(time_)/sizeof(*time_)); + int prob_[] = {100}; + vector prob(prob_, prob_+sizeof(prob_)/sizeof(*prob_)); + int s = 101; + double _ = 4.0; +END +CASE(2) + int time_[] = {5,10}; + vector time(time_, time_+sizeof(time_)/sizeof(*time_)); + int prob_[] = {50,50}; + vector prob(prob_, prob_+sizeof(prob_)/sizeof(*prob_)); + int s = 88888; + double _ = 3.666666666666667; +END +CASE(3) + int time_[] = {1,2,3,4}; + vector time(time_, time_+sizeof(time_)/sizeof(*time_)); + int prob_[] = {10,20,30,40}; + vector prob(prob_, prob_+sizeof(prob_)/sizeof(*prob_)); + int s = 1000; + double _ = 1.166666666666667; +END +CASE(4) + int time_[] = {10,100,1000,10000,100000}; + vector time(time_, time_+sizeof(time_)/sizeof(*time_)); + int prob_[] = {90,4,3,2,1}; + vector prob(prob_, prob_+sizeof(prob_)/sizeof(*prob_)); + int s = 100000; + double _ = 21148.147303578935; +END +/* +CASE(5) + int time_[] = ; + vector time(time_, time_+sizeof(time_)/sizeof(*time_)); + int prob_[] = ; + vector prob(prob_, prob_+sizeof(prob_)/sizeof(*prob_)); + int s = ; + double _ = ; +END +CASE(6) + int time_[] = ; + vector time(time_, time_+sizeof(time_)/sizeof(*time_)); + int prob_[] = ; + vector prob(prob_, prob_+sizeof(prob_)/sizeof(*prob_)); + int s = ; + double _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/642-U/1C-U.cpp Index: SRM/642-U/1C-U.cpp ================================================================== --- SRM/642-U/1C-U.cpp +++ SRM/642-U/1C-U.cpp @@ -0,0 +1,196 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +template +struct DP2 +{ + const int N1, N2; + vector data; + DP2(int N1, int N2, const T& t = T()) + : N1(N1), N2(N2), data(N1*N2, t) { assert(data.size()*sizeof(T)<(1<<28)); } + T& operator()(int i1, int i2) + { return data[ (i1*N2)+i2 ]; } + void swap(DP2& rhs) + { data.swap(rhs.data); } +}; + +template +struct DP3 +{ + int N1, N2, N3; + vector data; + DP3(int N1, int N2, int N3, const T& t = T()) + : N1(N1), N2(N2), N3(N3), data(N1*N2*N3, t) { assert(data.size()*sizeof(T)<(1<<28)); } + T& operator()(int i1, int i2, int i3) + { return data[ ((i1*N2)+i2)*N3+i3 ]; } + void swap(DP3& rhs) + { data.swap(rhs.data); } +}; + +class WheelofFortune { public: + double maxExpectedValue(int N, vector s) + { + const int SCORE_MAX = s.size(); + + // wlog Alice always declares "0". + + // ps(i+1, a) := probability that "0" is covered |a| times after i-th try. + // es(i+1, a) := expected score distrobution + DP2 ps(s.size()+1, SCORE_MAX+1, 0.0); + DP3 es(s.size()+1, SCORE_MAX+1, N, 0.0); + ps(0, 0) = 1.0; + + vector> memo1(N+1, vector(N, -1)); + vector> memo2(N+1, vector(N, -1)); + + for(int i=0; i>& memo) + { + if(memo[s][cell] >= 0) + return memo[s][cell]; + + int cnt = 0, tot = 0; + for(int b=0; b>& memo) + { + if(memo[s][cell] >= 0) + return memo[s][cell]; + + int cnt = 0, tot=0; + for(int b=0; b +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::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(_, WheelofFortune().maxExpectedValue(N, s));} +int main(){ + +CASE(0) + int N = 4; + int s_[] = {2}; + vector s(s_, s_+sizeof(s_)/sizeof(*s_)); + double _ = 1.25; +END +CASE(1) + int N = 6; + int s_[] = {1,1,1,1,1,1}; + vector s(s_, s_+sizeof(s_)/sizeof(*s_)); + double _ = 2.0000000000000004; +END +CASE(2) + int N = 20; + int s_[] = {1,20,1,20,1}; + vector s(s_, s_+sizeof(s_)/sizeof(*s_)); + double _ = 4.299999999999999; +END +CASE(3) + int N = 10; + int s_[] = {3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6,4,3,3,8,3,2,7,9,5}; + vector s(s_, s_+sizeof(s_)/sizeof(*s_)); + double _ = 31.973469385798197; +END +CASE(4) + int N = 15; + int s_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; + vector s(s_, s_+sizeof(s_)/sizeof(*s_)); + double _ = 16.691531334568044; +END +/* +CASE(5) + int N = ; + int s_[] = ; + vector s(s_, s_+sizeof(s_)/sizeof(*s_)); + double _ = ; +END +CASE(6) + int N = ; + int s_[] = ; + vector s(s_, s_+sizeof(s_)/sizeof(*s_)); + double _ = ; +END +*/ +} +// END CUT HERE