f3a2b05988 2013-10-15 kinaba: #include <iostream> f3a2b05988 2013-10-15 kinaba: #include <sstream> f3a2b05988 2013-10-15 kinaba: #include <iomanip> f3a2b05988 2013-10-15 kinaba: #include <vector> f3a2b05988 2013-10-15 kinaba: #include <string> f3a2b05988 2013-10-15 kinaba: #include <map> f3a2b05988 2013-10-15 kinaba: #include <set> f3a2b05988 2013-10-15 kinaba: #include <algorithm> f3a2b05988 2013-10-15 kinaba: #include <numeric> f3a2b05988 2013-10-15 kinaba: #include <iterator> f3a2b05988 2013-10-15 kinaba: #include <functional> f3a2b05988 2013-10-15 kinaba: #include <complex> f3a2b05988 2013-10-15 kinaba: #include <queue> f3a2b05988 2013-10-15 kinaba: #include <stack> f3a2b05988 2013-10-15 kinaba: #include <cmath> f3a2b05988 2013-10-15 kinaba: #include <cassert> f3a2b05988 2013-10-15 kinaba: #include <tuple> f3a2b05988 2013-10-15 kinaba: using namespace std; f3a2b05988 2013-10-15 kinaba: typedef long long LL; f3a2b05988 2013-10-15 kinaba: typedef complex<double> CMP; f3a2b05988 2013-10-15 kinaba: f3a2b05988 2013-10-15 kinaba: class MayTheBestPetWin { public: f3a2b05988 2013-10-15 kinaba: int calc(vector <int> A, vector <int> B) f3a2b05988 2013-10-15 kinaba: { f3a2b05988 2013-10-15 kinaba: const int N = A.size(); f3a2b05988 2013-10-15 kinaba: vector<int> C(N); f3a2b05988 2013-10-15 kinaba: for(int i=0; i<N; ++i) f3a2b05988 2013-10-15 kinaba: C[i] = A[i] + B[i]; f3a2b05988 2013-10-15 kinaba: f3a2b05988 2013-10-15 kinaba: const int allmin = accumulate(A.begin(), A.end(), 0); f3a2b05988 2013-10-15 kinaba: const int allmax = accumulate(B.begin(), B.end(), 0); f3a2b05988 2013-10-15 kinaba: const int allsum = accumulate(C.begin(), C.end(), 0); f3a2b05988 2013-10-15 kinaba: f3a2b05988 2013-10-15 kinaba: vector<bool> can(allsum+1); f3a2b05988 2013-10-15 kinaba: can[0] = true; f3a2b05988 2013-10-15 kinaba: for(int i=0; i<N; ++i) f3a2b05988 2013-10-15 kinaba: for(int k=allsum; k>=C[i]; --k) f3a2b05988 2013-10-15 kinaba: if(can[k-C[i]]) f3a2b05988 2013-10-15 kinaba: can[k] = true; f3a2b05988 2013-10-15 kinaba: f3a2b05988 2013-10-15 kinaba: int best = allsum; f3a2b05988 2013-10-15 kinaba: for(int i=0; i<=allsum; ++i) f3a2b05988 2013-10-15 kinaba: if(can[i]) f3a2b05988 2013-10-15 kinaba: best = min(best, max(abs(i-allmin), abs(allmax-i))); f3a2b05988 2013-10-15 kinaba: return best; f3a2b05988 2013-10-15 kinaba: } f3a2b05988 2013-10-15 kinaba: }; f3a2b05988 2013-10-15 kinaba: f3a2b05988 2013-10-15 kinaba: // BEGIN CUT HERE f3a2b05988 2013-10-15 kinaba: #include <ctime> f3a2b05988 2013-10-15 kinaba: double start_time; string timer() f3a2b05988 2013-10-15 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } f3a2b05988 2013-10-15 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) f3a2b05988 2013-10-15 kinaba: { os << "{ "; f3a2b05988 2013-10-15 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) f3a2b05988 2013-10-15 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } f3a2b05988 2013-10-15 kinaba: void verify_case(const int& Expected, const int& Received) { f3a2b05988 2013-10-15 kinaba: bool ok = (Expected == Received); f3a2b05988 2013-10-15 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; f3a2b05988 2013-10-15 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } f3a2b05988 2013-10-15 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); f3a2b05988 2013-10-15 kinaba: #define END verify_case(_, MayTheBestPetWin().calc(A, B));} f3a2b05988 2013-10-15 kinaba: int main(){ f3a2b05988 2013-10-15 kinaba: f3a2b05988 2013-10-15 kinaba: CASE(0) f3a2b05988 2013-10-15 kinaba: int A_[] = {3,4,4,7}; f3a2b05988 2013-10-15 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); f3a2b05988 2013-10-15 kinaba: int B_[] = {3,4,4,7}; f3a2b05988 2013-10-15 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); f3a2b05988 2013-10-15 kinaba: int _ = 2; f3a2b05988 2013-10-15 kinaba: END f3a2b05988 2013-10-15 kinaba: CASE(1) f3a2b05988 2013-10-15 kinaba: int A_[] = {1,3,5,4,5}; f3a2b05988 2013-10-15 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); f3a2b05988 2013-10-15 kinaba: int B_[] = {2,5,6,8,7}; f3a2b05988 2013-10-15 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); f3a2b05988 2013-10-15 kinaba: int _ = 5; f3a2b05988 2013-10-15 kinaba: END f3a2b05988 2013-10-15 kinaba: CASE(2) f3a2b05988 2013-10-15 kinaba: int A_[] = {2907,949,1674,6092,8608,5186,2630,970,1050,2415,1923,2697,5571,6941,8065,4710,716,756,5185,1341,993,5092,248,1895,4223,1783,3844,3531,2431,1755,2837,4015}; f3a2b05988 2013-10-15 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); f3a2b05988 2013-10-15 kinaba: int B_[] = {7296,6954,4407,9724,8645,8065,9323,8433,1352,9618,6487,7309,9297,8999,9960,5653,4721,7623,6017,7320,3513,6642,6359,3145,7233,5077,6457,3605,2911,4679,5381,6574}; f3a2b05988 2013-10-15 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); f3a2b05988 2013-10-15 kinaba: int _ = 52873; f3a2b05988 2013-10-15 kinaba: END f3a2b05988 2013-10-15 kinaba: /* f3a2b05988 2013-10-15 kinaba: CASE(3) f3a2b05988 2013-10-15 kinaba: int A_[] = ; f3a2b05988 2013-10-15 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); f3a2b05988 2013-10-15 kinaba: int B_[] = ; f3a2b05988 2013-10-15 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); f3a2b05988 2013-10-15 kinaba: int _ = ; f3a2b05988 2013-10-15 kinaba: END f3a2b05988 2013-10-15 kinaba: CASE(4) f3a2b05988 2013-10-15 kinaba: int A_[] = ; f3a2b05988 2013-10-15 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); f3a2b05988 2013-10-15 kinaba: int B_[] = ; f3a2b05988 2013-10-15 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); f3a2b05988 2013-10-15 kinaba: int _ = ; f3a2b05988 2013-10-15 kinaba: END f3a2b05988 2013-10-15 kinaba: */ f3a2b05988 2013-10-15 kinaba: } f3a2b05988 2013-10-15 kinaba: // END CUT HERE