Artifact Content
Not logged in

Artifact 0d8ab1eb2aa0ceeb07c44f6cc3583ffbe5ade778


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <functional>
#include <cassert>
using namespace std;
typedef long long LL;
typedef LL BITS;

class PickingUp
{
public:
	vector<int> fairPickingUp(vector<long long> score1, vector<long long> score2) 
	{
		LL t = accumulate(score2.begin(), score2.end(), 0LL);
		vector<LL> s;
		transform(score1.begin(), score1.end(), score2.begin(), back_inserter(s), plus<LL>());

		int N = s.size() / 2;

		vector< map<LL,BITS> > L = try_all( s.begin(), N );
		vector< map<LL,BITS> > R = try_all( s.begin()+N, N );

		pair<LL, BITS> best(0x7fffffffffffffffLL, 0);
		for(int i=0; i<=N; ++i)
			best = min(best, best_possible( L[i], R[N-i], t, N ));

		vector<int> ans;
		for(int i=0; i!=s.size(); ++i)
			ans.push_back( (-best.second & (1LL<<s.size()-1-i)) ? 1 : 2 );
		return ans;
	}

	pair<LL, BITS> best_possible( map<LL,BITS>& L, map<LL,BITS>& R, LL t, int N )
	{
		typedef map<LL,BITS>::iterator mit;

		pair<LL, BITS> best(0x7fffffffffffffffLL, 0);
		for(mit i=L.begin(); i!=L.end(); ++i)
		{
			LL as = i->first;
			LL am = i->second;
			mit j = R.lower_bound(t-as);
			if( j!=R.end() ) {
				LL bs = j->first;
				LL bm = j->second;
				best = min(best, make_pair(abs(as+bs-t), -((am<<N)|bm)));
			}
			if( j!=R.begin() ) {
				--j;
				LL bs = j->first;
				LL bm = j->second;
				best = min(best, make_pair(abs(as+bs-t), -((am<<N)|bm)));
			}
		}

		return best;
	}

	template<typename Ite>
	vector< map<LL,BITS> > try_all( Ite s, int N )
	{
		vector< map<LL,BITS> > a(N+1);
		for(BITS m=(1<<N)-1; m>=0; --m)
		{
			int nb = 0;
			LL  sm = 0;
			for(int i=0; i<N; ++i)
				if( m & (1LL<<N-1-i) )
					++nb, sm+=*(s+i);
			if( !a[nb].count(sm) )
				a[nb][sm] = m;
		}
		return a;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }

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(); }
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;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	long score1_[] = {5,9};
	  vector<long long> score1(score1_, score1_+sizeof(score1_)/sizeof(*score1_)); 
	long score2_[] = {8,6};
	  vector<long long> score2(score2_, score2_+sizeof(score2_)/sizeof(*score2_)); 
	int RetVal_[] = {1, 2 };
	  vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, PickingUp().fairPickingUp(score1, score2)); }
int Test_(Case_<1>) {
	long score1_[] = {2,3,4,7};
	  vector<long long> score1(score1_, score1_+sizeof(score1_)/sizeof(*score1_)); 
	long score2_[] = {2,4,5,8};
	  vector<long long> score2(score2_, score2_+sizeof(score2_)/sizeof(*score2_)); 
	int RetVal_[] = {1, 2, 2, 1 };
	  vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, PickingUp().fairPickingUp(score1, score2)); }
int Test_(Case_<2>) {
	long score1_[] = {1,5,6,8};
	  vector<long long> score1(score1_, score1_+sizeof(score1_)/sizeof(*score1_)); 
	long score2_[] = {7,5,3,1};
	  vector<long long> score2(score2_, score2_+sizeof(score2_)/sizeof(*score2_)); 
	int RetVal_[] = {1, 2, 1, 2 };
	  vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, PickingUp().fairPickingUp(score1, score2)); }
int Test_(Case_<3>) {
	long score1_[] = {300,300,300,300};
	  vector<long long> score1(score1_, score1_+sizeof(score1_)/sizeof(*score1_)); 
	long score2_[] = {600,10,10,10};
	  vector<long long> score2(score2_, score2_+sizeof(score2_)/sizeof(*score2_)); 
	int RetVal_[] = {2, 1, 1, 2 };
	  vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, PickingUp().fairPickingUp(score1, score2)); }
int Test_(Case_<4>) {
	long score1_[] = {50,50,50,1};
	  vector<long long> score1(score1_, score1_+sizeof(score1_)/sizeof(*score1_)); 
	long score2_[] = {30,30,30,150};
	  vector<long long> score2(score2_, score2_+sizeof(score2_)/sizeof(*score2_)); 
	int RetVal_[] = {1, 2, 2, 1 };
	  vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, PickingUp().fairPickingUp(score1, score2)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE