fc5b59158c 2014-03-04 kinaba: #include <iostream> fc5b59158c 2014-03-04 kinaba: #include <sstream> fc5b59158c 2014-03-04 kinaba: #include <iomanip> fc5b59158c 2014-03-04 kinaba: #include <vector> fc5b59158c 2014-03-04 kinaba: #include <string> fc5b59158c 2014-03-04 kinaba: #include <map> fc5b59158c 2014-03-04 kinaba: #include <set> fc5b59158c 2014-03-04 kinaba: #include <algorithm> fc5b59158c 2014-03-04 kinaba: #include <numeric> fc5b59158c 2014-03-04 kinaba: #include <iterator> fc5b59158c 2014-03-04 kinaba: #include <functional> fc5b59158c 2014-03-04 kinaba: #include <complex> fc5b59158c 2014-03-04 kinaba: #include <queue> fc5b59158c 2014-03-04 kinaba: #include <stack> fc5b59158c 2014-03-04 kinaba: #include <cmath> fc5b59158c 2014-03-04 kinaba: #include <cassert> fc5b59158c 2014-03-04 kinaba: #include <tuple> fc5b59158c 2014-03-04 kinaba: using namespace std; fc5b59158c 2014-03-04 kinaba: typedef long long LL; fc5b59158c 2014-03-04 kinaba: typedef complex<double> CMP; fc5b59158c 2014-03-04 kinaba: fc5b59158c 2014-03-04 kinaba: class MiningGoldHard { public: fc5b59158c 2014-03-04 kinaba: int GetMaximumGold(int N, int M, vector <int> event_i, vector <int> event_j, vector <int> event_di, vector <int> event_dj) fc5b59158c 2014-03-04 kinaba: { fc5b59158c 2014-03-04 kinaba: return solve(N, event_i, event_di) + solve(M, event_j, event_dj); fc5b59158c 2014-03-04 kinaba: } fc5b59158c 2014-03-04 kinaba: fc5b59158c 2014-03-04 kinaba: int solve(int W, const vector<int>& e, const vector<int>& d) fc5b59158c 2014-03-04 kinaba: { fc5b59158c 2014-03-04 kinaba: vector<int> dp(W+1, 0); fc5b59158c 2014-03-04 kinaba: for(int x=0; x<=W; ++x) fc5b59158c 2014-03-04 kinaba: dp[x] = W - abs(x-e[0]); fc5b59158c 2014-03-04 kinaba: fc5b59158c 2014-03-04 kinaba: for(int i=0; i<d.size(); ++i) { fc5b59158c 2014-03-04 kinaba: vector<int> dp2(W+1, 0); fc5b59158c 2014-03-04 kinaba: for(int x=0; x<=W; ++x) fc5b59158c 2014-03-04 kinaba: dp2[x] = W - abs(x-e[i+1]); fc5b59158c 2014-03-04 kinaba: fc5b59158c 2014-03-04 kinaba: deque<int> q; fc5b59158c 2014-03-04 kinaba: int px = 0; fc5b59158c 2014-03-04 kinaba: for(px=0; px<=d[i] && px<=W; ++px) { fc5b59158c 2014-03-04 kinaba: while(!q.empty() && dp[q.back()] < dp[px]) fc5b59158c 2014-03-04 kinaba: q.pop_back(); fc5b59158c 2014-03-04 kinaba: q.push_back(px); fc5b59158c 2014-03-04 kinaba: } fc5b59158c 2014-03-04 kinaba: fc5b59158c 2014-03-04 kinaba: for(int x=0; x<=W; ++x) { fc5b59158c 2014-03-04 kinaba: while(q.front() < x-d[i]) fc5b59158c 2014-03-04 kinaba: q.pop_front(); fc5b59158c 2014-03-04 kinaba: fc5b59158c 2014-03-04 kinaba: dp2[x] += dp[q.front()]; fc5b59158c 2014-03-04 kinaba: fc5b59158c 2014-03-04 kinaba: if(px<=W) { fc5b59158c 2014-03-04 kinaba: while(!q.empty() && dp[q.back()] < dp[px]) fc5b59158c 2014-03-04 kinaba: q.pop_back(); fc5b59158c 2014-03-04 kinaba: q.push_back(px); fc5b59158c 2014-03-04 kinaba: fc5b59158c 2014-03-04 kinaba: ++px; fc5b59158c 2014-03-04 kinaba: } fc5b59158c 2014-03-04 kinaba: } fc5b59158c 2014-03-04 kinaba: fc5b59158c 2014-03-04 kinaba: dp.swap(dp2); fc5b59158c 2014-03-04 kinaba: } fc5b59158c 2014-03-04 kinaba: return *max_element(dp.begin(), dp.end()); fc5b59158c 2014-03-04 kinaba: } fc5b59158c 2014-03-04 kinaba: }; fc5b59158c 2014-03-04 kinaba: fc5b59158c 2014-03-04 kinaba: // BEGIN CUT HERE fc5b59158c 2014-03-04 kinaba: #include <ctime> fc5b59158c 2014-03-04 kinaba: double start_time; string timer() fc5b59158c 2014-03-04 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } fc5b59158c 2014-03-04 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) fc5b59158c 2014-03-04 kinaba: { os << "{ "; fc5b59158c 2014-03-04 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) fc5b59158c 2014-03-04 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } fc5b59158c 2014-03-04 kinaba: void verify_case(const int& Expected, const int& Received) { fc5b59158c 2014-03-04 kinaba: bool ok = (Expected == Received); fc5b59158c 2014-03-04 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; fc5b59158c 2014-03-04 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } fc5b59158c 2014-03-04 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); fc5b59158c 2014-03-04 kinaba: #define END verify_case(_, MiningGoldHard().GetMaximumGold(N, M, event_i, event_j, event_di, event_dj));} fc5b59158c 2014-03-04 kinaba: int main(){ fc5b59158c 2014-03-04 kinaba: fc5b59158c 2014-03-04 kinaba: CASE(0) fc5b59158c 2014-03-04 kinaba: int N = 3; fc5b59158c 2014-03-04 kinaba: int M = 3; fc5b59158c 2014-03-04 kinaba: int event_i_[] = {1}; fc5b59158c 2014-03-04 kinaba: vector <int> event_i(event_i_, event_i_+sizeof(event_i_)/sizeof(*event_i_)); fc5b59158c 2014-03-04 kinaba: int event_j_[] = {1}; fc5b59158c 2014-03-04 kinaba: vector <int> event_j(event_j_, event_j_+sizeof(event_j_)/sizeof(*event_j_)); fc5b59158c 2014-03-04 kinaba: vector <int> event_di; fc5b59158c 2014-03-04 kinaba: vector <int> event_dj; fc5b59158c 2014-03-04 kinaba: int _ = 6; fc5b59158c 2014-03-04 kinaba: END fc5b59158c 2014-03-04 kinaba: CASE(1) fc5b59158c 2014-03-04 kinaba: int N = 3; fc5b59158c 2014-03-04 kinaba: int M = 3; fc5b59158c 2014-03-04 kinaba: int event_i_[] = {0, 2}; fc5b59158c 2014-03-04 kinaba: vector <int> event_i(event_i_, event_i_+sizeof(event_i_)/sizeof(*event_i_)); fc5b59158c 2014-03-04 kinaba: int event_j_[] = {0, 2}; fc5b59158c 2014-03-04 kinaba: vector <int> event_j(event_j_, event_j_+sizeof(event_j_)/sizeof(*event_j_)); fc5b59158c 2014-03-04 kinaba: int event_di_[] = {1}; fc5b59158c 2014-03-04 kinaba: vector <int> event_di(event_di_, event_di_+sizeof(event_di_)/sizeof(*event_di_)); fc5b59158c 2014-03-04 kinaba: int event_dj_[] = {1}; fc5b59158c 2014-03-04 kinaba: vector <int> event_dj(event_dj_, event_dj_+sizeof(event_dj_)/sizeof(*event_dj_)); fc5b59158c 2014-03-04 kinaba: int _ = 10; fc5b59158c 2014-03-04 kinaba: END fc5b59158c 2014-03-04 kinaba: CASE(2) fc5b59158c 2014-03-04 kinaba: int N = 4; fc5b59158c 2014-03-04 kinaba: int M = 2; fc5b59158c 2014-03-04 kinaba: int event_i_[] = {1, 4, 4}; fc5b59158c 2014-03-04 kinaba: vector <int> event_i(event_i_, event_i_+sizeof(event_i_)/sizeof(*event_i_)); fc5b59158c 2014-03-04 kinaba: int event_j_[] = {1, 2, 0}; fc5b59158c 2014-03-04 kinaba: vector <int> event_j(event_j_, event_j_+sizeof(event_j_)/sizeof(*event_j_)); fc5b59158c 2014-03-04 kinaba: int event_di_[] = {1, 1}; fc5b59158c 2014-03-04 kinaba: vector <int> event_di(event_di_, event_di_+sizeof(event_di_)/sizeof(*event_di_)); fc5b59158c 2014-03-04 kinaba: int event_dj_[] = {1, 1}; fc5b59158c 2014-03-04 kinaba: vector <int> event_dj(event_dj_, event_dj_+sizeof(event_dj_)/sizeof(*event_dj_)); fc5b59158c 2014-03-04 kinaba: int _ = 15; fc5b59158c 2014-03-04 kinaba: END fc5b59158c 2014-03-04 kinaba: CASE(3) fc5b59158c 2014-03-04 kinaba: int N = 6; fc5b59158c 2014-03-04 kinaba: int M = 6; fc5b59158c 2014-03-04 kinaba: int event_i_[] = {0, 2, 1, 5, 6, 4}; fc5b59158c 2014-03-04 kinaba: vector <int> event_i(event_i_, event_i_+sizeof(event_i_)/sizeof(*event_i_)); fc5b59158c 2014-03-04 kinaba: int event_j_[] = {4, 3, 1, 6, 2, 0}; fc5b59158c 2014-03-04 kinaba: vector <int> event_j(event_j_, event_j_+sizeof(event_j_)/sizeof(*event_j_)); fc5b59158c 2014-03-04 kinaba: int event_di_[] = {2, 3, 1, 5, 6}; fc5b59158c 2014-03-04 kinaba: vector <int> event_di(event_di_, event_di_+sizeof(event_di_)/sizeof(*event_di_)); fc5b59158c 2014-03-04 kinaba: int event_dj_[] = {2, 4, 0, 5, 1}; fc5b59158c 2014-03-04 kinaba: vector <int> event_dj(event_dj_, event_dj_+sizeof(event_dj_)/sizeof(*event_dj_)); fc5b59158c 2014-03-04 kinaba: int _ = 63; fc5b59158c 2014-03-04 kinaba: END fc5b59158c 2014-03-04 kinaba: CASE(4) fc5b59158c 2014-03-04 kinaba: int N = 72; fc5b59158c 2014-03-04 kinaba: int M = 90; fc5b59158c 2014-03-04 kinaba: int event_i_[] = {9, 9, 42, 64, 37, 4, 67, 65, 20, 18, 25, 45, 19, 57, 34, 29, 20, 43, 17, 46, 61, 1, 18, 53, 54, 23, 9, 69, 57, 44, 34, 50, 37, 4, 26, 1, 8, 4, 66}; fc5b59158c 2014-03-04 kinaba: vector <int> event_i(event_i_, event_i_+sizeof(event_i_)/sizeof(*event_i_)); fc5b59158c 2014-03-04 kinaba: int event_j_[] = {37, 47, 48, 69, 56, 22, 40, 52, 43, 46, 64, 24, 48, 54, 54, 56, 32, 77, 50, 8, 7, 90, 55, 34, 40, 89, 57, 44, 21, 59, 89, 21, 69, 46, 0, 89, 31, 3, 50}; fc5b59158c 2014-03-04 kinaba: vector <int> event_j(event_j_, event_j_+sizeof(event_j_)/sizeof(*event_j_)); fc5b59158c 2014-03-04 kinaba: int event_di_[] = {56, 5, 21, 5, 22, 5, 45, 4, 44, 20, 68, 63, 37, 14, 43, 31, 70, 28, 51, 38, 20, 59, 72, 66, 16, 20, 39, 72, 11, 71, 21, 51, 60, 42, 40, 10, 32, 70}; fc5b59158c 2014-03-04 kinaba: vector <int> event_di(event_di_, event_di_+sizeof(event_di_)/sizeof(*event_di_)); fc5b59158c 2014-03-04 kinaba: int event_dj_[] = {77, 73, 42, 80, 43, 24, 81, 68, 40, 86, 1, 76, 43, 10, 43, 53, 40, 26, 18, 70, 60, 68, 29, 17, 66, 2, 87, 71, 90, 33, 11, 76, 69, 17, 65, 21, 4, 19}; fc5b59158c 2014-03-04 kinaba: vector <int> event_dj(event_dj_, event_dj_+sizeof(event_dj_)/sizeof(*event_dj_)); fc5b59158c 2014-03-04 kinaba: int _ = 5810; fc5b59158c 2014-03-04 kinaba: END fc5b59158c 2014-03-04 kinaba: CASE(5) fc5b59158c 2014-03-04 kinaba: int N = 1000000; fc5b59158c 2014-03-04 kinaba: int M = 1000000; fc5b59158c 2014-03-04 kinaba: int event_i_[] = {9, 9, 42, 64, 37, 4, 67, 65, 20, 18, 25, 45, 19, 57, 34, 29, 20, 43, 17, 46, 61, 1, 18, 53, 54, 23, 9, 69, 57, 44, 34, 50, 37, 4, 26, 1, 8, 4, 66}; fc5b59158c 2014-03-04 kinaba: vector <int> event_i(event_i_, event_i_+sizeof(event_i_)/sizeof(*event_i_)); fc5b59158c 2014-03-04 kinaba: int event_j_[] = {37, 47, 48, 69, 56, 22, 40, 52, 43, 46, 64, 24, 48, 54, 54, 56, 32, 77, 50, 8, 7, 90, 55, 34, 40, 89, 57, 44, 21, 59, 89, 21, 69, 46, 0, 89, 31, 3, 50}; fc5b59158c 2014-03-04 kinaba: vector <int> event_j(event_j_, event_j_+sizeof(event_j_)/sizeof(*event_j_)); fc5b59158c 2014-03-04 kinaba: int event_di_[] = {56, 5, 21, 5, 22, 5, 45, 4, 44, 20, 68, 63, 37, 14, 43, 31, 70, 28, 51, 38, 20, 59, 72, 66, 16, 20, 39, 72, 11, 71, 21, 51, 60, 42, 40, 10, 32, 70}; fc5b59158c 2014-03-04 kinaba: vector <int> event_di(event_di_, event_di_+sizeof(event_di_)/sizeof(*event_di_)); fc5b59158c 2014-03-04 kinaba: int event_dj_[] = {77, 73, 42, 80, 43, 24, 81, 68, 40, 86, 1, 76, 43, 10, 43, 53, 40, 26, 18, 70, 60, 68, 29, 17, 66, 2, 87, 71, 90, 33, 11, 76, 69, 17, 65, 21, 4, 19}; fc5b59158c 2014-03-04 kinaba: vector <int> event_dj(event_dj_, event_dj_+sizeof(event_dj_)/sizeof(*event_dj_)); fc5b59158c 2014-03-04 kinaba: int _ = -1; fc5b59158c 2014-03-04 kinaba: END fc5b59158c 2014-03-04 kinaba: /* fc5b59158c 2014-03-04 kinaba: CASE(6) fc5b59158c 2014-03-04 kinaba: int N = 1000000; fc5b59158c 2014-03-04 kinaba: int M = 1000000; fc5b59158c 2014-03-04 kinaba: int event_i_[] = ; fc5b59158c 2014-03-04 kinaba: vector <int> event_i(event_i_, event_i_+sizeof(event_i_)/sizeof(*event_i_)); fc5b59158c 2014-03-04 kinaba: int event_j_[] = ; fc5b59158c 2014-03-04 kinaba: vector <int> event_j(event_j_, event_j_+sizeof(event_j_)/sizeof(*event_j_)); fc5b59158c 2014-03-04 kinaba: int event_di_[] = ; fc5b59158c 2014-03-04 kinaba: vector <int> event_di(event_di_, event_di_+sizeof(event_di_)/sizeof(*event_di_)); fc5b59158c 2014-03-04 kinaba: int event_dj_[] = ; fc5b59158c 2014-03-04 kinaba: vector <int> event_dj(event_dj_, event_dj_+sizeof(event_dj_)/sizeof(*event_dj_)); fc5b59158c 2014-03-04 kinaba: int _ = ; fc5b59158c 2014-03-04 kinaba: END fc5b59158c 2014-03-04 kinaba: */ fc5b59158c 2014-03-04 kinaba: } fc5b59158c 2014-03-04 kinaba: // END CUT HERE