Artifact Content
Not logged in

Artifact b2dc4bd6b9ccd651dc83702906002ac7aab6fde7


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
#ifdef __GNUC__
#include <ext/hash_map>
#define unordered_map __gnu_cxx::hash_map
#else
#include <unordered_map>
#endif
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

template<typename T>
struct DP2
{
	const int N1, N2;
	vector<T> data;
	DP2(int N1, int N2, const T& t = T())
		: N1(N1), N2(N2), data(N1*N2, t) { assert(data.size()*sizeof(T)<(1<<26)); }
	T& operator()(int i1, int i2)
		{ return data[ (i1*N2)+i2 ]; }
	void swap(DP2& rhs)
		{ data.swap(rhs.data); }
};

class HeavyBooks { public:
	vector <int> findWeight(vector <int> books, vector <int> moves)
	{
		const int K = moves[0];

		vector<int> tomek, wojtek;
		for(int i=0; i<K; ++i)
			wojtek.push_back(i);

		for(int i=1; i<moves.size(); ++i) {
			vector<int>& fr = (i%2 == 0 ? tomek : wojtek);
			vector<int>& to = (i%2 == 0 ? wojtek : tomek);
			for(int k=0; k<moves[i] && !fr.empty(); ++k) {
				to.push_back(fr.back());
				fr.pop_back();
			}
			sort(fr.begin(), fr.end());
			sort(to.begin(), to.end());
		}

		vector<bool> for_tomek(K);
		for(int i=0; i<tomek.size(); ++i)
			for_tomek[tomek[i]] = true;

		sort(books.begin(), books.end());

		DP2< pair<int,int> > dp(books.size(), K+1);
		for(int i=0; i<books.size(); ++i)
		for(int k=0; k<=K; ++k)
		{
			int DifIncr = for_tomek[k-1] ? -books[i] : +books[i];
			int SumIncr = books[i];
			if( i == 0 ) {
				if( k == 0 )
					dp(i, k) = make_pair(0, 0);
				else if( k == 1 )
					dp(i, k) = make_pair(DifIncr, SumIncr);
				else
					dp(i, k) = make_pair(-123456789, -123456789);
			} else {
				dp(i, k) = dp(i-1, k);
				if( k == 0 ) {
				} else {
					int PrevDif = dp(i-1,k-1).first;
					int PrevSum = dp(i-1,k-1).second;
					pair<int,int> next(PrevDif+DifIncr, PrevSum+SumIncr);
					if( dp(i,k) < next )
						dp(i,k) = next;
				}
			}
		}

		int WminusT = dp(books.size()-1, K).first;
		int WplusT = dp(books.size()-1, K).second;
		vector<int> answer;
		answer.push_back( (WplusT - WminusT)/2 );
		answer.push_back( (WplusT + WminusT)/2 );
		return answer;
	}
};

// 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> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const vector <int>& Expected, const vector <int>& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, HeavyBooks().findWeight(books, moves));}
int main(){

CASE(0)
	int books_[] = {5,2,3,1};
	  vector <int> books(books_, books_+sizeof(books_)/sizeof(*books_)); 
	int moves_[] = {3,2,1,1,1};
	  vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); 
	int __[] = {3, 7 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(1)
	int books_[] = {10, 100, 1000};
	  vector <int> books(books_, books_+sizeof(books_)/sizeof(*books_)); 
	int moves_[] = {2, 3};
	  vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); 
	int __[] = {110, 0 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(2)
	int books_[] = {155870,565381};
	  vector <int> books(books_, books_+sizeof(books_)/sizeof(*books_)); 
	int moves_[] = {1,1,1};
	  vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); 
	int __[] = {0, 565381 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(3)
	int books_[] = {500,500,500,500};
	  vector <int> books(books_, books_+sizeof(books_)/sizeof(*books_)); 
	int moves_[] = {4,1,1,3,2};
	  vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); 
	int __[] = {500, 1500 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(4)
	int books_[] = {1,1,1,1,1000000};
	  vector <int> books(books_, books_+sizeof(books_)/sizeof(*books_)); 
	int moves_[] = {1};
	  vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); 
	int __[] = {0, 1000000 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(5)
	int books_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
	  vector <int> books(books_, books_+sizeof(books_)/sizeof(*books_)); 
	int moves_[] = {20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
	  vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); 
	int __[] = {110, 100 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
/*
CASE(6)
	int books_[] = ;
	  vector <int> books(books_, books_+sizeof(books_)/sizeof(*books_)); 
	int moves_[] = ;
	  vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); 
	int __[] = ;
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(7)
	int books_[] = ;
	  vector <int> books(books_, books_+sizeof(books_)/sizeof(*books_)); 
	int moves_[] = ;
	  vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); 
	int __[] = ;
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
*/
}
// END CUT HERE