Artifact Content
Not logged in

Artifact 4bd4984796b0669c4d3d14b88a710e8ce4129ed0


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

class KingdomAndTrees { public:
	int minLevel(vector <int> heights)
	{
		// (L,R]
		int L = -1;
		int R = 1000000000;
		while(R-L>1)
		{
			int C = (L+R)/2;
			(possible(C,heights) ? R : L) = C;
		}
		return R;
	}

	bool possible(int C, const vector<int>& h)
	{
		int last = max(1, h[0]-C);
		for(int i=1; i<h.size(); ++i)
		{
			if( !(last < h[i]+C) )
				return false;
			last = max(last+1, h[i]-C);
		}
		return true;
	}
};

// 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 int& Expected, const 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(_, KingdomAndTrees().minLevel(heights));}
int main(){

CASE(0)
	int heights_[] = {9, 5, 11};
	  vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 
	int _ = 3; 
END
CASE(1)
	int heights_[] = {5, 8};
	  vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 
	int _ = 0; 
END
CASE(2)
	int heights_[] = {1, 1, 1, 1, 1};
	  vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 
	int _ = 4; 
END
CASE(3)
	int heights_[] = {548, 47, 58, 250, 2012};
	  vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 
	int _ = 251; 
END
CASE(4)
	int heights_[] = {337994619,837714986,7014426,516695490,268514071,590654553,940024470,79308071,891444369,231310251,172567010,283491054,951215936,92716118,911004789,896372476,617116292,874491895,120052194,635772188,938392572,466867891,418351197,278475278,635224368,38779964,762367612,370214557,20108108,314281202,644947239,868648377,617056931,542328796,280916141,281585869,895175595,529854516,862330692,733665485,173060292,579136324,401396401,303236137,161627936,410922706,172892990,840336279,848958999,849348801};
	  vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 
	int _ = -1; 
END
CASE(5)
	int heights_[] = {1000000000,1};
	  vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 
	int _ = 500000000; 
END

}
// END CUT HERE