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: template<typename T>
4fd800b3a8 2011-02-23        kinaba: struct DP3
4fd800b3a8 2011-02-23        kinaba: {
4fd800b3a8 2011-02-23        kinaba: 	int N1, N2, N3;
4fd800b3a8 2011-02-23        kinaba: 	vector<T> data;
4fd800b3a8 2011-02-23        kinaba: 	DP3(int N1, int N2, int N3, const T& t = T())
4fd800b3a8 2011-02-23        kinaba: 		: N1(N1), N2(N2), N3(N3), data(N1*N2*N3, t) { assert(data.size()*sizeof(T)<(1<<26)); }
4fd800b3a8 2011-02-23        kinaba: 	T& operator()(int i1, int i2, int i3)
4fd800b3a8 2011-02-23        kinaba: 		{ return data[ ((i1*N2)+i2)*N3+i3 ]; }
4fd800b3a8 2011-02-23        kinaba: 	void swap(DP3& rhs)
4fd800b3a8 2011-02-23        kinaba: 		{ data.swap(rhs.data); }
4fd800b3a8 2011-02-23        kinaba: };
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: int bitcnt(int U)
4fd800b3a8 2011-02-23        kinaba: {
4fd800b3a8 2011-02-23        kinaba: 	int c = 0;
4fd800b3a8 2011-02-23        kinaba: 	for(; U; U>>=1)
4fd800b3a8 2011-02-23        kinaba: 		c += (U&1);
4fd800b3a8 2011-02-23        kinaba: 	return c;
4fd800b3a8 2011-02-23        kinaba: }
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: class TicketPrinters { public:
4fd800b3a8 2011-02-23        kinaba: 	int minTime(int currentPrinter, vector <int> printerDistance, vector <int> startingValues, vector <int> wantedValues)
4fd800b3a8 2011-02-23        kinaba: 	{
4fd800b3a8 2011-02-23        kinaba: 		int N = startingValues.size();
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 		vector<int> d(N);
4fd800b3a8 2011-02-23        kinaba: 		partial_sum( printerDistance.begin(), printerDistance.end(), d.begin()+1 );
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 		// left, used, LeftOrRight
4fd800b3a8 2011-02-23        kinaba: 		DP3<int> dp(N,1<<N,2);
4fd800b3a8 2011-02-23        kinaba: 		for(int D=(1<<N)-1; D>=0; --D)
4fd800b3a8 2011-02-23        kinaba: 		{
4fd800b3a8 2011-02-23        kinaba: 			int bc = bitcnt(D);
4fd800b3a8 2011-02-23        kinaba: 			for(int L=0; L<N && L+bc<=N; ++L)
4fd800b3a8 2011-02-23        kinaba: 				for(int C=0; C<=1; ++C)
4fd800b3a8 2011-02-23        kinaba: 				{
4fd800b3a8 2011-02-23        kinaba: 					if( bc == N )
4fd800b3a8 2011-02-23        kinaba: 						dp(L,D,C) = 0;
4fd800b3a8 2011-02-23        kinaba: 					else {
4fd800b3a8 2011-02-23        kinaba: 						int cur = (C==0 ? L : L+bc-1);
4fd800b3a8 2011-02-23        kinaba: 						int mn = 0x7fffffff;
4fd800b3a8 2011-02-23        kinaba: 						for(int nextW=0; nextW<N; ++nextW)
4fd800b3a8 2011-02-23        kinaba: 							if( ((1<<nextW) & D) == 0 ) {
4fd800b3a8 2011-02-23        kinaba: 								int caseLeft = 0x7fffffff;
4fd800b3a8 2011-02-23        kinaba: 								if( L>0 ) {
4fd800b3a8 2011-02-23        kinaba: 									caseLeft = d[cur] - d[L-1] + max(dp(L-1, D|1<<nextW, 0),
4fd800b3a8 2011-02-23        kinaba: 										1+abs(startingValues[L-1]-wantedValues[nextW]));
4fd800b3a8 2011-02-23        kinaba: 								}
4fd800b3a8 2011-02-23        kinaba: 								int caseRight = 0x7fffffff;
4fd800b3a8 2011-02-23        kinaba: 								if( L+bc < N ) {
4fd800b3a8 2011-02-23        kinaba: 									caseRight = d[L+bc] - d[cur] + max(dp(L, D|1<<nextW, 1),
4fd800b3a8 2011-02-23        kinaba: 										1+abs(startingValues[L+bc]-wantedValues[nextW]));
4fd800b3a8 2011-02-23        kinaba: 								}
4fd800b3a8 2011-02-23        kinaba: 								mn = min(mn, min(caseLeft, caseRight));
4fd800b3a8 2011-02-23        kinaba: 							}
4fd800b3a8 2011-02-23        kinaba: 						dp(L,D,C) = mn;
4fd800b3a8 2011-02-23        kinaba: 					}
4fd800b3a8 2011-02-23        kinaba: 				}
4fd800b3a8 2011-02-23        kinaba: 		}
4fd800b3a8 2011-02-23        kinaba: 		return dp(currentPrinter,0,0);
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(_, TicketPrinters().minTime(currentPrinter, printerDistance, startingValues, wantedValues));}
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 currentPrinter = 0;
4fd800b3a8 2011-02-23        kinaba: 	int printerDistance_[] = {100, 20, 50};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> printerDistance(printerDistance_, printerDistance_+sizeof(printerDistance_)/sizeof(*printerDistance_));
4fd800b3a8 2011-02-23        kinaba: 	int startingValues_[] = {66, 78, 99, 5};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> startingValues(startingValues_, startingValues_+sizeof(startingValues_)/sizeof(*startingValues_));
4fd800b3a8 2011-02-23        kinaba: 	int wantedValues_[] = {99, 5, 78, 66};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> wantedValues(wantedValues_, wantedValues_+sizeof(wantedValues_)/sizeof(*wantedValues_));
4fd800b3a8 2011-02-23        kinaba: 	int _ = 171;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(1)
4fd800b3a8 2011-02-23        kinaba: 	int currentPrinter = 1;
4fd800b3a8 2011-02-23        kinaba: 	int printerDistance_[] = {50, 50};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> printerDistance(printerDistance_, printerDistance_+sizeof(printerDistance_)/sizeof(*printerDistance_));
4fd800b3a8 2011-02-23        kinaba: 	int startingValues_[] = {100, 200, 300};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> startingValues(startingValues_, startingValues_+sizeof(startingValues_)/sizeof(*startingValues_));
4fd800b3a8 2011-02-23        kinaba: 	int wantedValues_[] = {101, 201, 302};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> wantedValues(wantedValues_, wantedValues_+sizeof(wantedValues_)/sizeof(*wantedValues_));
4fd800b3a8 2011-02-23        kinaba: 	int _ = 152;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(2)
4fd800b3a8 2011-02-23        kinaba: 	int currentPrinter = 2;
4fd800b3a8 2011-02-23        kinaba: 	int printerDistance_[] = {13, 26, 39, 13};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> printerDistance(printerDistance_, printerDistance_+sizeof(printerDistance_)/sizeof(*printerDistance_));
4fd800b3a8 2011-02-23        kinaba: 	int startingValues_[] = {123, 12, 32, 67, 1015};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> startingValues(startingValues_, startingValues_+sizeof(startingValues_)/sizeof(*startingValues_));
4fd800b3a8 2011-02-23        kinaba: 	int wantedValues_[] = {1, 2, 3, 4, 5};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> wantedValues(wantedValues_, wantedValues_+sizeof(wantedValues_)/sizeof(*wantedValues_));
4fd800b3a8 2011-02-23        kinaba: 	int _ = 1063;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: 	/*
4fd800b3a8 2011-02-23        kinaba: CASE(3)
4fd800b3a8 2011-02-23        kinaba: 	int currentPrinter = ;
4fd800b3a8 2011-02-23        kinaba: 	int printerDistance_[] = ;
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> printerDistance(printerDistance_, printerDistance_+sizeof(printerDistance_)/sizeof(*printerDistance_));
4fd800b3a8 2011-02-23        kinaba: 	int startingValues_[] = ;
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> startingValues(startingValues_, startingValues_+sizeof(startingValues_)/sizeof(*startingValues_));
4fd800b3a8 2011-02-23        kinaba: 	int wantedValues_[] = ;
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> wantedValues(wantedValues_, wantedValues_+sizeof(wantedValues_)/sizeof(*wantedValues_));
4fd800b3a8 2011-02-23        kinaba: 	int _ = ;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(4)
4fd800b3a8 2011-02-23        kinaba: 	int currentPrinter = ;
4fd800b3a8 2011-02-23        kinaba: 	int printerDistance_[] = ;
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> printerDistance(printerDistance_, printerDistance_+sizeof(printerDistance_)/sizeof(*printerDistance_));
4fd800b3a8 2011-02-23        kinaba: 	int startingValues_[] = ;
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> startingValues(startingValues_, startingValues_+sizeof(startingValues_)/sizeof(*startingValues_));
4fd800b3a8 2011-02-23        kinaba: 	int wantedValues_[] = ;
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> wantedValues(wantedValues_, wantedValues_+sizeof(wantedValues_)/sizeof(*wantedValues_));
4fd800b3a8 2011-02-23        kinaba: 	int _ = ;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: */
4fd800b3a8 2011-02-23        kinaba: }
4fd800b3a8 2011-02-23        kinaba: // END CUT HERE