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 SaveTheTrees { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: int minimumCut(vector <int> x, vector <int> y, vector <int> h) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int N = x.size(); 4fd800b3a8 2011-02-23 kinaba: int ans = N-1; 4fd800b3a8 2011-02-23 kinaba: for(int i1=0; i1<N; ++i1) 4fd800b3a8 2011-02-23 kinaba: for(int i2=0; i2<N; ++i2) 4fd800b3a8 2011-02-23 kinaba: for(int k1=0; k1<N; ++k1) 4fd800b3a8 2011-02-23 kinaba: for(int k2=0; k2<N; ++k2) { 4fd800b3a8 2011-02-23 kinaba: int mx = min(x[i1], x[i2]); 4fd800b3a8 2011-02-23 kinaba: int Mx = max(x[i1], x[i2]); 4fd800b3a8 2011-02-23 kinaba: int my = min(y[k1], y[k2]); 4fd800b3a8 2011-02-23 kinaba: int My = max(y[k1], y[k2]); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int hh[40], q=0; 4fd800b3a8 2011-02-23 kinaba: int cut = 0, need = ((Mx-mx) + (My-my))*2; 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<N; ++j) 4fd800b3a8 2011-02-23 kinaba: if( mx<=x[j] && x[j]<=Mx && my<=y[j] && y[j]<=My ) 4fd800b3a8 2011-02-23 kinaba: hh[q++] = h[j]; 4fd800b3a8 2011-02-23 kinaba: else { 4fd800b3a8 2011-02-23 kinaba: cut++, need-=h[j]; 4fd800b3a8 2011-02-23 kinaba: if(cut>=ans) goto next; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: if( need > 0 ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: sort( &hh[0], &hh[q], greater<int>() ); 4fd800b3a8 2011-02-23 kinaba: // partial_sum( &hh[0], &hh[q], &hh[0] ); 4fd800b3a8 2011-02-23 kinaba: // cut += lower_bound( &hh[0], &hh[q], need ) - &hh[0]; 4fd800b3a8 2011-02-23 kinaba: for(int p=0; p<q; ++p) { 4fd800b3a8 2011-02-23 kinaba: ++cut, need -= hh[p]; 4fd800b3a8 2011-02-23 kinaba: if(cut>=ans) goto next; 4fd800b3a8 2011-02-23 kinaba: if(need <= 0) break; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: ans = cut; 4fd800b3a8 2011-02-23 kinaba: next:; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return ans; 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() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: 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(); } 4fd800b3a8 2011-02-23 kinaba: 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;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} }; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: int x_[] = {1, 2, 8, 9, 5}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 4fd800b3a8 2011-02-23 kinaba: int y_[] = {1, 8, 2, 9, 5}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 4fd800b3a8 2011-02-23 kinaba: int h_[] = {1, 1, 1, 1, 32}; 4fd800b3a8 2011-02-23 kinaba: vector <int> h(h_, h_+sizeof(h_)/sizeof(*h_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 1; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, SaveTheTrees().minimumCut(x, y, h)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: int x_[] = {1, 2, 8, 9, 5}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 4fd800b3a8 2011-02-23 kinaba: int y_[] = {1, 8, 2, 9, 5}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 4fd800b3a8 2011-02-23 kinaba: int h_[] = {1, 1, 1, 1, 27}; 4fd800b3a8 2011-02-23 kinaba: vector <int> h(h_, h_+sizeof(h_)/sizeof(*h_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 2; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, SaveTheTrees().minimumCut(x, y, h)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: int x_[] = {1, 2, 8, 9, 5}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 4fd800b3a8 2011-02-23 kinaba: int y_[] = {1, 8, 2, 9, 5}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 4fd800b3a8 2011-02-23 kinaba: int h_[] = {4, 4, 4, 4, 4}; 4fd800b3a8 2011-02-23 kinaba: vector <int> h(h_, h_+sizeof(h_)/sizeof(*h_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 3; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, SaveTheTrees().minimumCut(x, y, h)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: int x_[] = {1, 2, 8, 9, 5}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 4fd800b3a8 2011-02-23 kinaba: int y_[] = {1, 8, 2, 9, 5}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 4fd800b3a8 2011-02-23 kinaba: int h_[] = {3, 3, 3, 3, 3}; 4fd800b3a8 2011-02-23 kinaba: vector <int> h(h_, h_+sizeof(h_)/sizeof(*h_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 4; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, SaveTheTrees().minimumCut(x, y, h)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: int x_[] = {3, 10, 6, 2, 8, 7, 5, 4, 9, 1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 4fd800b3a8 2011-02-23 kinaba: int y_[] = {4, 10, 6, 9, 8, 7, 3, 2, 5, 1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 4fd800b3a8 2011-02-23 kinaba: int h_[] = {4, 2, 2, 4, 4, 5, 4, 4, 2, 3}; 4fd800b3a8 2011-02-23 kinaba: vector <int> h(h_, h_+sizeof(h_)/sizeof(*h_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 5; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, SaveTheTrees().minimumCut(x, y, h)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<5>) { 4fd800b3a8 2011-02-23 kinaba: int x_[] = {55, 67, 100, 38, 80, 98, 47, 58, 61, 33}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 4fd800b3a8 2011-02-23 kinaba: int y_[] = {87, 17, 20, 7, 57, 19, 23, 68, 27, 39}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 4fd800b3a8 2011-02-23 kinaba: int h_[] = {6, 2, 8, 8, 31, 25, 23, 19, 45, 4}; 4fd800b3a8 2011-02-23 kinaba: vector <int> h(h_, h_+sizeof(h_)/sizeof(*h_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 6; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, SaveTheTrees().minimumCut(x, y, h)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<6>) { 4fd800b3a8 2011-02-23 kinaba: 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}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 4fd800b3a8 2011-02-23 kinaba: 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}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 4fd800b3a8 2011-02-23 kinaba: 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}; 4fd800b3a8 2011-02-23 kinaba: vector <int> h(h_, h_+sizeof(h_)/sizeof(*h_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 6; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, SaveTheTrees().minimumCut(x, y, h)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: