File Annotation
Not logged in
3d2bcff745 2011-12-29        kinaba: #include <iostream>
3d2bcff745 2011-12-29        kinaba: #include <sstream>
3d2bcff745 2011-12-29        kinaba: #include <iomanip>
3d2bcff745 2011-12-29        kinaba: #include <vector>
3d2bcff745 2011-12-29        kinaba: #include <string>
3d2bcff745 2011-12-29        kinaba: #include <map>
3d2bcff745 2011-12-29        kinaba: #include <set>
3d2bcff745 2011-12-29        kinaba: #include <algorithm>
3d2bcff745 2011-12-29        kinaba: #include <numeric>
3d2bcff745 2011-12-29        kinaba: #include <iterator>
3d2bcff745 2011-12-29        kinaba: #include <functional>
3d2bcff745 2011-12-29        kinaba: #include <complex>
3d2bcff745 2011-12-29        kinaba: #include <queue>
3d2bcff745 2011-12-29        kinaba: #include <stack>
3d2bcff745 2011-12-29        kinaba: #include <cmath>
3d2bcff745 2011-12-29        kinaba: #include <cassert>
3d2bcff745 2011-12-29        kinaba: #include <cstring>
3d2bcff745 2011-12-29        kinaba: #ifdef __GNUC__
3d2bcff745 2011-12-29        kinaba: #include <ext/hash_map>
3d2bcff745 2011-12-29        kinaba: #define unordered_map __gnu_cxx::hash_map
3d2bcff745 2011-12-29        kinaba: #else
3d2bcff745 2011-12-29        kinaba: #include <unordered_map>
3d2bcff745 2011-12-29        kinaba: #endif
3d2bcff745 2011-12-29        kinaba: using namespace std;
3d2bcff745 2011-12-29        kinaba: typedef long long LL;
3d2bcff745 2011-12-29        kinaba: typedef complex<double> CMP;
3d2bcff745 2011-12-29        kinaba: 
3d2bcff745 2011-12-29        kinaba: template<typename T>
3d2bcff745 2011-12-29        kinaba: struct DP2x
3d2bcff745 2011-12-29        kinaba: {
3d2bcff745 2011-12-29        kinaba: 	const int N1, N2;
3d2bcff745 2011-12-29        kinaba: 	vector<T> data;
3d2bcff745 2011-12-29        kinaba: 	DP2x(int, int N2, const T& t = T())
3d2bcff745 2011-12-29        kinaba: 		: N1(2), N2(N2), data(N1*N2, t) { assert(data.size()*sizeof(T)<(1<<26)); }
3d2bcff745 2011-12-29        kinaba: 	T& operator()(int i1, int i2)
3d2bcff745 2011-12-29        kinaba: 		{ i1&=1; return data[ (i1*N2)+i2 ]; }
3d2bcff745 2011-12-29        kinaba: 	void swap(DP2x& rhs)
3d2bcff745 2011-12-29        kinaba: 		{ data.swap(rhs.data); }
3d2bcff745 2011-12-29        kinaba: };
3d2bcff745 2011-12-29        kinaba: 
3d2bcff745 2011-12-29        kinaba: class ColorfulCookie { public:
3d2bcff745 2011-12-29        kinaba: 	int getMaximum(vector <int> cookies, int P1, int P2)
3d2bcff745 2011-12-29        kinaba: 	{
3d2bcff745 2011-12-29        kinaba: 		int L = 0;
3d2bcff745 2011-12-29        kinaba: 		int R = accumulate(cookies.begin(), cookies.end(), 0) / (P1+P2) + 1;
3d2bcff745 2011-12-29        kinaba: 		while( R-L > 1 ) { // [L, R)
3d2bcff745 2011-12-29        kinaba: 			int C = (L+R) / 2;
3d2bcff745 2011-12-29        kinaba: 			(possible(cookies, P1, P2, C) ? L : R) = C;
3d2bcff745 2011-12-29        kinaba: 		}
3d2bcff745 2011-12-29        kinaba: 		return L * (P1+P2);
3d2bcff745 2011-12-29        kinaba: 	}
3d2bcff745 2011-12-29        kinaba: 
3d2bcff745 2011-12-29        kinaba: 	bool possible(const vector<int>& C, int P1, int P2, int Times)
3d2bcff745 2011-12-29        kinaba: 	{
3d2bcff745 2011-12-29        kinaba: 		DP2x<int> maxP2forP1(C.size()+1, Times+1, -1);
3d2bcff745 2011-12-29        kinaba: 		maxP2forP1(0,0) = 0;
3d2bcff745 2011-12-29        kinaba: 		for(int i=0; i<C.size(); ++i)
3d2bcff745 2011-12-29        kinaba: 			for(int n=0; n<=Times; ++n) {
3d2bcff745 2011-12-29        kinaba: 				maxP2forP1(i+1,n) = maxP2forP1(i,n);
3d2bcff745 2011-12-29        kinaba: 				for(int a=0; a<=n; ++a) {
3d2bcff745 2011-12-29        kinaba: 					if( n-a>=0 && maxP2forP1(i,n-a)>=0 && C[i]-a*P1>=0 )
3d2bcff745 2011-12-29        kinaba: 						maxP2forP1(i+1,n) = max(maxP2forP1(i+1,n),
3d2bcff745 2011-12-29        kinaba: 							maxP2forP1(i,n-a) + min(Times-a,(C[i]-a*P1)/P2)
3d2bcff745 2011-12-29        kinaba: 						);
3d2bcff745 2011-12-29        kinaba: 				}
3d2bcff745 2011-12-29        kinaba: 			}
3d2bcff745 2011-12-29        kinaba: 		return maxP2forP1(C.size(), Times) >= Times;
3d2bcff745 2011-12-29        kinaba: 	}
3d2bcff745 2011-12-29        kinaba: };
3d2bcff745 2011-12-29        kinaba: 
3d2bcff745 2011-12-29        kinaba: // BEGIN CUT HERE
3d2bcff745 2011-12-29        kinaba: #include <ctime>
3d2bcff745 2011-12-29        kinaba: double start_time; string timer()
3d2bcff745 2011-12-29        kinaba:  { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
3d2bcff745 2011-12-29        kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
3d2bcff745 2011-12-29        kinaba:  { os << "{ ";
3d2bcff745 2011-12-29        kinaba:    for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
3d2bcff745 2011-12-29        kinaba:    os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
3d2bcff745 2011-12-29        kinaba: void verify_case(const int& Expected, const int& Received) {
3d2bcff745 2011-12-29        kinaba:  bool ok = (Expected == Received);
3d2bcff745 2011-12-29        kinaba:  if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
3d2bcff745 2011-12-29        kinaba:  cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
3d2bcff745 2011-12-29        kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
3d2bcff745 2011-12-29        kinaba: #define END	 verify_case(_, ColorfulCookie().getMaximum(cookies, P1, P2));}
3d2bcff745 2011-12-29        kinaba: int main(){
3d2bcff745 2011-12-29        kinaba: 
3d2bcff745 2011-12-29        kinaba: CASE(0)
3d2bcff745 2011-12-29        kinaba: 	int cookies_[] = {100, 100};
3d2bcff745 2011-12-29        kinaba: 	  vector <int> cookies(cookies_, cookies_+sizeof(cookies_)/sizeof(*cookies_));
3d2bcff745 2011-12-29        kinaba: 	int P1 = 50;
3d2bcff745 2011-12-29        kinaba: 	int P2 = 50;
3d2bcff745 2011-12-29        kinaba: 	int _ = 200;
3d2bcff745 2011-12-29        kinaba: END
3d2bcff745 2011-12-29        kinaba: CASE(1)
3d2bcff745 2011-12-29        kinaba: 	int cookies_[] = {50, 250, 50};
3d2bcff745 2011-12-29        kinaba: 	  vector <int> cookies(cookies_, cookies_+sizeof(cookies_)/sizeof(*cookies_));
3d2bcff745 2011-12-29        kinaba: 	int P1 = 50;
3d2bcff745 2011-12-29        kinaba: 	int P2 = 100;
3d2bcff745 2011-12-29        kinaba: 	int _ = 300;
3d2bcff745 2011-12-29        kinaba: END
3d2bcff745 2011-12-29        kinaba: CASE(2)
3d2bcff745 2011-12-29        kinaba: 	int cookies_[] = {2000};
3d2bcff745 2011-12-29        kinaba: 	  vector <int> cookies(cookies_, cookies_+sizeof(cookies_)/sizeof(*cookies_));
3d2bcff745 2011-12-29        kinaba: 	int P1 = 100;
3d2bcff745 2011-12-29        kinaba: 	int P2 = 200;
3d2bcff745 2011-12-29        kinaba: 	int _ = 0;
3d2bcff745 2011-12-29        kinaba: END
3d2bcff745 2011-12-29        kinaba: CASE(3)
3d2bcff745 2011-12-29        kinaba: 	int cookies_[] = {123, 456, 789, 555};
3d2bcff745 2011-12-29        kinaba: 	  vector <int> cookies(cookies_, cookies_+sizeof(cookies_)/sizeof(*cookies_));
3d2bcff745 2011-12-29        kinaba: 	int P1 = 58;
3d2bcff745 2011-12-29        kinaba: 	int P2 = 158;
3d2bcff745 2011-12-29        kinaba: 	int _ = 1728;
3d2bcff745 2011-12-29        kinaba: END
3d2bcff745 2011-12-29        kinaba: /*
3d2bcff745 2011-12-29        kinaba: CASE(4)
3d2bcff745 2011-12-29        kinaba: 	int cookies_[] = ;
3d2bcff745 2011-12-29        kinaba: 	  vector <int> cookies(cookies_, cookies_+sizeof(cookies_)/sizeof(*cookies_));
3d2bcff745 2011-12-29        kinaba: 	int P1 = ;
3d2bcff745 2011-12-29        kinaba: 	int P2 = ;
3d2bcff745 2011-12-29        kinaba: 	int _ = ;
3d2bcff745 2011-12-29        kinaba: END
3d2bcff745 2011-12-29        kinaba: CASE(5)
3d2bcff745 2011-12-29        kinaba: 	int cookies_[] = ;
3d2bcff745 2011-12-29        kinaba: 	  vector <int> cookies(cookies_, cookies_+sizeof(cookies_)/sizeof(*cookies_));
3d2bcff745 2011-12-29        kinaba: 	int P1 = ;
3d2bcff745 2011-12-29        kinaba: 	int P2 = ;
3d2bcff745 2011-12-29        kinaba: 	int _ = ;
3d2bcff745 2011-12-29        kinaba: END
3d2bcff745 2011-12-29        kinaba: */
3d2bcff745 2011-12-29        kinaba: }
3d2bcff745 2011-12-29        kinaba: // END CUT HERE