90dd89d9af 2012-03-17 kinaba: #include <iostream> 90dd89d9af 2012-03-17 kinaba: #include <sstream> 90dd89d9af 2012-03-17 kinaba: #include <iomanip> 90dd89d9af 2012-03-17 kinaba: #include <vector> 90dd89d9af 2012-03-17 kinaba: #include <string> 90dd89d9af 2012-03-17 kinaba: #include <map> 90dd89d9af 2012-03-17 kinaba: #include <set> 90dd89d9af 2012-03-17 kinaba: #include <algorithm> 90dd89d9af 2012-03-17 kinaba: #include <numeric> 90dd89d9af 2012-03-17 kinaba: #include <iterator> 90dd89d9af 2012-03-17 kinaba: #include <functional> 90dd89d9af 2012-03-17 kinaba: #include <complex> 90dd89d9af 2012-03-17 kinaba: #include <queue> 90dd89d9af 2012-03-17 kinaba: #include <stack> 90dd89d9af 2012-03-17 kinaba: #include <cmath> 90dd89d9af 2012-03-17 kinaba: #include <cassert> 90dd89d9af 2012-03-17 kinaba: #include <cstring> 90dd89d9af 2012-03-17 kinaba: #ifdef __GNUC__ 90dd89d9af 2012-03-17 kinaba: #include <ext/hash_map> 90dd89d9af 2012-03-17 kinaba: #define unordered_map __gnu_cxx::hash_map 90dd89d9af 2012-03-17 kinaba: #else 90dd89d9af 2012-03-17 kinaba: #include <unordered_map> 90dd89d9af 2012-03-17 kinaba: #endif 90dd89d9af 2012-03-17 kinaba: using namespace std; 90dd89d9af 2012-03-17 kinaba: typedef long long LL; 90dd89d9af 2012-03-17 kinaba: typedef complex<double> CMP; 90dd89d9af 2012-03-17 kinaba: 90dd89d9af 2012-03-17 kinaba: class MergersDivOne { public: 90dd89d9af 2012-03-17 kinaba: double findMaximum(vector <int> revenues) 90dd89d9af 2012-03-17 kinaba: { 90dd89d9af 2012-03-17 kinaba: vector<double> r(revenues.begin(), revenues.end()); 90dd89d9af 2012-03-17 kinaba: sort(r.begin(), r.end()); 90dd89d9af 2012-03-17 kinaba: return best(r, r.size()); 90dd89d9af 2012-03-17 kinaba: } 90dd89d9af 2012-03-17 kinaba: 90dd89d9af 2012-03-17 kinaba: 90dd89d9af 2012-03-17 kinaba: map<int, double> memo; 90dd89d9af 2012-03-17 kinaba: double best(const vector<double>& r, int N) 90dd89d9af 2012-03-17 kinaba: { 90dd89d9af 2012-03-17 kinaba: if( N == 1 ) 90dd89d9af 2012-03-17 kinaba: return r[0]; 90dd89d9af 2012-03-17 kinaba: if( memo.count(N) ) 90dd89d9af 2012-03-17 kinaba: return memo[N]; 90dd89d9af 2012-03-17 kinaba: 90dd89d9af 2012-03-17 kinaba: double score = -1e+100; 90dd89d9af 2012-03-17 kinaba: for(int k=1; k<N; ++k) { 90dd89d9af 2012-03-17 kinaba: double a = accumulate(r.begin()+k, r.begin()+N, best(r, k)) / (N-k+1); 90dd89d9af 2012-03-17 kinaba: score = max(score, a); 90dd89d9af 2012-03-17 kinaba: } 90dd89d9af 2012-03-17 kinaba: return memo[N] = score; 90dd89d9af 2012-03-17 kinaba: } 90dd89d9af 2012-03-17 kinaba: }; 90dd89d9af 2012-03-17 kinaba: 90dd89d9af 2012-03-17 kinaba: // BEGIN CUT HERE 90dd89d9af 2012-03-17 kinaba: #include <ctime> 90dd89d9af 2012-03-17 kinaba: double start_time; string timer() 90dd89d9af 2012-03-17 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 90dd89d9af 2012-03-17 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 90dd89d9af 2012-03-17 kinaba: { os << "{ "; 90dd89d9af 2012-03-17 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 90dd89d9af 2012-03-17 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 90dd89d9af 2012-03-17 kinaba: void verify_case(const double& Expected, const double& Received) { 90dd89d9af 2012-03-17 kinaba: bool ok = (abs(Expected - Received) < 1e-9); 90dd89d9af 2012-03-17 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 90dd89d9af 2012-03-17 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 90dd89d9af 2012-03-17 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 90dd89d9af 2012-03-17 kinaba: #define END verify_case(_, MergersDivOne().findMaximum(revenues));} 90dd89d9af 2012-03-17 kinaba: int main(){ 90dd89d9af 2012-03-17 kinaba: 90dd89d9af 2012-03-17 kinaba: CASE(0) 90dd89d9af 2012-03-17 kinaba: int revenues_[] = {5, -7, 3}; 90dd89d9af 2012-03-17 kinaba: vector <int> revenues(revenues_, revenues_+sizeof(revenues_)/sizeof(*revenues_)); 90dd89d9af 2012-03-17 kinaba: double _ = 1.5; 90dd89d9af 2012-03-17 kinaba: END 90dd89d9af 2012-03-17 kinaba: CASE(1) 90dd89d9af 2012-03-17 kinaba: int revenues_[] = {10, -17}; 90dd89d9af 2012-03-17 kinaba: vector <int> revenues(revenues_, revenues_+sizeof(revenues_)/sizeof(*revenues_)); 90dd89d9af 2012-03-17 kinaba: double _ = -3.5; 90dd89d9af 2012-03-17 kinaba: END 90dd89d9af 2012-03-17 kinaba: CASE(2) 90dd89d9af 2012-03-17 kinaba: int revenues_[] = {12, 12, 12, 12, 12}; 90dd89d9af 2012-03-17 kinaba: vector <int> revenues(revenues_, revenues_+sizeof(revenues_)/sizeof(*revenues_)); 90dd89d9af 2012-03-17 kinaba: double _ = 12.0; 90dd89d9af 2012-03-17 kinaba: END 90dd89d9af 2012-03-17 kinaba: CASE(3) 90dd89d9af 2012-03-17 kinaba: int revenues_[] = {0, 0, 0, 0, 0, 100}; 90dd89d9af 2012-03-17 kinaba: vector <int> revenues(revenues_, revenues_+sizeof(revenues_)/sizeof(*revenues_)); 90dd89d9af 2012-03-17 kinaba: double _ = 50.0; 90dd89d9af 2012-03-17 kinaba: END 90dd89d9af 2012-03-17 kinaba: CASE(4) 90dd89d9af 2012-03-17 kinaba: int revenues_[] = {10, -10, 100, -100, 1000, -1000}; 90dd89d9af 2012-03-17 kinaba: vector <int> revenues(revenues_, revenues_+sizeof(revenues_)/sizeof(*revenues_)); 90dd89d9af 2012-03-17 kinaba: double _ = 491.25; 90dd89d9af 2012-03-17 kinaba: END 90dd89d9af 2012-03-17 kinaba: CASE(5) 90dd89d9af 2012-03-17 kinaba: int revenues_[] = {0,0,100,100}; 90dd89d9af 2012-03-17 kinaba: vector <int> revenues(revenues_, revenues_+sizeof(revenues_)/sizeof(*revenues_)); 90dd89d9af 2012-03-17 kinaba: double _ = -1; 90dd89d9af 2012-03-17 kinaba: END 90dd89d9af 2012-03-17 kinaba: /* 90dd89d9af 2012-03-17 kinaba: CASE(6) 90dd89d9af 2012-03-17 kinaba: int revenues_[] = ; 90dd89d9af 2012-03-17 kinaba: vector <int> revenues(revenues_, revenues_+sizeof(revenues_)/sizeof(*revenues_)); 90dd89d9af 2012-03-17 kinaba: double _ = ; 90dd89d9af 2012-03-17 kinaba: END 90dd89d9af 2012-03-17 kinaba: */ 90dd89d9af 2012-03-17 kinaba: } 90dd89d9af 2012-03-17 kinaba: // END CUT HERE