Artifact Content
Not logged in

Artifact 7f6a0aa40df322bf38db67de0b3f3f7c6f23d5c0


#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 SaveTheTrees {
public:
	int minimumCut(vector <int> x, vector <int> y, vector <int> h) 
	{
		int N = x.size();
		int ans = N-1;
		for(int i1=0; i1<N; ++i1)
		for(int i2=0; i2<N; ++i2)
		for(int k1=0; k1<N; ++k1)
		for(int k2=0; k2<N; ++k2) {
			int mx = min(x[i1], x[i2]);
			int Mx = max(x[i1], x[i2]);
			int my = min(y[k1], y[k2]);
			int My = max(y[k1], y[k2]);

			int hh[40], q=0;
			int cut = 0, need = ((Mx-mx) + (My-my))*2;
			for(int j=0; j<N; ++j)
				if( mx<=x[j] && x[j]<=Mx && my<=y[j] && y[j]<=My )
					hh[q++] = h[j];
				else {
					cut++, need-=h[j];
					if(cut>=ans) goto next;
				}
			if( need > 0 )
			{
				sort( &hh[0], &hh[q], greater<int>() );
//				partial_sum( &hh[0], &hh[q], &hh[0] );
//				cut += lower_bound( &hh[0], &hh[q], need ) - &hh[0];
				for(int p=0; p<q; ++p) {
					++cut, need -= hh[p];
					if(cut>=ans) goto next;
					if(need <= 0) break;
				}
			}
			ans = cut;
		next:;
		}
		return ans;
	}
};

// 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> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	int x_[] = {1, 2, 8, 9, 5};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {1, 8, 2, 9, 5};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int h_[] = {1, 1, 1, 1, 32};
	  vector <int> h(h_, h_+sizeof(h_)/sizeof(*h_)); 
	int RetVal = 1; 
	return verify_case(RetVal, SaveTheTrees().minimumCut(x, y, h)); }
int Test_(Case_<1>) {
	int x_[] = {1, 2, 8, 9, 5};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {1, 8, 2, 9, 5};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int h_[] = {1, 1, 1, 1, 27};
	  vector <int> h(h_, h_+sizeof(h_)/sizeof(*h_)); 
	int RetVal = 2; 
	return verify_case(RetVal, SaveTheTrees().minimumCut(x, y, h)); }
int Test_(Case_<2>) {
	int x_[] = {1, 2, 8, 9, 5};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {1, 8, 2, 9, 5};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int h_[] = {4, 4, 4, 4, 4};
	  vector <int> h(h_, h_+sizeof(h_)/sizeof(*h_)); 
	int RetVal = 3; 
	return verify_case(RetVal, SaveTheTrees().minimumCut(x, y, h)); }
int Test_(Case_<3>) {
	int x_[] = {1, 2, 8, 9, 5};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {1, 8, 2, 9, 5};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int h_[] = {3, 3, 3, 3, 3};
	  vector <int> h(h_, h_+sizeof(h_)/sizeof(*h_)); 
	int RetVal = 4; 
	return verify_case(RetVal, SaveTheTrees().minimumCut(x, y, h)); }
int Test_(Case_<4>) {
	int x_[] = {3, 10, 6, 2, 8, 7, 5, 4, 9, 1};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {4, 10, 6, 9, 8, 7, 3, 2, 5, 1};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int h_[] = {4, 2, 2, 4, 4, 5, 4, 4, 2, 3};
	  vector <int> h(h_, h_+sizeof(h_)/sizeof(*h_)); 
	int RetVal = 5; 
	return verify_case(RetVal, SaveTheTrees().minimumCut(x, y, h)); }
int Test_(Case_<5>) {
	int x_[] = {55, 67, 100, 38, 80, 98, 47, 58, 61, 33};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {87, 17, 20, 7, 57, 19, 23, 68, 27, 39};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int h_[] = {6, 2, 8, 8, 31, 25, 23, 19, 45, 4};
	  vector <int> h(h_, h_+sizeof(h_)/sizeof(*h_)); 
	int RetVal = 6; 
	return verify_case(RetVal, SaveTheTrees().minimumCut(x, y, h)); }
int Test_(Case_<6>) {
	int x_[] = {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};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {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};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int h_[] = {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> h(h_, h_+sizeof(h_)/sizeof(*h_)); 
	int RetVal = 6; 
	return verify_case(RetVal, SaveTheTrees().minimumCut(x, y, h)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE