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 <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: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int INF = 10000000; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class TwinTowns 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: vector <int> optimalTwinTowns(vector <int> x, vector <int> y, int maxPartners, int minDistance) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int N = x.size(); 4fd800b3a8 2011-02-23 kinaba: int cost[10][10]; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<N; ++i) 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<N; ++j) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int d = abs(x[i]-x[j]) + abs(y[i]-y[j]); 4fd800b3a8 2011-02-23 kinaba: cost[i][j] = (d < minDistance ? INF : d); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<int> v(N, maxPartners); 4fd800b3a8 2011-02-23 kinaba: pair<int,int> r = rec(0, N, v, cost); 4fd800b3a8 2011-02-23 kinaba: vector<int> ans(2); 4fd800b3a8 2011-02-23 kinaba: ans[0] = -r.first; 4fd800b3a8 2011-02-23 kinaba: ans[1] = r.second; 4fd800b3a8 2011-02-23 kinaba: return ans; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: map< vector<int>,pair<int,int> > memo; 4fd800b3a8 2011-02-23 kinaba: pair<int,int> rec(int i, int N, vector<int>& v, int cost[10][10]) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( i==N ) 4fd800b3a8 2011-02-23 kinaba: return make_pair(0, 0); 4fd800b3a8 2011-02-23 kinaba: vector<int> key(v.begin()+i, v.end()); 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: // choose at most v[i] js s.t. cost[i][j]!=INF && v[j]!=0 4fd800b3a8 2011-02-23 kinaba: pair<int,int> best = rec(i+1, N, v, cost); 4fd800b3a8 2011-02-23 kinaba: if( v[i] >= 1 ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: for(int j1= i+1; j1<N; ++j1) if(cost[i][j1]!=INF && v[j1]) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: v[j1]--; 4fd800b3a8 2011-02-23 kinaba: pair<int,int> a = rec(i+1, N, v, cost); 4fd800b3a8 2011-02-23 kinaba: a.first -= 1; 4fd800b3a8 2011-02-23 kinaba: a.second += cost[i][j1]; 4fd800b3a8 2011-02-23 kinaba: best = min(best, a); 4fd800b3a8 2011-02-23 kinaba: v[j1]++; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: if( v[i] >= 2 ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: for(int j1= i+1; j1<N; ++j1) if(cost[i][j1]!=INF && v[j1]) 4fd800b3a8 2011-02-23 kinaba: for(int j2=j1+1; j2<N; ++j2) if(cost[i][j2]!=INF && v[j2]) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: v[j1]--; 4fd800b3a8 2011-02-23 kinaba: v[j2]--; 4fd800b3a8 2011-02-23 kinaba: pair<int,int> a = rec(i+1, N, v, cost); 4fd800b3a8 2011-02-23 kinaba: a.first -= 2; 4fd800b3a8 2011-02-23 kinaba: a.second += cost[i][j1] + cost[i][j2]; 4fd800b3a8 2011-02-23 kinaba: best = min(best, a); 4fd800b3a8 2011-02-23 kinaba: v[j1]++; 4fd800b3a8 2011-02-23 kinaba: v[j2]++; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: if( v[i] >= 3 ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: for(int j1= i+1; j1<N; ++j1) if(cost[i][j1]!=INF && v[j1]) 4fd800b3a8 2011-02-23 kinaba: for(int j2=j1+1; j2<N; ++j2) if(cost[i][j2]!=INF && v[j2]) 4fd800b3a8 2011-02-23 kinaba: for(int j3=j2+1; j3<N; ++j3) if(cost[i][j3]!=INF && v[j3]) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: v[j1]--; 4fd800b3a8 2011-02-23 kinaba: v[j2]--; 4fd800b3a8 2011-02-23 kinaba: v[j3]--; 4fd800b3a8 2011-02-23 kinaba: pair<int,int> a = rec(i+1, N, v, cost); 4fd800b3a8 2011-02-23 kinaba: a.first -= 3; 4fd800b3a8 2011-02-23 kinaba: a.second += cost[i][j1] + cost[i][j2] + cost[i][j3]; 4fd800b3a8 2011-02-23 kinaba: best = min(best, a); 4fd800b3a8 2011-02-23 kinaba: v[j1]++; 4fd800b3a8 2011-02-23 kinaba: v[j2]++; 4fd800b3a8 2011-02-23 kinaba: v[j3]++; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: return memo[key] = best; 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 vector <int> &Expected, const vector <int> &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(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_[] = {0,0,10}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 4fd800b3a8 2011-02-23 kinaba: int y_[] = {0,10,4}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 4fd800b3a8 2011-02-23 kinaba: int maxPartners = 1; 4fd800b3a8 2011-02-23 kinaba: int minDistance = 1; 4fd800b3a8 2011-02-23 kinaba: int RetVal_[] = {1, 10 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, TwinTowns().optimalTwinTowns(x, y, maxPartners, minDistance)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: int x_[] = {0,0,10}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 4fd800b3a8 2011-02-23 kinaba: int y_[] = {0,10,4}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 4fd800b3a8 2011-02-23 kinaba: int maxPartners = 1; 4fd800b3a8 2011-02-23 kinaba: int minDistance = 11; 4fd800b3a8 2011-02-23 kinaba: int RetVal_[] = {1, 14 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, TwinTowns().optimalTwinTowns(x, y, maxPartners, minDistance)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: int x_[] = {0,10,0,10}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 4fd800b3a8 2011-02-23 kinaba: int y_[] = {0,0,20,20}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 4fd800b3a8 2011-02-23 kinaba: int maxPartners = 1; 4fd800b3a8 2011-02-23 kinaba: int minDistance = 1; 4fd800b3a8 2011-02-23 kinaba: int RetVal_[] = {2, 20 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, TwinTowns().optimalTwinTowns(x, y, maxPartners, minDistance)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: int x_[] = {0,10,0,10}; 4fd800b3a8 2011-02-23 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 4fd800b3a8 2011-02-23 kinaba: int y_[] = {0,0,20,20}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 4fd800b3a8 2011-02-23 kinaba: int maxPartners = 2; 4fd800b3a8 2011-02-23 kinaba: int minDistance = 10; 4fd800b3a8 2011-02-23 kinaba: int RetVal_[] = {4, 60 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, TwinTowns().optimalTwinTowns(x, y, maxPartners, minDistance)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: int x_[] = {0,0,0,0,0,0,0,0,0}; 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}; 4fd800b3a8 2011-02-23 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 4fd800b3a8 2011-02-23 kinaba: int maxPartners = 3; 4fd800b3a8 2011-02-23 kinaba: int minDistance = 6; 4fd800b3a8 2011-02-23 kinaba: int RetVal_[] = {6, 40 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, TwinTowns().optimalTwinTowns(x, y, maxPartners, minDistance)); } 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: