File Annotation
Not logged in
4fd800b3a8 2011-02-23        kinaba: #include <iostream>
4fd800b3a8 2011-02-23        kinaba: #include <sstream>
4fd800b3a8 2011-02-23        kinaba: #include <iomanip>
4fd800b3a8 2011-02-23        kinaba: #include <vector>
4fd800b3a8 2011-02-23        kinaba: #include <string>
4fd800b3a8 2011-02-23        kinaba: #include <map>
4fd800b3a8 2011-02-23        kinaba: #include <set>
4fd800b3a8 2011-02-23        kinaba: #include <algorithm>
4fd800b3a8 2011-02-23        kinaba: #include <numeric>
4fd800b3a8 2011-02-23        kinaba: #include <iterator>
4fd800b3a8 2011-02-23        kinaba: #include <functional>
4fd800b3a8 2011-02-23        kinaba: #include <complex>
4fd800b3a8 2011-02-23        kinaba: #include <queue>
4fd800b3a8 2011-02-23        kinaba: #include <stack>
4fd800b3a8 2011-02-23        kinaba: #include <cmath>
4fd800b3a8 2011-02-23        kinaba: #include <cassert>
4fd800b3a8 2011-02-23        kinaba: #include <cstring>
4fd800b3a8 2011-02-23        kinaba: using namespace std;
4fd800b3a8 2011-02-23        kinaba: typedef long long LL;
4fd800b3a8 2011-02-23        kinaba: typedef complex<double> CMP;
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: class BaronsAndCoins { public:
4fd800b3a8 2011-02-23        kinaba: 	int getMaximum(vector <int> x, vector <int> y)
4fd800b3a8 2011-02-23        kinaba: 	{
4fd800b3a8 2011-02-23        kinaba: 		vector< pair<int,int> > P;
4fd800b3a8 2011-02-23        kinaba: 		for(int i=0; i<x.size(); ++i)
4fd800b3a8 2011-02-23        kinaba: 			P.push_back( make_pair(y[i], x[i]) );
4fd800b3a8 2011-02-23        kinaba: 		P.push_back( make_pair(0, 0) );
4fd800b3a8 2011-02-23        kinaba: 		sort( P.begin(), P.end() );
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 		return maxDepth(0, 0, P);
4fd800b3a8 2011-02-23        kinaba: 	}
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 	map<pair<int,int>,int> memo;
4fd800b3a8 2011-02-23        kinaba: 	int maxDepth( int i, int k, vector< pair<int,int> >& P )
4fd800b3a8 2011-02-23        kinaba: 	{
4fd800b3a8 2011-02-23        kinaba: 		pair<int,int> key(i,k);
4fd800b3a8 2011-02-23        kinaba: 		if( memo.count(key) )
4fd800b3a8 2011-02-23        kinaba: 			return memo[key];
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 		int md = 0;
4fd800b3a8 2011-02-23        kinaba: 		for(int j=i+1; j<P.size(); ++j)
4fd800b3a8 2011-02-23        kinaba: 		{
4fd800b3a8 2011-02-23        kinaba: 			int lk = leastReachK( P[j].first-P[i].first, P[j].second-P[i].second, k );
4fd800b3a8 2011-02-23        kinaba: 			if( lk > 0 )
4fd800b3a8 2011-02-23        kinaba: 				md = max(md, 1 + maxDepth(j, lk, P));
4fd800b3a8 2011-02-23        kinaba: 		}
4fd800b3a8 2011-02-23        kinaba: 		return memo[key] = md;
4fd800b3a8 2011-02-23        kinaba: 	}
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 	int leastReachK( int dy, int dx, int k )
4fd800b3a8 2011-02-23        kinaba: 	{
4fd800b3a8 2011-02-23        kinaba: 		if( dy <= 0 || dx <= 0 )
4fd800b3a8 2011-02-23        kinaba: 			return -1;
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 		int minX = ((k+1)+(k+dy))*dy / 2;
4fd800b3a8 2011-02-23        kinaba: 		if( dx < minX )
4fd800b3a8 2011-02-23        kinaba: 			return -1;
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 		int curK = k+dy;
4fd800b3a8 2011-02-23        kinaba: 		int rest = dx - minX;
4fd800b3a8 2011-02-23        kinaba: 		while( rest )
4fd800b3a8 2011-02-23        kinaba: 		{
4fd800b3a8 2011-02-23        kinaba: 			curK += rest / dy;
4fd800b3a8 2011-02-23        kinaba: 			rest %= dy;
4fd800b3a8 2011-02-23        kinaba: 			--dy;
4fd800b3a8 2011-02-23        kinaba: 		}
4fd800b3a8 2011-02-23        kinaba: 		return curK;
4fd800b3a8 2011-02-23        kinaba: 	}
4fd800b3a8 2011-02-23        kinaba: };
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: // BEGIN CUT HERE
4fd800b3a8 2011-02-23        kinaba: #include <ctime>
4fd800b3a8 2011-02-23        kinaba: double start_time; string timer()
4fd800b3a8 2011-02-23        kinaba:  { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
4fd800b3a8 2011-02-23        kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
4fd800b3a8 2011-02-23        kinaba:  { os << "{ ";
4fd800b3a8 2011-02-23        kinaba:    for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
4fd800b3a8 2011-02-23        kinaba:    os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
4fd800b3a8 2011-02-23        kinaba: void verify_case(const int& Expected, const int& Received) {
4fd800b3a8 2011-02-23        kinaba:  bool ok = (Expected == Received);
4fd800b3a8 2011-02-23        kinaba:  if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
4fd800b3a8 2011-02-23        kinaba:  cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
4fd800b3a8 2011-02-23        kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
4fd800b3a8 2011-02-23        kinaba: #define END	 verify_case(_, BaronsAndCoins().getMaximum(x, y));}
4fd800b3a8 2011-02-23        kinaba: int main(){
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: CASE(0)
4fd800b3a8 2011-02-23        kinaba: 	int x_[] = {15, 5, 30};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_));
4fd800b3a8 2011-02-23        kinaba: 	int y_[] = {4, 5, 6};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_));
4fd800b3a8 2011-02-23        kinaba: 	int _ = 2;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(1)
4fd800b3a8 2011-02-23        kinaba: 	int x_[] = {10};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_));
4fd800b3a8 2011-02-23        kinaba: 	int y_[] = {10};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_));
4fd800b3a8 2011-02-23        kinaba: 	int _ = 0;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(2)
4fd800b3a8 2011-02-23        kinaba: 	int x_[] = {1, 3, 6, 10, 15, 21};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_));
4fd800b3a8 2011-02-23        kinaba: 	int y_[] = {1, 2, 3, 4, 5, 6};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_));
4fd800b3a8 2011-02-23        kinaba: 	int _ = 6;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(3)
4fd800b3a8 2011-02-23        kinaba: 	int x_[] = {5, 10, 15, 20, 25, 30, 35, 40, 45};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_));
4fd800b3a8 2011-02-23        kinaba: 	int y_[] = {1, 1, 1, 2, 2, 2, 3, 3, 3};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_));
4fd800b3a8 2011-02-23        kinaba: 	int _ = 3;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(4)
4fd800b3a8 2011-02-23        kinaba: 	int x_[] = {1, 3, 6, 10, 22, 35, 50, 66};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_));
4fd800b3a8 2011-02-23        kinaba: 	int y_[] = {1, 2, 3, 1, 2, 3, 4, 5};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_));
4fd800b3a8 2011-02-23        kinaba: 	int _ = 5;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(5)
4fd800b3a8 2011-02-23        kinaba: 	int x_[] = {10};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_));
4fd800b3a8 2011-02-23        kinaba: 	int y_[] = {4};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_));
4fd800b3a8 2011-02-23        kinaba: 	int _ = 1;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(6)
4fd800b3a8 2011-02-23        kinaba: 	int x_[] = {9};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_));
4fd800b3a8 2011-02-23        kinaba: 	int y_[] = {4};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_));
4fd800b3a8 2011-02-23        kinaba: 	int _ = 0;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(7)
4fd800b3a8 2011-02-23        kinaba: 	int x_[] = {11};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_));
4fd800b3a8 2011-02-23        kinaba: 	int y_[] = {4};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_));
4fd800b3a8 2011-02-23        kinaba: 	int _ = 1;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(8)
4fd800b3a8 2011-02-23        kinaba: 	int x_[] = {1,3,6,10,15,21,28,36,45,55,66,78,91,105,120,136,153,171,190,210,231,253,276,300,325,351,378,406,435,465,496,528,561,595,630,666,703,741,780,820,861,903,946,990,1035,1081,1128,1176,1225,1275};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_));
4fd800b3a8 2011-02-23        kinaba: 	int y_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_));
4fd800b3a8 2011-02-23        kinaba: 	int _ = 50;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(9)
4fd800b3a8 2011-02-23        kinaba: 	int x_[] = {1,30,60,100,150,210,280,360,450,550,660,780,910,1050,1200,1360,1530,1710,1900,2100,2310,2530,2760,3000,3250,3510,3780,4060,4350,4650,4960,5280,5610,5950,6300,6660,7030,7410,7800,8200,8610,9030,9460,9900};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_));
4fd800b3a8 2011-02-23        kinaba: 	int y_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_));
4fd800b3a8 2011-02-23        kinaba: 	int _ = 44;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(10)
4fd800b3a8 2011-02-23        kinaba: 	int x_[] = {999,3,15};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_));
4fd800b3a8 2011-02-23        kinaba: 	int y_[] = {1,2,3};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_));
4fd800b3a8 2011-02-23        kinaba: 	int _ = 2;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(11)
4fd800b3a8 2011-02-23        kinaba: 	int x_[] = {4,12,18};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_));
4fd800b3a8 2011-02-23        kinaba: 	int y_[] = {1,3,4};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_));
4fd800b3a8 2011-02-23        kinaba: 	int _ = 2;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: }
4fd800b3a8 2011-02-23        kinaba: // END CUT HERE