Artifact Content
Not logged in

Artifact a01ad9bff4f4fef14182ee26c85923e12d3e22b6


#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>
#ifdef __GNUC__
#include <ext/hash_map>
#define unordered_map __gnu_cxx::hash_map
#else
#include <unordered_map>
#endif
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class MergersDivOne { public:
	double findMaximum(vector <int> revenues)
	{
		vector<double> r(revenues.begin(), revenues.end());
		sort(r.begin(), r.end());
		return best(r, r.size());
	}


	map<int, double> memo;
	double best(const vector<double>& r, int N)
	{
		if( N == 1 )
			return r[0];
		if( memo.count(N) ) 
			return memo[N];

		double score = -1e+100;
		for(int k=1; k<N; ++k) {
			double a = accumulate(r.begin()+k, r.begin()+N, best(r, k)) / (N-k+1);
			score = max(score, a);
		}
		return memo[N] = score;
	}
};

// 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(_, MergersDivOne().findMaximum(revenues));}
int main(){

CASE(0)
	int revenues_[] = {5, -7, 3};
	  vector <int> revenues(revenues_, revenues_+sizeof(revenues_)/sizeof(*revenues_)); 
	double _ = 1.5; 
END
CASE(1)
	int revenues_[] = {10, -17};
	  vector <int> revenues(revenues_, revenues_+sizeof(revenues_)/sizeof(*revenues_)); 
	double _ = -3.5; 
END
CASE(2)
	int revenues_[] = {12, 12, 12, 12, 12};
	  vector <int> revenues(revenues_, revenues_+sizeof(revenues_)/sizeof(*revenues_)); 
	double _ = 12.0; 
END
CASE(3)
	int revenues_[] = {0, 0, 0, 0, 0, 100};
	  vector <int> revenues(revenues_, revenues_+sizeof(revenues_)/sizeof(*revenues_)); 
	double _ = 50.0; 
END
CASE(4)
	int revenues_[] = {10, -10, 100, -100, 1000, -1000};
	  vector <int> revenues(revenues_, revenues_+sizeof(revenues_)/sizeof(*revenues_)); 
	double _ = 491.25; 
END
CASE(5)
int revenues_[] = {0,0,100,100};
	  vector <int> revenues(revenues_, revenues_+sizeof(revenues_)/sizeof(*revenues_)); 
	double _ = -1; 
END
/*
CASE(6)
	int revenues_[] = ;
	  vector <int> revenues(revenues_, revenues_+sizeof(revenues_)/sizeof(*revenues_)); 
	double _ = ; 
END
*/
}
// END CUT HERE