f6e551e04b 2012-05-19 kinaba: #include <iostream> f6e551e04b 2012-05-19 kinaba: #include <sstream> f6e551e04b 2012-05-19 kinaba: #include <iomanip> f6e551e04b 2012-05-19 kinaba: #include <vector> f6e551e04b 2012-05-19 kinaba: #include <string> f6e551e04b 2012-05-19 kinaba: #include <map> f6e551e04b 2012-05-19 kinaba: #include <set> f6e551e04b 2012-05-19 kinaba: #include <algorithm> f6e551e04b 2012-05-19 kinaba: #include <numeric> f6e551e04b 2012-05-19 kinaba: #include <iterator> f6e551e04b 2012-05-19 kinaba: #include <functional> f6e551e04b 2012-05-19 kinaba: #include <complex> f6e551e04b 2012-05-19 kinaba: #include <queue> f6e551e04b 2012-05-19 kinaba: #include <stack> f6e551e04b 2012-05-19 kinaba: #include <cmath> f6e551e04b 2012-05-19 kinaba: #include <cassert> f6e551e04b 2012-05-19 kinaba: #include <cstring> f6e551e04b 2012-05-19 kinaba: #ifdef __GNUC__ f6e551e04b 2012-05-19 kinaba: #include <ext/hash_map> f6e551e04b 2012-05-19 kinaba: #define unordered_map __gnu_cxx::hash_map f6e551e04b 2012-05-19 kinaba: #else f6e551e04b 2012-05-19 kinaba: #include <unordered_map> f6e551e04b 2012-05-19 kinaba: #endif f6e551e04b 2012-05-19 kinaba: using namespace std; f6e551e04b 2012-05-19 kinaba: typedef long long LL; f6e551e04b 2012-05-19 kinaba: typedef complex<double> CMP; f6e551e04b 2012-05-19 kinaba: f6e551e04b 2012-05-19 kinaba: template<typename T> f6e551e04b 2012-05-19 kinaba: struct DP2 f6e551e04b 2012-05-19 kinaba: { f6e551e04b 2012-05-19 kinaba: const int N1, N2; f6e551e04b 2012-05-19 kinaba: vector<T> data; f6e551e04b 2012-05-19 kinaba: DP2(int N1, int N2, const T& t = T()) f6e551e04b 2012-05-19 kinaba: : N1(N1), N2(N2), data(N1*N2, t) { assert(data.size()*sizeof(T)<(1<<26)); } f6e551e04b 2012-05-19 kinaba: T& operator()(int i1, int i2) f6e551e04b 2012-05-19 kinaba: { return data[ (i1*N2)+i2 ]; } f6e551e04b 2012-05-19 kinaba: void swap(DP2& rhs) f6e551e04b 2012-05-19 kinaba: { data.swap(rhs.data); } f6e551e04b 2012-05-19 kinaba: }; f6e551e04b 2012-05-19 kinaba: f6e551e04b 2012-05-19 kinaba: class HeavyBooks { public: f6e551e04b 2012-05-19 kinaba: vector <int> findWeight(vector <int> books, vector <int> moves) f6e551e04b 2012-05-19 kinaba: { f6e551e04b 2012-05-19 kinaba: const int K = moves[0]; f6e551e04b 2012-05-19 kinaba: f6e551e04b 2012-05-19 kinaba: vector<int> tomek, wojtek; f6e551e04b 2012-05-19 kinaba: for(int i=0; i<K; ++i) f6e551e04b 2012-05-19 kinaba: wojtek.push_back(i); f6e551e04b 2012-05-19 kinaba: f6e551e04b 2012-05-19 kinaba: for(int i=1; i<moves.size(); ++i) { f6e551e04b 2012-05-19 kinaba: vector<int>& fr = (i%2 == 0 ? tomek : wojtek); f6e551e04b 2012-05-19 kinaba: vector<int>& to = (i%2 == 0 ? wojtek : tomek); f6e551e04b 2012-05-19 kinaba: for(int k=0; k<moves[i] && !fr.empty(); ++k) { f6e551e04b 2012-05-19 kinaba: to.push_back(fr.back()); f6e551e04b 2012-05-19 kinaba: fr.pop_back(); f6e551e04b 2012-05-19 kinaba: } f6e551e04b 2012-05-19 kinaba: sort(fr.begin(), fr.end()); f6e551e04b 2012-05-19 kinaba: sort(to.begin(), to.end()); f6e551e04b 2012-05-19 kinaba: } f6e551e04b 2012-05-19 kinaba: f6e551e04b 2012-05-19 kinaba: vector<bool> for_tomek(K); f6e551e04b 2012-05-19 kinaba: for(int i=0; i<tomek.size(); ++i) f6e551e04b 2012-05-19 kinaba: for_tomek[tomek[i]] = true; f6e551e04b 2012-05-19 kinaba: f6e551e04b 2012-05-19 kinaba: sort(books.begin(), books.end()); f6e551e04b 2012-05-19 kinaba: f6e551e04b 2012-05-19 kinaba: DP2< pair<int,int> > dp(books.size(), K+1); f6e551e04b 2012-05-19 kinaba: for(int i=0; i<books.size(); ++i) f6e551e04b 2012-05-19 kinaba: for(int k=0; k<=K; ++k) f6e551e04b 2012-05-19 kinaba: { f6e551e04b 2012-05-19 kinaba: int DifIncr = for_tomek[k-1] ? -books[i] : +books[i]; f6e551e04b 2012-05-19 kinaba: int SumIncr = books[i]; f6e551e04b 2012-05-19 kinaba: if( i == 0 ) { f6e551e04b 2012-05-19 kinaba: if( k == 0 ) f6e551e04b 2012-05-19 kinaba: dp(i, k) = make_pair(0, 0); f6e551e04b 2012-05-19 kinaba: else if( k == 1 ) f6e551e04b 2012-05-19 kinaba: dp(i, k) = make_pair(DifIncr, SumIncr); f6e551e04b 2012-05-19 kinaba: else f6e551e04b 2012-05-19 kinaba: dp(i, k) = make_pair(-123456789, -123456789); f6e551e04b 2012-05-19 kinaba: } else { f6e551e04b 2012-05-19 kinaba: dp(i, k) = dp(i-1, k); f6e551e04b 2012-05-19 kinaba: if( k == 0 ) { f6e551e04b 2012-05-19 kinaba: } else { f6e551e04b 2012-05-19 kinaba: int PrevDif = dp(i-1,k-1).first; f6e551e04b 2012-05-19 kinaba: int PrevSum = dp(i-1,k-1).second; f6e551e04b 2012-05-19 kinaba: pair<int,int> next(PrevDif+DifIncr, PrevSum+SumIncr); f6e551e04b 2012-05-19 kinaba: if( dp(i,k) < next ) f6e551e04b 2012-05-19 kinaba: dp(i,k) = next; f6e551e04b 2012-05-19 kinaba: } f6e551e04b 2012-05-19 kinaba: } f6e551e04b 2012-05-19 kinaba: } f6e551e04b 2012-05-19 kinaba: f6e551e04b 2012-05-19 kinaba: int WminusT = dp(books.size()-1, K).first; f6e551e04b 2012-05-19 kinaba: int WplusT = dp(books.size()-1, K).second; f6e551e04b 2012-05-19 kinaba: vector<int> answer; f6e551e04b 2012-05-19 kinaba: answer.push_back( (WplusT - WminusT)/2 ); f6e551e04b 2012-05-19 kinaba: answer.push_back( (WplusT + WminusT)/2 ); f6e551e04b 2012-05-19 kinaba: return answer; f6e551e04b 2012-05-19 kinaba: } f6e551e04b 2012-05-19 kinaba: }; f6e551e04b 2012-05-19 kinaba: f6e551e04b 2012-05-19 kinaba: // BEGIN CUT HERE f6e551e04b 2012-05-19 kinaba: #include <ctime> f6e551e04b 2012-05-19 kinaba: double start_time; string timer() f6e551e04b 2012-05-19 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } f6e551e04b 2012-05-19 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) f6e551e04b 2012-05-19 kinaba: { os << "{ "; f6e551e04b 2012-05-19 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) f6e551e04b 2012-05-19 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } f6e551e04b 2012-05-19 kinaba: void verify_case(const vector <int>& Expected, const vector <int>& Received) { f6e551e04b 2012-05-19 kinaba: bool ok = (Expected == Received); f6e551e04b 2012-05-19 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; f6e551e04b 2012-05-19 kinaba: cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } f6e551e04b 2012-05-19 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); f6e551e04b 2012-05-19 kinaba: #define END verify_case(_, HeavyBooks().findWeight(books, moves));} f6e551e04b 2012-05-19 kinaba: int main(){ f6e551e04b 2012-05-19 kinaba: f6e551e04b 2012-05-19 kinaba: CASE(0) f6e551e04b 2012-05-19 kinaba: int books_[] = {5,2,3,1}; f6e551e04b 2012-05-19 kinaba: vector <int> books(books_, books_+sizeof(books_)/sizeof(*books_)); f6e551e04b 2012-05-19 kinaba: int moves_[] = {3,2,1,1,1}; f6e551e04b 2012-05-19 kinaba: vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); f6e551e04b 2012-05-19 kinaba: int __[] = {3, 7 }; f6e551e04b 2012-05-19 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); f6e551e04b 2012-05-19 kinaba: END f6e551e04b 2012-05-19 kinaba: CASE(1) f6e551e04b 2012-05-19 kinaba: int books_[] = {10, 100, 1000}; f6e551e04b 2012-05-19 kinaba: vector <int> books(books_, books_+sizeof(books_)/sizeof(*books_)); f6e551e04b 2012-05-19 kinaba: int moves_[] = {2, 3}; f6e551e04b 2012-05-19 kinaba: vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); f6e551e04b 2012-05-19 kinaba: int __[] = {110, 0 }; f6e551e04b 2012-05-19 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); f6e551e04b 2012-05-19 kinaba: END f6e551e04b 2012-05-19 kinaba: CASE(2) f6e551e04b 2012-05-19 kinaba: int books_[] = {155870,565381}; f6e551e04b 2012-05-19 kinaba: vector <int> books(books_, books_+sizeof(books_)/sizeof(*books_)); f6e551e04b 2012-05-19 kinaba: int moves_[] = {1,1,1}; f6e551e04b 2012-05-19 kinaba: vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); f6e551e04b 2012-05-19 kinaba: int __[] = {0, 565381 }; f6e551e04b 2012-05-19 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); f6e551e04b 2012-05-19 kinaba: END f6e551e04b 2012-05-19 kinaba: CASE(3) f6e551e04b 2012-05-19 kinaba: int books_[] = {500,500,500,500}; f6e551e04b 2012-05-19 kinaba: vector <int> books(books_, books_+sizeof(books_)/sizeof(*books_)); f6e551e04b 2012-05-19 kinaba: int moves_[] = {4,1,1,3,2}; f6e551e04b 2012-05-19 kinaba: vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); f6e551e04b 2012-05-19 kinaba: int __[] = {500, 1500 }; f6e551e04b 2012-05-19 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); f6e551e04b 2012-05-19 kinaba: END f6e551e04b 2012-05-19 kinaba: CASE(4) f6e551e04b 2012-05-19 kinaba: int books_[] = {1,1,1,1,1000000}; f6e551e04b 2012-05-19 kinaba: vector <int> books(books_, books_+sizeof(books_)/sizeof(*books_)); f6e551e04b 2012-05-19 kinaba: int moves_[] = {1}; f6e551e04b 2012-05-19 kinaba: vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); f6e551e04b 2012-05-19 kinaba: int __[] = {0, 1000000 }; f6e551e04b 2012-05-19 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); f6e551e04b 2012-05-19 kinaba: END f6e551e04b 2012-05-19 kinaba: CASE(5) f6e551e04b 2012-05-19 kinaba: int books_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; f6e551e04b 2012-05-19 kinaba: vector <int> books(books_, books_+sizeof(books_)/sizeof(*books_)); f6e551e04b 2012-05-19 kinaba: int moves_[] = {20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1}; f6e551e04b 2012-05-19 kinaba: vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); f6e551e04b 2012-05-19 kinaba: int __[] = {110, 100 }; f6e551e04b 2012-05-19 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); f6e551e04b 2012-05-19 kinaba: END f6e551e04b 2012-05-19 kinaba: /* f6e551e04b 2012-05-19 kinaba: CASE(6) f6e551e04b 2012-05-19 kinaba: int books_[] = ; f6e551e04b 2012-05-19 kinaba: vector <int> books(books_, books_+sizeof(books_)/sizeof(*books_)); f6e551e04b 2012-05-19 kinaba: int moves_[] = ; f6e551e04b 2012-05-19 kinaba: vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); f6e551e04b 2012-05-19 kinaba: int __[] = ; f6e551e04b 2012-05-19 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); f6e551e04b 2012-05-19 kinaba: END f6e551e04b 2012-05-19 kinaba: CASE(7) f6e551e04b 2012-05-19 kinaba: int books_[] = ; f6e551e04b 2012-05-19 kinaba: vector <int> books(books_, books_+sizeof(books_)/sizeof(*books_)); f6e551e04b 2012-05-19 kinaba: int moves_[] = ; f6e551e04b 2012-05-19 kinaba: vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); f6e551e04b 2012-05-19 kinaba: int __[] = ; f6e551e04b 2012-05-19 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); f6e551e04b 2012-05-19 kinaba: END f6e551e04b 2012-05-19 kinaba: */ f6e551e04b 2012-05-19 kinaba: } f6e551e04b 2012-05-19 kinaba: // END CUT HERE