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 HeightRound { public: 4fd800b3a8 2011-02-23 kinaba: typedef pair<pair<int,int>,int> key_t; 4fd800b3a8 2011-02-23 kinaba: map<key_t, int> memo; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int score( int F, int B, int I, const vector<int>& h ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( I == h.size()-1 ) 4fd800b3a8 2011-02-23 kinaba: return max(h[I]-h[F], h[I]-h[B]); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: key_t key(make_pair(F,B), I); 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 s1 = max(h[I]-h[F], score(I, B, I+1, h)); 4fd800b3a8 2011-02-23 kinaba: int s2 = max(h[I]-h[B], score(F, I, I+1, h)); 4fd800b3a8 2011-02-23 kinaba: return memo[key] = min(s1, s2); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: void best( int F, int B, int I, const vector<int>& h, int acc, deque<int>& result ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( I == h.size()-1 ) { 4fd800b3a8 2011-02-23 kinaba: result = deque<int>(1, h[I]); 4fd800b3a8 2011-02-23 kinaba: result.push_front(h[F]); 4fd800b3a8 2011-02-23 kinaba: result.push_back(h[B]); 4fd800b3a8 2011-02-23 kinaba: return; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int s1 = max(acc, max(h[I]-h[F], score(I, B, I+1, h))); 4fd800b3a8 2011-02-23 kinaba: int s2 = max(acc, max(h[I]-h[B], score(F, I, I+1, h))); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( s1<=s2 ) { // if tie, choose lexicographically earlier one 4fd800b3a8 2011-02-23 kinaba: best( I, B, I+1, h, s1, result ); 4fd800b3a8 2011-02-23 kinaba: result.push_front( h[F] ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: else { 4fd800b3a8 2011-02-23 kinaba: best( F, I, I+1, h, s2, result ); 4fd800b3a8 2011-02-23 kinaba: result.push_back( h[B] ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector <int> getBestRound(vector <int> h) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // The answer must be of form: 4fd800b3a8 2011-02-23 kinaba: // h[0], h[1], (...ascending...), h[$-1], (...descending...). 4fd800b3a8 2011-02-23 kinaba: // Thus, I begin from 4fd800b3a8 2011-02-23 kinaba: // F=1, B=0: [h[1], .........., h[0]] 4fd800b3a8 2011-02-23 kinaba: // and fill each h[I=2,3,...] either to the left or right of the blank space, 4fd800b3a8 2011-02-23 kinaba: // and choose the best one. To choose lex-first candidate, I use the "jo-seki" 4fd800b3a8 2011-02-23 kinaba: // technique, which first computes only scores and then choose the instance 4fd800b3a8 2011-02-23 kinaba: // lexicographically afterwards. 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: sort(h.begin(), h.end()); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: deque<int> result; 4fd800b3a8 2011-02-23 kinaba: best( 1, 0, 2, h, 0, result ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: rotate(result.begin(), result.end()-1, result.end()); 4fd800b3a8 2011-02-23 kinaba: return vector<int>(result.begin(), result.end()); 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 vector <int>& Expected, const vector <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(_, HeightRound().getBestRound(heights));} 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 heights_[] = {1,2,3,4}; 4fd800b3a8 2011-02-23 kinaba: vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 4fd800b3a8 2011-02-23 kinaba: int __[] = {1, 2, 4, 3 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: int heights_[] = {1000,500,1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 4fd800b3a8 2011-02-23 kinaba: int __[] = {1, 500, 1000 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: int heights_[] = {1,3,4,5,7}; 4fd800b3a8 2011-02-23 kinaba: vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 4fd800b3a8 2011-02-23 kinaba: int __[] = {1, 3, 5, 7, 4 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: 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, 4fd800b3a8 2011-02-23 kinaba: 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}; 4fd800b3a8 2011-02-23 kinaba: vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 4fd800b3a8 2011-02-23 kinaba: 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 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: int heights_[] = {1,2,1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 4fd800b3a8 2011-02-23 kinaba: int __[] = {1,1,2}; 4fd800b3a8 2011-02-23 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: 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}; 4fd800b3a8 2011-02-23 kinaba: vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 4fd800b3a8 2011-02-23 kinaba: 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}; 4fd800b3a8 2011-02-23 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(6) 4fd800b3a8 2011-02-23 kinaba: int heights_[] = {7, 43, 3, 6, 32, 2, 42, 23, 1, 30}; 4fd800b3a8 2011-02-23 kinaba: vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 4fd800b3a8 2011-02-23 kinaba: int __[] = {1, 2, 3, 6, 7, 30, 32, 42, 43, 23}; 4fd800b3a8 2011-02-23 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE