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 FastPostman { public:
4fd800b3a8 2011-02-23        kinaba: 	struct vert
4fd800b3a8 2011-02-23        kinaba: 	{
4fd800b3a8 2011-02-23        kinaba: 		int Ldone, Rdone;
4fd800b3a8 2011-02-23        kinaba: 		bool nowAtL;
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 		bool operator<( const vert& rhs ) const {
4fd800b3a8 2011-02-23        kinaba: 			if( Ldone != rhs.Ldone ) return Ldone < rhs.Ldone;
4fd800b3a8 2011-02-23        kinaba: 			if( Rdone != rhs.Rdone ) return Rdone < rhs.Rdone;
4fd800b3a8 2011-02-23        kinaba: 			return nowAtL < rhs.nowAtL;
4fd800b3a8 2011-02-23        kinaba: 		}
4fd800b3a8 2011-02-23        kinaba: 		vert(int l, int r, bool b) : Ldone(l), Rdone(r), nowAtL(b) {}
4fd800b3a8 2011-02-23        kinaba: 	};
4fd800b3a8 2011-02-23        kinaba: 	typedef vert edge;
4fd800b3a8 2011-02-23        kinaba: 	typedef pair<int,edge> cedge;
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 	int minTime(vector <int> address, vector <int> maxTime, int initialPos)
4fd800b3a8 2011-02-23        kinaba: 	{
4fd800b3a8 2011-02-23        kinaba: 		const int N = address.size();
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 		vector< pair<int,int> > L, R;
4fd800b3a8 2011-02-23        kinaba: 		R.push_back( make_pair(initialPos,0) );
4fd800b3a8 2011-02-23        kinaba: 		for(int i=0; i<N; ++i)
4fd800b3a8 2011-02-23        kinaba: 			if( initialPos <= address[i] )
4fd800b3a8 2011-02-23        kinaba: 				R.push_back( make_pair(address[i], maxTime[i]) );
4fd800b3a8 2011-02-23        kinaba: 			else
4fd800b3a8 2011-02-23        kinaba: 				L.push_back( make_pair(address[i], maxTime[i]) );
4fd800b3a8 2011-02-23        kinaba: 		stable_sort( R.begin(), R.end() );
4fd800b3a8 2011-02-23        kinaba: 		stable_sort( L.rbegin(), L.rend() );
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 		vert S(0, 1, false);
4fd800b3a8 2011-02-23        kinaba: 		priority_queue< cedge, vector<cedge>, greater<cedge> > Q;
4fd800b3a8 2011-02-23        kinaba: 		set<vert> V;
4fd800b3a8 2011-02-23        kinaba: 		Q.push( cedge(0,S) );
4fd800b3a8 2011-02-23        kinaba: 		while( !Q.empty() )
4fd800b3a8 2011-02-23        kinaba: 		{
4fd800b3a8 2011-02-23        kinaba: 			int  c = Q.top().first;
4fd800b3a8 2011-02-23        kinaba: 			vert v = Q.top().second;
4fd800b3a8 2011-02-23        kinaba: 			Q.pop();
4fd800b3a8 2011-02-23        kinaba: 			if( V.count(v) )
4fd800b3a8 2011-02-23        kinaba: 				continue;
4fd800b3a8 2011-02-23        kinaba: 			if( v.Ldone == L.size() && v.Rdone == R.size() )
4fd800b3a8 2011-02-23        kinaba: 				return c;
4fd800b3a8 2011-02-23        kinaba: 			V.insert(v);
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 			if( v.Ldone < L.size() ) {
4fd800b3a8 2011-02-23        kinaba: 				vert u(v.Ldone+1, v.Rdone, true);
4fd800b3a8 2011-02-23        kinaba: 				int cu = c + abs( (v.nowAtL ? L[v.Ldone-1] : R[v.Rdone-1]).first - L[v.Ldone].first );
4fd800b3a8 2011-02-23        kinaba: 				if( !V.count(u) && cu <= L[v.Ldone].second )
4fd800b3a8 2011-02-23        kinaba: 					Q.push( cedge(cu,u) );
4fd800b3a8 2011-02-23        kinaba: 			}
4fd800b3a8 2011-02-23        kinaba: 			if( v.Rdone < R.size() ) {
4fd800b3a8 2011-02-23        kinaba: 				vert u(v.Ldone, v.Rdone+1, false);
4fd800b3a8 2011-02-23        kinaba: 				int cu = c + abs( (v.nowAtL ? L[v.Ldone-1] : R[v.Rdone-1]).first - R[v.Rdone].first );
4fd800b3a8 2011-02-23        kinaba: 				if( !V.count(u) && cu <= R[v.Rdone].second )
4fd800b3a8 2011-02-23        kinaba: 					Q.push( cedge(cu,u) );
4fd800b3a8 2011-02-23        kinaba: 			}
4fd800b3a8 2011-02-23        kinaba: 		}
4fd800b3a8 2011-02-23        kinaba: 		return -1;
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(_, FastPostman().minTime(address, maxTime, initialPos));}
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 address_[] = {1,3,5,7};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> address(address_, address_+sizeof(address_)/sizeof(*address_));
4fd800b3a8 2011-02-23        kinaba: 	int maxTime_[] = {9,2,5,100};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTime_));
4fd800b3a8 2011-02-23        kinaba: 	int initialPos = 4;
4fd800b3a8 2011-02-23        kinaba: 	int _ = 13;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(1)
4fd800b3a8 2011-02-23        kinaba: 	int address_[] = {1,5};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> address(address_, address_+sizeof(address_)/sizeof(*address_));
4fd800b3a8 2011-02-23        kinaba: 	int maxTime_[] = {6,2};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTime_));
4fd800b3a8 2011-02-23        kinaba: 	int initialPos = 3;
4fd800b3a8 2011-02-23        kinaba: 	int _ = 6;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(2)
4fd800b3a8 2011-02-23        kinaba: 	int address_[] = {1000000};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> address(address_, address_+sizeof(address_)/sizeof(*address_));
4fd800b3a8 2011-02-23        kinaba: 	int maxTime_[] = {45634};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTime_));
4fd800b3a8 2011-02-23        kinaba: 	int initialPos = 876;
4fd800b3a8 2011-02-23        kinaba: 	int _ = -1;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(3)
4fd800b3a8 2011-02-23        kinaba: 	int address_[] = {1,7,10,4};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> address(address_, address_+sizeof(address_)/sizeof(*address_));
4fd800b3a8 2011-02-23        kinaba: 	int maxTime_[] = {15,6,28,39};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTime_));
4fd800b3a8 2011-02-23        kinaba: 	int initialPos = 2;
4fd800b3a8 2011-02-23        kinaba: 	int _ = 20;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(4)
4fd800b3a8 2011-02-23        kinaba: 	int address_[] = {1000000,1000000,1000000,1000000};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> address(address_, address_+sizeof(address_)/sizeof(*address_));
4fd800b3a8 2011-02-23        kinaba: 	int maxTime_[] = {563,23452,32426,1};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTime_));
4fd800b3a8 2011-02-23        kinaba: 	int initialPos = 1000000;
4fd800b3a8 2011-02-23        kinaba: 	int _ = 0;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(5)
4fd800b3a8 2011-02-23        kinaba: 	int address_[] = {1};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> address(address_, address_+sizeof(address_)/sizeof(*address_));
4fd800b3a8 2011-02-23        kinaba: 	int maxTime_[] = {2};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTime_));
4fd800b3a8 2011-02-23        kinaba: 	int initialPos = {3};
4fd800b3a8 2011-02-23        kinaba: 	int _ = 2;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(6)
4fd800b3a8 2011-02-23        kinaba: 	int address_[] = {0, 1000000};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> address(address_, address_+sizeof(address_)/sizeof(*address_));
4fd800b3a8 2011-02-23        kinaba: 	int maxTime_[] = {500000, 1500000};
4fd800b3a8 2011-02-23        kinaba: 	  vector <int> maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTime_));
4fd800b3a8 2011-02-23        kinaba: 	int initialPos = 500000;
4fd800b3a8 2011-02-23        kinaba: 	int _ = 1500000;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: }
4fd800b3a8 2011-02-23        kinaba: // END CUT HERE