0048d1d21b 2013-11-01 kinaba: #include <iostream> 0048d1d21b 2013-11-01 kinaba: #include <sstream> 0048d1d21b 2013-11-01 kinaba: #include <iomanip> 0048d1d21b 2013-11-01 kinaba: #include <vector> 0048d1d21b 2013-11-01 kinaba: #include <string> 0048d1d21b 2013-11-01 kinaba: #include <map> 0048d1d21b 2013-11-01 kinaba: #include <set> 0048d1d21b 2013-11-01 kinaba: #include <algorithm> 0048d1d21b 2013-11-01 kinaba: #include <numeric> 0048d1d21b 2013-11-01 kinaba: #include <iterator> 0048d1d21b 2013-11-01 kinaba: #include <functional> 0048d1d21b 2013-11-01 kinaba: #include <complex> 0048d1d21b 2013-11-01 kinaba: #include <queue> 0048d1d21b 2013-11-01 kinaba: #include <stack> 0048d1d21b 2013-11-01 kinaba: #include <cmath> 0048d1d21b 2013-11-01 kinaba: #include <cassert> 0048d1d21b 2013-11-01 kinaba: #include <tuple> 0048d1d21b 2013-11-01 kinaba: using namespace std; 0048d1d21b 2013-11-01 kinaba: typedef long long LL; 0048d1d21b 2013-11-01 kinaba: typedef complex<double> CMP; 0048d1d21b 2013-11-01 kinaba: 0048d1d21b 2013-11-01 kinaba: class AstronomicalRecords { public: 0048d1d21b 2013-11-01 kinaba: int minimalPlanets(vector <int> A, vector <int> B) 0048d1d21b 2013-11-01 kinaba: { 0048d1d21b 2013-11-01 kinaba: int best = A.size() + B.size(); 0048d1d21b 2013-11-01 kinaba: for(int i=0; i<A.size(); ++i) 0048d1d21b 2013-11-01 kinaba: for(int k=0; k<B.size(); ++k) 0048d1d21b 2013-11-01 kinaba: { 0048d1d21b 2013-11-01 kinaba: int score = 1; 0048d1d21b 2013-11-01 kinaba: { 0048d1d21b 2013-11-01 kinaba: vector<LL> AA, BB; 0048d1d21b 2013-11-01 kinaba: for(int x=0; x<i; ++x) AA.push_back(LL(A[x])*B[k]); 0048d1d21b 2013-11-01 kinaba: for(int x=0; x<k; ++x) BB.push_back(LL(B[x])*A[i]); 0048d1d21b 2013-11-01 kinaba: score += AA.size()+BB.size()-lcs(AA, BB); 0048d1d21b 2013-11-01 kinaba: } 0048d1d21b 2013-11-01 kinaba: { 0048d1d21b 2013-11-01 kinaba: vector<LL> AA, BB; 0048d1d21b 2013-11-01 kinaba: for(int x=i+1; x<A.size(); ++x) AA.push_back(LL(A[x])*B[k]); 0048d1d21b 2013-11-01 kinaba: for(int x=k+1; x<B.size(); ++x) BB.push_back(LL(B[x])*A[i]); 0048d1d21b 2013-11-01 kinaba: score += AA.size()+BB.size()-lcs(AA, BB); 0048d1d21b 2013-11-01 kinaba: } 0048d1d21b 2013-11-01 kinaba: best = min(best, score); 0048d1d21b 2013-11-01 kinaba: } 0048d1d21b 2013-11-01 kinaba: return best; 0048d1d21b 2013-11-01 kinaba: } 0048d1d21b 2013-11-01 kinaba: 0048d1d21b 2013-11-01 kinaba: int lcs(vector<LL> A, vector<LL> B) 0048d1d21b 2013-11-01 kinaba: { 0048d1d21b 2013-11-01 kinaba: vector< vector<int> > dp(A.size()+1, vector<int>(B.size()+1)); 0048d1d21b 2013-11-01 kinaba: 0048d1d21b 2013-11-01 kinaba: for(int i=0; i<A.size(); ++i) 0048d1d21b 2013-11-01 kinaba: for(int k=0; k<B.size(); ++k) 0048d1d21b 2013-11-01 kinaba: { 0048d1d21b 2013-11-01 kinaba: int z = max(dp[i][k+1], dp[i+1][k]); 0048d1d21b 2013-11-01 kinaba: if(A[i] == B[k]) 0048d1d21b 2013-11-01 kinaba: z = max(z, dp[i][k]+1); 0048d1d21b 2013-11-01 kinaba: dp[i+1][k+1] = z; 0048d1d21b 2013-11-01 kinaba: } 0048d1d21b 2013-11-01 kinaba: return dp[A.size()][B.size()]; 0048d1d21b 2013-11-01 kinaba: } 0048d1d21b 2013-11-01 kinaba: }; 0048d1d21b 2013-11-01 kinaba: 0048d1d21b 2013-11-01 kinaba: // BEGIN CUT HERE 0048d1d21b 2013-11-01 kinaba: #include <ctime> 0048d1d21b 2013-11-01 kinaba: double start_time; string timer() 0048d1d21b 2013-11-01 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 0048d1d21b 2013-11-01 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 0048d1d21b 2013-11-01 kinaba: { os << "{ "; 0048d1d21b 2013-11-01 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 0048d1d21b 2013-11-01 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 0048d1d21b 2013-11-01 kinaba: void verify_case(const int& Expected, const int& Received) { 0048d1d21b 2013-11-01 kinaba: bool ok = (Expected == Received); 0048d1d21b 2013-11-01 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 0048d1d21b 2013-11-01 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 0048d1d21b 2013-11-01 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 0048d1d21b 2013-11-01 kinaba: #define END verify_case(_, AstronomicalRecords().minimalPlanets(A, B));} 0048d1d21b 2013-11-01 kinaba: int main(){ 0048d1d21b 2013-11-01 kinaba: 0048d1d21b 2013-11-01 kinaba: CASE(0) 0048d1d21b 2013-11-01 kinaba: int A_[] = {1,2,1,2,1}; 0048d1d21b 2013-11-01 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 0048d1d21b 2013-11-01 kinaba: int B_[] = {2,1,2,1,2}; 0048d1d21b 2013-11-01 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 0048d1d21b 2013-11-01 kinaba: int _ = 6; 0048d1d21b 2013-11-01 kinaba: END 0048d1d21b 2013-11-01 kinaba: CASE(1) 0048d1d21b 2013-11-01 kinaba: int A_[] = {1,2,3,4}; 0048d1d21b 2013-11-01 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 0048d1d21b 2013-11-01 kinaba: int B_[] = {2,4,6,8}; 0048d1d21b 2013-11-01 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 0048d1d21b 2013-11-01 kinaba: int _ = 4; 0048d1d21b 2013-11-01 kinaba: END 0048d1d21b 2013-11-01 kinaba: CASE(2) 0048d1d21b 2013-11-01 kinaba: int A_[] = {2,3,2,3,2,3,2}; 0048d1d21b 2013-11-01 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 0048d1d21b 2013-11-01 kinaba: int B_[] = {600,700,600,700,600,700,600}; 0048d1d21b 2013-11-01 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 0048d1d21b 2013-11-01 kinaba: int _ = 10; 0048d1d21b 2013-11-01 kinaba: END 0048d1d21b 2013-11-01 kinaba: CASE(3) 0048d1d21b 2013-11-01 kinaba: int A_[] = {1,2,3,4,5,6,7,8,9}; 0048d1d21b 2013-11-01 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 0048d1d21b 2013-11-01 kinaba: int B_[] = {6,7,8,9,10,11,12}; 0048d1d21b 2013-11-01 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 0048d1d21b 2013-11-01 kinaba: int _ = 12; 0048d1d21b 2013-11-01 kinaba: END 0048d1d21b 2013-11-01 kinaba: CASE(4) 0048d1d21b 2013-11-01 kinaba: int A_[] = {100000000,200000000}; 0048d1d21b 2013-11-01 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 0048d1d21b 2013-11-01 kinaba: int B_[] = {200000000,100000000}; 0048d1d21b 2013-11-01 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 0048d1d21b 2013-11-01 kinaba: int _ = 3; 0048d1d21b 2013-11-01 kinaba: END 0048d1d21b 2013-11-01 kinaba: CASE(5) 0048d1d21b 2013-11-01 kinaba: int A_[] = {7,14,7,14,7,1}; 0048d1d21b 2013-11-01 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 0048d1d21b 2013-11-01 kinaba: int B_[] = {10,5,10,5,10,1}; 0048d1d21b 2013-11-01 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 0048d1d21b 2013-11-01 kinaba: int _ = 8; 0048d1d21b 2013-11-01 kinaba: END 0048d1d21b 2013-11-01 kinaba: /* 0048d1d21b 2013-11-01 kinaba: CASE(6) 0048d1d21b 2013-11-01 kinaba: int A_[] = ; 0048d1d21b 2013-11-01 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 0048d1d21b 2013-11-01 kinaba: int B_[] = ; 0048d1d21b 2013-11-01 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 0048d1d21b 2013-11-01 kinaba: int _ = ; 0048d1d21b 2013-11-01 kinaba: END 0048d1d21b 2013-11-01 kinaba: */ 0048d1d21b 2013-11-01 kinaba: } 0048d1d21b 2013-11-01 kinaba: // END CUT HERE