6cfba8c225 2015-12-10 kinaba: #include <iostream> 6cfba8c225 2015-12-10 kinaba: #include <sstream> 6cfba8c225 2015-12-10 kinaba: #include <iomanip> 6cfba8c225 2015-12-10 kinaba: #include <vector> 6cfba8c225 2015-12-10 kinaba: #include <string> 6cfba8c225 2015-12-10 kinaba: #include <map> 6cfba8c225 2015-12-10 kinaba: #include <set> 6cfba8c225 2015-12-10 kinaba: #include <algorithm> 6cfba8c225 2015-12-10 kinaba: #include <numeric> 6cfba8c225 2015-12-10 kinaba: #include <iterator> 6cfba8c225 2015-12-10 kinaba: #include <functional> 6cfba8c225 2015-12-10 kinaba: #include <complex> 6cfba8c225 2015-12-10 kinaba: #include <queue> 6cfba8c225 2015-12-10 kinaba: #include <stack> 6cfba8c225 2015-12-10 kinaba: #include <cmath> 6cfba8c225 2015-12-10 kinaba: #include <cassert> 6cfba8c225 2015-12-10 kinaba: #include <tuple> 6cfba8c225 2015-12-10 kinaba: using namespace std; 6cfba8c225 2015-12-10 kinaba: typedef long long LL; 6cfba8c225 2015-12-10 kinaba: typedef complex<double> CMP; 6cfba8c225 2015-12-10 kinaba: 6cfba8c225 2015-12-10 kinaba: class ClassicProblem { public: 6cfba8c225 2015-12-10 kinaba: long long maximalValue(vector <int> cnt, vector <int> w, vector <int> v, int limit) 6cfba8c225 2015-12-10 kinaba: { 6cfba8c225 2015-12-10 kinaba: vector<int> idx; 6cfba8c225 2015-12-10 kinaba: for(int i=0; i<cnt.size(); ++i) 6cfba8c225 2015-12-10 kinaba: idx.push_back(i); 6cfba8c225 2015-12-10 kinaba: sort(idx.begin(), idx.end(), [&](int a, int b) { 6cfba8c225 2015-12-10 kinaba: // sort by increasing order of v[a]/w[a] 6cfba8c225 2015-12-10 kinaba: if(LL(v[a])*w[b] == LL(v[b])*w[a]) 6cfba8c225 2015-12-10 kinaba: return w[a]>w[b]; 6cfba8c225 2015-12-10 kinaba: return LL(v[a])*w[b] < LL(v[b])*w[a]; 6cfba8c225 2015-12-10 kinaba: }); 6cfba8c225 2015-12-10 kinaba: 6cfba8c225 2015-12-10 kinaba: LL gain = 0; 6cfba8c225 2015-12-10 kinaba: while(limit > 6480) { 6cfba8c225 2015-12-10 kinaba: if(idx.empty()) 6cfba8c225 2015-12-10 kinaba: break; 6cfba8c225 2015-12-10 kinaba: int i = idx.back(), ww=w[i], vv=v[i]; 6cfba8c225 2015-12-10 kinaba: int n = min(cnt[i], (limit-6400)/ww); 6cfba8c225 2015-12-10 kinaba: limit -= LL(ww)*n; 6cfba8c225 2015-12-10 kinaba: gain += LL(vv)*n; 6cfba8c225 2015-12-10 kinaba: cnt[i] -= n; 6cfba8c225 2015-12-10 kinaba: idx.pop_back(); 6cfba8c225 2015-12-10 kinaba: } 6cfba8c225 2015-12-10 kinaba: 6cfba8c225 2015-12-10 kinaba: vector<LL> w2v = rec(0, cnt, w, v, limit); 6cfba8c225 2015-12-10 kinaba: return gain + *max_element(w2v.begin(), w2v.end()); 6cfba8c225 2015-12-10 kinaba: } 6cfba8c225 2015-12-10 kinaba: 6cfba8c225 2015-12-10 kinaba: vector<LL> rec(int i, const vector <int>& C, const vector<int>& W, const vector<int>& V, int limit) 6cfba8c225 2015-12-10 kinaba: { 6cfba8c225 2015-12-10 kinaba: if(i == C.size()) 6cfba8c225 2015-12-10 kinaba: return vector<LL>(1, 0LL); 6cfba8c225 2015-12-10 kinaba: 6cfba8c225 2015-12-10 kinaba: vector<LL> w2v = rec(i+1, C, W, V, limit); 6cfba8c225 2015-12-10 kinaba: if(C[i] == 0) 6cfba8c225 2015-12-10 kinaba: return w2v; 6cfba8c225 2015-12-10 kinaba: 6cfba8c225 2015-12-10 kinaba: vector<LL> w2v_neo(limit+1, 0LL); 6cfba8c225 2015-12-10 kinaba: for(int w=0; w<w2v.size(); ++w) { 6cfba8c225 2015-12-10 kinaba: for(int k=0; k<=C[i]; ++k) { // this type of typical DP can be faster 6cfba8c225 2015-12-10 kinaba: int ww = w + LL(W[i])*k; 6cfba8c225 2015-12-10 kinaba: if(ww > limit) 6cfba8c225 2015-12-10 kinaba: break; 6cfba8c225 2015-12-10 kinaba: w2v_neo[ww] = max(w2v_neo[ww], w2v[w] + LL(V[i])*k); 6cfba8c225 2015-12-10 kinaba: } 6cfba8c225 2015-12-10 kinaba: } 6cfba8c225 2015-12-10 kinaba: return w2v_neo; 6cfba8c225 2015-12-10 kinaba: } 6cfba8c225 2015-12-10 kinaba: }; 6cfba8c225 2015-12-10 kinaba: 6cfba8c225 2015-12-10 kinaba: // BEGIN CUT HERE 6cfba8c225 2015-12-10 kinaba: #include <ctime> 6cfba8c225 2015-12-10 kinaba: double start_time; string timer() 6cfba8c225 2015-12-10 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 6cfba8c225 2015-12-10 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 6cfba8c225 2015-12-10 kinaba: { os << "{ "; 6cfba8c225 2015-12-10 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 6cfba8c225 2015-12-10 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 6cfba8c225 2015-12-10 kinaba: void verify_case(const long long& Expected, const long long& Received) { 6cfba8c225 2015-12-10 kinaba: bool ok = (Expected == Received); 6cfba8c225 2015-12-10 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 6cfba8c225 2015-12-10 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 6cfba8c225 2015-12-10 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 6cfba8c225 2015-12-10 kinaba: #define END verify_case(_, ClassicProblem().maximalValue(cnt, w, v, limit));} 6cfba8c225 2015-12-10 kinaba: int main(){ 6cfba8c225 2015-12-10 kinaba: 6cfba8c225 2015-12-10 kinaba: CASE(0) 6cfba8c225 2015-12-10 kinaba: int cnt_[] = {100,100}; 6cfba8c225 2015-12-10 kinaba: vector <int> cnt(cnt_, cnt_+sizeof(cnt_)/sizeof(*cnt_)); 6cfba8c225 2015-12-10 kinaba: int w_[] = {2,3}; 6cfba8c225 2015-12-10 kinaba: vector <int> w(w_, w_+sizeof(w_)/sizeof(*w_)); 6cfba8c225 2015-12-10 kinaba: int v_[] = {3,5}; 6cfba8c225 2015-12-10 kinaba: vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 6cfba8c225 2015-12-10 kinaba: int limit = 6; 6cfba8c225 2015-12-10 kinaba: long long _ = 10LL; 6cfba8c225 2015-12-10 kinaba: END 6cfba8c225 2015-12-10 kinaba: CASE(1) 6cfba8c225 2015-12-10 kinaba: int cnt_[] = {100,100}; 6cfba8c225 2015-12-10 kinaba: vector <int> cnt(cnt_, cnt_+sizeof(cnt_)/sizeof(*cnt_)); 6cfba8c225 2015-12-10 kinaba: int w_[] = {2,3}; 6cfba8c225 2015-12-10 kinaba: vector <int> w(w_, w_+sizeof(w_)/sizeof(*w_)); 6cfba8c225 2015-12-10 kinaba: int v_[] = {3,5}; 6cfba8c225 2015-12-10 kinaba: vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 6cfba8c225 2015-12-10 kinaba: int limit = 5; 6cfba8c225 2015-12-10 kinaba: long long _ = 8LL; 6cfba8c225 2015-12-10 kinaba: END 6cfba8c225 2015-12-10 kinaba: CASE(2) 6cfba8c225 2015-12-10 kinaba: int cnt_[] = {100,102}; 6cfba8c225 2015-12-10 kinaba: vector <int> cnt(cnt_, cnt_+sizeof(cnt_)/sizeof(*cnt_)); 6cfba8c225 2015-12-10 kinaba: int w_[] = {2,3}; 6cfba8c225 2015-12-10 kinaba: vector <int> w(w_, w_+sizeof(w_)/sizeof(*w_)); 6cfba8c225 2015-12-10 kinaba: int v_[] = {3,5}; 6cfba8c225 2015-12-10 kinaba: vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 6cfba8c225 2015-12-10 kinaba: int limit = 1000000000; 6cfba8c225 2015-12-10 kinaba: long long _ = 810LL; 6cfba8c225 2015-12-10 kinaba: END 6cfba8c225 2015-12-10 kinaba: CASE(3) 6cfba8c225 2015-12-10 kinaba: int cnt_[] = {100,100}; 6cfba8c225 2015-12-10 kinaba: vector <int> cnt(cnt_, cnt_+sizeof(cnt_)/sizeof(*cnt_)); 6cfba8c225 2015-12-10 kinaba: int w_[] = {2,3}; 6cfba8c225 2015-12-10 kinaba: vector <int> w(w_, w_+sizeof(w_)/sizeof(*w_)); 6cfba8c225 2015-12-10 kinaba: int v_[] = {3,5}; 6cfba8c225 2015-12-10 kinaba: vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 6cfba8c225 2015-12-10 kinaba: int limit = 1; 6cfba8c225 2015-12-10 kinaba: long long _ = 0LL; 6cfba8c225 2015-12-10 kinaba: END 6cfba8c225 2015-12-10 kinaba: CASE(4) 6cfba8c225 2015-12-10 kinaba: int cnt_[] = {1,2,3,4,5,6,7,8}; 6cfba8c225 2015-12-10 kinaba: vector <int> cnt(cnt_, cnt_+sizeof(cnt_)/sizeof(*cnt_)); 6cfba8c225 2015-12-10 kinaba: int w_[] = {4,2,6,7,5,8,3,1}; 6cfba8c225 2015-12-10 kinaba: vector <int> w(w_, w_+sizeof(w_)/sizeof(*w_)); 6cfba8c225 2015-12-10 kinaba: int v_[] = {3,6,4,1,2,8,5,7}; 6cfba8c225 2015-12-10 kinaba: vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 6cfba8c225 2015-12-10 kinaba: int limit = 15; 6cfba8c225 2015-12-10 kinaba: long long _ = 73LL; 6cfba8c225 2015-12-10 kinaba: END 6cfba8c225 2015-12-10 kinaba: CASE(5) 6cfba8c225 2015-12-10 kinaba: int cnt_[] = {1000000000}; 6cfba8c225 2015-12-10 kinaba: vector <int> cnt(cnt_, cnt_+sizeof(cnt_)/sizeof(*cnt_)); 6cfba8c225 2015-12-10 kinaba: int w_[] = {1}; 6cfba8c225 2015-12-10 kinaba: vector <int> w(w_, w_+sizeof(w_)/sizeof(*w_)); 6cfba8c225 2015-12-10 kinaba: int v_[] = {1000000000}; 6cfba8c225 2015-12-10 kinaba: vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 6cfba8c225 2015-12-10 kinaba: int limit = 1000000000; 6cfba8c225 2015-12-10 kinaba: long long _ = 1000000000000000000LL; 6cfba8c225 2015-12-10 kinaba: END 6cfba8c225 2015-12-10 kinaba: /* 6cfba8c225 2015-12-10 kinaba: CASE(6) 6cfba8c225 2015-12-10 kinaba: int cnt_[] = ; 6cfba8c225 2015-12-10 kinaba: vector <int> cnt(cnt_, cnt_+sizeof(cnt_)/sizeof(*cnt_)); 6cfba8c225 2015-12-10 kinaba: int w_[] = ; 6cfba8c225 2015-12-10 kinaba: vector <int> w(w_, w_+sizeof(w_)/sizeof(*w_)); 6cfba8c225 2015-12-10 kinaba: int v_[] = ; 6cfba8c225 2015-12-10 kinaba: vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 6cfba8c225 2015-12-10 kinaba: int limit = ; 6cfba8c225 2015-12-10 kinaba: long long _ = LL; 6cfba8c225 2015-12-10 kinaba: END 6cfba8c225 2015-12-10 kinaba: CASE(7) 6cfba8c225 2015-12-10 kinaba: int cnt_[] = ; 6cfba8c225 2015-12-10 kinaba: vector <int> cnt(cnt_, cnt_+sizeof(cnt_)/sizeof(*cnt_)); 6cfba8c225 2015-12-10 kinaba: int w_[] = ; 6cfba8c225 2015-12-10 kinaba: vector <int> w(w_, w_+sizeof(w_)/sizeof(*w_)); 6cfba8c225 2015-12-10 kinaba: int v_[] = ; 6cfba8c225 2015-12-10 kinaba: vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 6cfba8c225 2015-12-10 kinaba: int limit = ; 6cfba8c225 2015-12-10 kinaba: long long _ = LL; 6cfba8c225 2015-12-10 kinaba: END 6cfba8c225 2015-12-10 kinaba: */ 6cfba8c225 2015-12-10 kinaba: } 6cfba8c225 2015-12-10 kinaba: // END CUT HERE