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 <functional> 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: typedef LL BITS; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class PickingUp 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: vector<int> fairPickingUp(vector<long long> score1, vector<long long> score2) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL t = accumulate(score2.begin(), score2.end(), 0LL); 4fd800b3a8 2011-02-23 kinaba: vector<LL> s; 4fd800b3a8 2011-02-23 kinaba: transform(score1.begin(), score1.end(), score2.begin(), back_inserter(s), plus<LL>()); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int N = s.size() / 2; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector< map<LL,BITS> > L = try_all( s.begin(), N ); 4fd800b3a8 2011-02-23 kinaba: vector< map<LL,BITS> > R = try_all( s.begin()+N, N ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: pair<LL, BITS> best(0x7fffffffffffffffLL, 0); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<=N; ++i) 4fd800b3a8 2011-02-23 kinaba: best = min(best, best_possible( L[i], R[N-i], t, N )); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<int> ans; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i!=s.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: ans.push_back( (-best.second & (1LL<<s.size()-1-i)) ? 1 : 2 ); 4fd800b3a8 2011-02-23 kinaba: return ans; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: pair<LL, BITS> best_possible( map<LL,BITS>& L, map<LL,BITS>& R, LL t, int N ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: typedef map<LL,BITS>::iterator mit; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: pair<LL, BITS> best(0x7fffffffffffffffLL, 0); 4fd800b3a8 2011-02-23 kinaba: for(mit i=L.begin(); i!=L.end(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL as = i->first; 4fd800b3a8 2011-02-23 kinaba: LL am = i->second; 4fd800b3a8 2011-02-23 kinaba: mit j = R.lower_bound(t-as); 4fd800b3a8 2011-02-23 kinaba: if( j!=R.end() ) { 4fd800b3a8 2011-02-23 kinaba: LL bs = j->first; 4fd800b3a8 2011-02-23 kinaba: LL bm = j->second; 4fd800b3a8 2011-02-23 kinaba: best = min(best, make_pair(abs(as+bs-t), -((am<<N)|bm))); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: if( j!=R.begin() ) { 4fd800b3a8 2011-02-23 kinaba: --j; 4fd800b3a8 2011-02-23 kinaba: LL bs = j->first; 4fd800b3a8 2011-02-23 kinaba: LL bm = j->second; 4fd800b3a8 2011-02-23 kinaba: best = min(best, make_pair(abs(as+bs-t), -((am<<N)|bm))); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: return best; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<typename Ite> 4fd800b3a8 2011-02-23 kinaba: vector< map<LL,BITS> > try_all( Ite s, int N ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector< map<LL,BITS> > a(N+1); 4fd800b3a8 2011-02-23 kinaba: for(BITS m=(1<<N)-1; m>=0; --m) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int nb = 0; 4fd800b3a8 2011-02-23 kinaba: LL sm = 0; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<N; ++i) 4fd800b3a8 2011-02-23 kinaba: if( m & (1LL<<N-1-i) ) 4fd800b3a8 2011-02-23 kinaba: ++nb, sm+=*(s+i); 4fd800b3a8 2011-02-23 kinaba: if( !a[nb].count(sm) ) 4fd800b3a8 2011-02-23 kinaba: a[nb][sm] = m; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return a; 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: long score1_[] = {5,9}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> score1(score1_, score1_+sizeof(score1_)/sizeof(*score1_)); 4fd800b3a8 2011-02-23 kinaba: long score2_[] = {8,6}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> score2(score2_, score2_+sizeof(score2_)/sizeof(*score2_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal_[] = {1, 2 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, PickingUp().fairPickingUp(score1, score2)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: long score1_[] = {2,3,4,7}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> score1(score1_, score1_+sizeof(score1_)/sizeof(*score1_)); 4fd800b3a8 2011-02-23 kinaba: long score2_[] = {2,4,5,8}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> score2(score2_, score2_+sizeof(score2_)/sizeof(*score2_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal_[] = {1, 2, 2, 1 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, PickingUp().fairPickingUp(score1, score2)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: long score1_[] = {1,5,6,8}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> score1(score1_, score1_+sizeof(score1_)/sizeof(*score1_)); 4fd800b3a8 2011-02-23 kinaba: long score2_[] = {7,5,3,1}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> score2(score2_, score2_+sizeof(score2_)/sizeof(*score2_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal_[] = {1, 2, 1, 2 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, PickingUp().fairPickingUp(score1, score2)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: long score1_[] = {300,300,300,300}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> score1(score1_, score1_+sizeof(score1_)/sizeof(*score1_)); 4fd800b3a8 2011-02-23 kinaba: long score2_[] = {600,10,10,10}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> score2(score2_, score2_+sizeof(score2_)/sizeof(*score2_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal_[] = {2, 1, 1, 2 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, PickingUp().fairPickingUp(score1, score2)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: long score1_[] = {50,50,50,1}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> score1(score1_, score1_+sizeof(score1_)/sizeof(*score1_)); 4fd800b3a8 2011-02-23 kinaba: long score2_[] = {30,30,30,150}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> score2(score2_, score2_+sizeof(score2_)/sizeof(*score2_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal_[] = {1, 2, 2, 1 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, PickingUp().fairPickingUp(score1, score2)); } 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: