e16cea6bfe 2012-02-19 kinaba: #include <iostream> e16cea6bfe 2012-02-19 kinaba: #include <sstream> e16cea6bfe 2012-02-19 kinaba: #include <iomanip> e16cea6bfe 2012-02-19 kinaba: #include <vector> e16cea6bfe 2012-02-19 kinaba: #include <string> e16cea6bfe 2012-02-19 kinaba: #include <map> e16cea6bfe 2012-02-19 kinaba: #include <set> e16cea6bfe 2012-02-19 kinaba: #include <algorithm> e16cea6bfe 2012-02-19 kinaba: #include <numeric> e16cea6bfe 2012-02-19 kinaba: #include <iterator> e16cea6bfe 2012-02-19 kinaba: #include <functional> e16cea6bfe 2012-02-19 kinaba: #include <complex> e16cea6bfe 2012-02-19 kinaba: #include <queue> e16cea6bfe 2012-02-19 kinaba: #include <stack> e16cea6bfe 2012-02-19 kinaba: #include <cmath> e16cea6bfe 2012-02-19 kinaba: #include <cassert> e16cea6bfe 2012-02-19 kinaba: #include <cstring> e16cea6bfe 2012-02-19 kinaba: #ifdef __GNUC__ e16cea6bfe 2012-02-19 kinaba: #include <ext/hash_map> e16cea6bfe 2012-02-19 kinaba: #define unordered_map __gnu_cxx::hash_map e16cea6bfe 2012-02-19 kinaba: #else e16cea6bfe 2012-02-19 kinaba: #include <unordered_map> e16cea6bfe 2012-02-19 kinaba: #endif e16cea6bfe 2012-02-19 kinaba: using namespace std; e16cea6bfe 2012-02-19 kinaba: typedef long long LL; e16cea6bfe 2012-02-19 kinaba: typedef complex<double> CMP; e16cea6bfe 2012-02-19 kinaba: e16cea6bfe 2012-02-19 kinaba: template<typename T> e16cea6bfe 2012-02-19 kinaba: struct DP2 e16cea6bfe 2012-02-19 kinaba: { e16cea6bfe 2012-02-19 kinaba: const int N1, N2; e16cea6bfe 2012-02-19 kinaba: vector<T> data; e16cea6bfe 2012-02-19 kinaba: DP2(int N1, int N2, const T& t = T()) e16cea6bfe 2012-02-19 kinaba: : N1(N1), N2(N2), data(N1*N2, t) { assert(data.size()*sizeof(T)<(1<<26)); } e16cea6bfe 2012-02-19 kinaba: T& operator()(int i1, int i2) e16cea6bfe 2012-02-19 kinaba: { return data[ (i1*N2)+i2 ]; } e16cea6bfe 2012-02-19 kinaba: void swap(DP2& rhs) e16cea6bfe 2012-02-19 kinaba: { data.swap(rhs.data); } e16cea6bfe 2012-02-19 kinaba: }; e16cea6bfe 2012-02-19 kinaba: e16cea6bfe 2012-02-19 kinaba: class MagicalGirl { public: e16cea6bfe 2012-02-19 kinaba: double maxExpectation(int M, vector <int> day, vector <int> win, vector <int> gain) e16cea6bfe 2012-02-19 kinaba: { e16cea6bfe 2012-02-19 kinaba: vector< pair< int, pair<double, int> > > majos; e16cea6bfe 2012-02-19 kinaba: for(int i=0; i<day.size(); ++i) e16cea6bfe 2012-02-19 kinaba: majos.push_back( make_pair(day[i], make_pair(win[i]/100.0, gain[i])) ); e16cea6bfe 2012-02-19 kinaba: sort(majos.begin(), majos.end()); e16cea6bfe 2012-02-19 kinaba: e16cea6bfe 2012-02-19 kinaba: DP2<double> dp(day.size()+1, M+1); e16cea6bfe 2012-02-19 kinaba: for(int i=majos.size(); i>=0; --i) e16cea6bfe 2012-02-19 kinaba: for(int hp=0; hp<=M; ++hp) e16cea6bfe 2012-02-19 kinaba: { e16cea6bfe 2012-02-19 kinaba: // Days passed from the last Witch's attack. e16cea6bfe 2012-02-19 kinaba: const int days = i==majos.size() ? INT_MAX e16cea6bfe 2012-02-19 kinaba: : majos[i].first - (i==0 ? 0 : majos[i-1].first); e16cea6bfe 2012-02-19 kinaba: if( hp <= days ) e16cea6bfe 2012-02-19 kinaba: dp(i, hp) = hp; // no way to gain power any more... e16cea6bfe 2012-02-19 kinaba: else { e16cea6bfe 2012-02-19 kinaba: const double pWin = majos[i].second.first; e16cea6bfe 2012-02-19 kinaba: const int gain = majos[i].second.second; e16cea6bfe 2012-02-19 kinaba: dp(i, hp) = days + max(pWin*dp(i+1, min(M, hp-days+gain)), // fight or e16cea6bfe 2012-02-19 kinaba: dp(i+1, min(M, hp-days))); // skip e16cea6bfe 2012-02-19 kinaba: } e16cea6bfe 2012-02-19 kinaba: } e16cea6bfe 2012-02-19 kinaba: return dp(0, M); e16cea6bfe 2012-02-19 kinaba: } e16cea6bfe 2012-02-19 kinaba: }; e16cea6bfe 2012-02-19 kinaba: e16cea6bfe 2012-02-19 kinaba: // BEGIN CUT HERE e16cea6bfe 2012-02-19 kinaba: #include <ctime> e16cea6bfe 2012-02-19 kinaba: double start_time; string timer() e16cea6bfe 2012-02-19 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } e16cea6bfe 2012-02-19 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) e16cea6bfe 2012-02-19 kinaba: { os << "{ "; e16cea6bfe 2012-02-19 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) e16cea6bfe 2012-02-19 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } e16cea6bfe 2012-02-19 kinaba: void verify_case(const double& Expected, const double& Received) { e16cea6bfe 2012-02-19 kinaba: bool ok = (abs(Expected - Received) < 1e-9); e16cea6bfe 2012-02-19 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; e16cea6bfe 2012-02-19 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } e16cea6bfe 2012-02-19 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); e16cea6bfe 2012-02-19 kinaba: #define END verify_case(_, MagicalGirl().maxExpectation(M, day, win, gain));} e16cea6bfe 2012-02-19 kinaba: int main(){ e16cea6bfe 2012-02-19 kinaba: e16cea6bfe 2012-02-19 kinaba: CASE(0) e16cea6bfe 2012-02-19 kinaba: int M = 2; e16cea6bfe 2012-02-19 kinaba: int day_[] = {1}; e16cea6bfe 2012-02-19 kinaba: vector <int> day(day_, day_+sizeof(day_)/sizeof(*day_)); e16cea6bfe 2012-02-19 kinaba: int win_[] = {75}; e16cea6bfe 2012-02-19 kinaba: vector <int> win(win_, win_+sizeof(win_)/sizeof(*win_)); e16cea6bfe 2012-02-19 kinaba: int gain_[] = {1}; e16cea6bfe 2012-02-19 kinaba: vector <int> gain(gain_, gain_+sizeof(gain_)/sizeof(*gain_)); e16cea6bfe 2012-02-19 kinaba: double _ = 2.5; e16cea6bfe 2012-02-19 kinaba: END e16cea6bfe 2012-02-19 kinaba: CASE(1) e16cea6bfe 2012-02-19 kinaba: int M = 10; e16cea6bfe 2012-02-19 kinaba: int day_[] = {5,5,5,5}; e16cea6bfe 2012-02-19 kinaba: vector <int> day(day_, day_+sizeof(day_)/sizeof(*day_)); e16cea6bfe 2012-02-19 kinaba: int win_[] = {100,100,100,100}; e16cea6bfe 2012-02-19 kinaba: vector <int> win(win_, win_+sizeof(win_)/sizeof(*win_)); e16cea6bfe 2012-02-19 kinaba: int gain_[] = {1,1,1,1}; e16cea6bfe 2012-02-19 kinaba: vector <int> gain(gain_, gain_+sizeof(gain_)/sizeof(*gain_)); e16cea6bfe 2012-02-19 kinaba: double _ = 14.0; e16cea6bfe 2012-02-19 kinaba: END e16cea6bfe 2012-02-19 kinaba: CASE(2) e16cea6bfe 2012-02-19 kinaba: int M = 10; e16cea6bfe 2012-02-19 kinaba: int day_[] = {5,5,5,5,5}; e16cea6bfe 2012-02-19 kinaba: vector <int> day(day_, day_+sizeof(day_)/sizeof(*day_)); e16cea6bfe 2012-02-19 kinaba: int win_[] = {40,80,60,20,0}; e16cea6bfe 2012-02-19 kinaba: vector <int> win(win_, win_+sizeof(win_)/sizeof(*win_)); e16cea6bfe 2012-02-19 kinaba: int gain_[] = {10,10,10,10,10}; e16cea6bfe 2012-02-19 kinaba: vector <int> gain(gain_, gain_+sizeof(gain_)/sizeof(*gain_)); e16cea6bfe 2012-02-19 kinaba: double _ = 13.0; e16cea6bfe 2012-02-19 kinaba: END e16cea6bfe 2012-02-19 kinaba: CASE(3) e16cea6bfe 2012-02-19 kinaba: int M = 2; e16cea6bfe 2012-02-19 kinaba: int day_[] = {2}; e16cea6bfe 2012-02-19 kinaba: vector <int> day(day_, day_+sizeof(day_)/sizeof(*day_)); e16cea6bfe 2012-02-19 kinaba: int win_[] = {100}; e16cea6bfe 2012-02-19 kinaba: vector <int> win(win_, win_+sizeof(win_)/sizeof(*win_)); e16cea6bfe 2012-02-19 kinaba: int gain_[] = {2}; e16cea6bfe 2012-02-19 kinaba: vector <int> gain(gain_, gain_+sizeof(gain_)/sizeof(*gain_)); e16cea6bfe 2012-02-19 kinaba: double _ = 2.0; e16cea6bfe 2012-02-19 kinaba: END e16cea6bfe 2012-02-19 kinaba: CASE(4) e16cea6bfe 2012-02-19 kinaba: int M = 20; e16cea6bfe 2012-02-19 kinaba: int day_[] = {2,13,8,7,9,4,6,21}; e16cea6bfe 2012-02-19 kinaba: vector <int> day(day_, day_+sizeof(day_)/sizeof(*day_)); e16cea6bfe 2012-02-19 kinaba: int win_[] = {18,83,75,23,45,23,10,98}; e16cea6bfe 2012-02-19 kinaba: vector <int> win(win_, win_+sizeof(win_)/sizeof(*win_)); e16cea6bfe 2012-02-19 kinaba: int gain_[] = {10,9,8,10,7,16,13,20}; e16cea6bfe 2012-02-19 kinaba: vector <int> gain(gain_, gain_+sizeof(gain_)/sizeof(*gain_)); e16cea6bfe 2012-02-19 kinaba: double _ = 35.908; e16cea6bfe 2012-02-19 kinaba: END e16cea6bfe 2012-02-19 kinaba: CASE(5) e16cea6bfe 2012-02-19 kinaba: int M = 11; e16cea6bfe 2012-02-19 kinaba: int day_[] = {10,20,30,40,50,60,70,80,90}; e16cea6bfe 2012-02-19 kinaba: vector <int> day(day_, day_+sizeof(day_)/sizeof(*day_)); e16cea6bfe 2012-02-19 kinaba: int win_[] = {100,100,100,100,100,100,100,100,100}; e16cea6bfe 2012-02-19 kinaba: vector <int> win(win_, win_+sizeof(win_)/sizeof(*win_)); e16cea6bfe 2012-02-19 kinaba: int gain_[] = {10,10,10,10,10,10,10,10,10}; e16cea6bfe 2012-02-19 kinaba: vector <int> gain(gain_, gain_+sizeof(gain_)/sizeof(*gain_)); e16cea6bfe 2012-02-19 kinaba: double _ = 101.0; e16cea6bfe 2012-02-19 kinaba: END e16cea6bfe 2012-02-19 kinaba: CASE(6) e16cea6bfe 2012-02-19 kinaba: int M = 100000; e16cea6bfe 2012-02-19 kinaba: int day_[] = {1000000}; e16cea6bfe 2012-02-19 kinaba: vector <int> day(day_, day_+sizeof(day_)/sizeof(*day_)); e16cea6bfe 2012-02-19 kinaba: int win_[] = {100}; e16cea6bfe 2012-02-19 kinaba: vector <int> win(win_, win_+sizeof(win_)/sizeof(*win_)); e16cea6bfe 2012-02-19 kinaba: int gain_[] = {100000}; e16cea6bfe 2012-02-19 kinaba: vector <int> gain(gain_, gain_+sizeof(gain_)/sizeof(*gain_)); e16cea6bfe 2012-02-19 kinaba: double _ = 100000.0; e16cea6bfe 2012-02-19 kinaba: END e16cea6bfe 2012-02-19 kinaba: /* e16cea6bfe 2012-02-19 kinaba: CASE(7) e16cea6bfe 2012-02-19 kinaba: int M = ; e16cea6bfe 2012-02-19 kinaba: int day_[] = ; e16cea6bfe 2012-02-19 kinaba: vector <int> day(day_, day_+sizeof(day_)/sizeof(*day_)); e16cea6bfe 2012-02-19 kinaba: int win_[] = ; e16cea6bfe 2012-02-19 kinaba: vector <int> win(win_, win_+sizeof(win_)/sizeof(*win_)); e16cea6bfe 2012-02-19 kinaba: int gain_[] = ; e16cea6bfe 2012-02-19 kinaba: vector <int> gain(gain_, gain_+sizeof(gain_)/sizeof(*gain_)); e16cea6bfe 2012-02-19 kinaba: double _ = ; e16cea6bfe 2012-02-19 kinaba: END e16cea6bfe 2012-02-19 kinaba: CASE(8) e16cea6bfe 2012-02-19 kinaba: int M = ; e16cea6bfe 2012-02-19 kinaba: int day_[] = ; e16cea6bfe 2012-02-19 kinaba: vector <int> day(day_, day_+sizeof(day_)/sizeof(*day_)); e16cea6bfe 2012-02-19 kinaba: int win_[] = ; e16cea6bfe 2012-02-19 kinaba: vector <int> win(win_, win_+sizeof(win_)/sizeof(*win_)); e16cea6bfe 2012-02-19 kinaba: int gain_[] = ; e16cea6bfe 2012-02-19 kinaba: vector <int> gain(gain_, gain_+sizeof(gain_)/sizeof(*gain_)); e16cea6bfe 2012-02-19 kinaba: double _ = ; e16cea6bfe 2012-02-19 kinaba: END e16cea6bfe 2012-02-19 kinaba: */ e16cea6bfe 2012-02-19 kinaba: } e16cea6bfe 2012-02-19 kinaba: // END CUT HERE