Artifact Content
Not logged in

Artifact b041c671ce46ca29e057c394049d1a3900c4c2d2


#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>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class HeightRound { public:
	typedef pair<pair<int,int>,int> key_t;
	map<key_t, int> memo;

	int score( int F, int B, int I, const vector<int>& h )
	{
		if( I == h.size()-1 )
			return max(h[I]-h[F], h[I]-h[B]);

		key_t key(make_pair(F,B), I);
		if( memo.count(key) )
			return memo[key];

		int s1 = max(h[I]-h[F], score(I, B, I+1, h));
		int s2 = max(h[I]-h[B], score(F, I, I+1, h));
		return memo[key] = min(s1, s2);
	}

	void best( int F, int B, int I, const vector<int>& h, int acc, deque<int>& result )
	{
		if( I == h.size()-1 ) {
			result = deque<int>(1, h[I]);
			result.push_front(h[F]);
			result.push_back(h[B]);
			return;
		}

		int s1 = max(acc, max(h[I]-h[F], score(I, B, I+1, h)));
		int s2 = max(acc, max(h[I]-h[B], score(F, I, I+1, h)));

		if( s1<=s2 ) { // if tie, choose lexicographically earlier one
			best( I, B, I+1, h, s1, result );
			result.push_front( h[F] );
		}
		else {
			best( F, I, I+1, h, s2, result );
			result.push_back( h[B] );
		}
	}

	vector <int> getBestRound(vector <int> h) 
	{
		// The answer must be of form:
		//    h[0], h[1], (...ascending...), h[$-1], (...descending...).
		// Thus, I begin from
		//    F=1, B=0: [h[1], ..........,  h[0]]
		// and fill each h[I=2,3,...] either to the left or right of the blank space,
		// and choose the best one. To choose lex-first candidate, I use the "jo-seki"
		// technique, which first computes only scores and then choose the instance
		// lexicographically afterwards.

		sort(h.begin(), h.end());

		deque<int> result;
		best( 1, 0, 2, h, 0, result );

		rotate(result.begin(), result.end()-1, result.end());
		return vector<int>(result.begin(), result.end());
	}
};

// 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 vector <int>& Expected, const vector <int>& Received) {
 bool ok = (Expected == Received);
 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(_, HeightRound().getBestRound(heights));}
int main(){

CASE(0)
	int heights_[] = {1,2,3,4};
	  vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 
	int __[] = {1, 2, 4, 3 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(1)
	int heights_[] = {1000,500,1};
	  vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 
	int __[] = {1, 500, 1000 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(2)
	int heights_[] = {1,3,4,5,7};
	  vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 
	int __[] = {1, 3, 5, 7, 4 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(3)
	int heights_[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
	  vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 
	int __[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(4)
	int heights_[] = {1,2,1};
	  vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 
	int __[] = {1,1,2};
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(5)
	int heights_[] = {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,1000};
	  vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 
	int __[] = {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,1000,49};
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(6)
	int heights_[] = {7, 43, 3, 6, 32, 2, 42, 23, 1, 30};
	  vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 
	int __[] = {1, 2, 3, 6, 7, 30, 32, 42, 43, 23};
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END

}
// END CUT HERE