aae12c19ff 2015-07-23 kinaba: #include <iostream> aae12c19ff 2015-07-23 kinaba: #include <sstream> aae12c19ff 2015-07-23 kinaba: #include <iomanip> aae12c19ff 2015-07-23 kinaba: #include <vector> aae12c19ff 2015-07-23 kinaba: #include <string> aae12c19ff 2015-07-23 kinaba: #include <map> aae12c19ff 2015-07-23 kinaba: #include <set> aae12c19ff 2015-07-23 kinaba: #include <algorithm> aae12c19ff 2015-07-23 kinaba: #include <numeric> aae12c19ff 2015-07-23 kinaba: #include <iterator> aae12c19ff 2015-07-23 kinaba: #include <functional> aae12c19ff 2015-07-23 kinaba: #include <complex> aae12c19ff 2015-07-23 kinaba: #include <queue> aae12c19ff 2015-07-23 kinaba: #include <stack> aae12c19ff 2015-07-23 kinaba: #include <cmath> aae12c19ff 2015-07-23 kinaba: #include <cassert> aae12c19ff 2015-07-23 kinaba: #include <tuple> aae12c19ff 2015-07-23 kinaba: using namespace std; aae12c19ff 2015-07-23 kinaba: typedef long long LL; aae12c19ff 2015-07-23 kinaba: typedef complex<double> CMP; aae12c19ff 2015-07-23 kinaba: aae12c19ff 2015-07-23 kinaba: class YetAnotherCardGame { public: aae12c19ff 2015-07-23 kinaba: int maxCards(vector <int> petr, vector <int> snuke) aae12c19ff 2015-07-23 kinaba: { aae12c19ff 2015-07-23 kinaba: sort(petr.begin(), petr.end()); aae12c19ff 2015-07-23 kinaba: sort(snuke.begin(), snuke.end()); aae12c19ff 2015-07-23 kinaba: aae12c19ff 2015-07-23 kinaba: return rec(&petr, &snuke, 0, 0, 0, 0, 0); aae12c19ff 2015-07-23 kinaba: } aae12c19ff 2015-07-23 kinaba: aae12c19ff 2015-07-23 kinaba: map<tuple<int,int,int,int,int>,int> memo; aae12c19ff 2015-07-23 kinaba: int rec(const vector<int>* me, const vector<int>* you, aae12c19ff 2015-07-23 kinaba: int turn, int m_next, int y_next, int m_musteat, int y_musteat) aae12c19ff 2015-07-23 kinaba: { aae12c19ff 2015-07-23 kinaba: if(m_musteat>=0 && m_next+m_musteat==me->size()) aae12c19ff 2015-07-23 kinaba: return 0; aae12c19ff 2015-07-23 kinaba: aae12c19ff 2015-07-23 kinaba: tuple<int,int,int,int,int> key(turn,m_next,y_next,m_musteat,y_musteat); aae12c19ff 2015-07-23 kinaba: if(memo.count(key)) aae12c19ff 2015-07-23 kinaba: return memo[key]; aae12c19ff 2015-07-23 kinaba: aae12c19ff 2015-07-23 kinaba: // eat aae12c19ff 2015-07-23 kinaba: int best = rec(you,me,turn^1,y_next,m_next,y_musteat,m_musteat+1); aae12c19ff 2015-07-23 kinaba: aae12c19ff 2015-07-23 kinaba: // play aae12c19ff 2015-07-23 kinaba: if(m_next < me->size()) { aae12c19ff 2015-07-23 kinaba: int v = (*me)[m_next]; aae12c19ff 2015-07-23 kinaba: m_next++; aae12c19ff 2015-07-23 kinaba: while(m_next<me->size() && (*me)[m_next]<=v) aae12c19ff 2015-07-23 kinaba: m_next++, m_musteat--; aae12c19ff 2015-07-23 kinaba: while(y_next<you->size() && (*you)[y_next]<=v) aae12c19ff 2015-07-23 kinaba: y_next++, y_musteat--; aae12c19ff 2015-07-23 kinaba: best = max(best, 1+rec(you,me,turn^1,y_next,m_next,y_musteat,m_musteat)); aae12c19ff 2015-07-23 kinaba: } aae12c19ff 2015-07-23 kinaba: aae12c19ff 2015-07-23 kinaba: return memo[key] = best; aae12c19ff 2015-07-23 kinaba: } aae12c19ff 2015-07-23 kinaba: }; aae12c19ff 2015-07-23 kinaba: aae12c19ff 2015-07-23 kinaba: // BEGIN CUT HERE aae12c19ff 2015-07-23 kinaba: #include <ctime> aae12c19ff 2015-07-23 kinaba: double start_time; string timer() aae12c19ff 2015-07-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } aae12c19ff 2015-07-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) aae12c19ff 2015-07-23 kinaba: { os << "{ "; aae12c19ff 2015-07-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) aae12c19ff 2015-07-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } aae12c19ff 2015-07-23 kinaba: void verify_case(const int& Expected, const int& Received) { aae12c19ff 2015-07-23 kinaba: bool ok = (Expected == Received); aae12c19ff 2015-07-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; aae12c19ff 2015-07-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } aae12c19ff 2015-07-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); aae12c19ff 2015-07-23 kinaba: #define END verify_case(_, YetAnotherCardGame().maxCards(petr, snuke));} aae12c19ff 2015-07-23 kinaba: int main(){ aae12c19ff 2015-07-23 kinaba: aae12c19ff 2015-07-23 kinaba: CASE(0) aae12c19ff 2015-07-23 kinaba: int petr_[] = {2, 5}; aae12c19ff 2015-07-23 kinaba: vector <int> petr(petr_, petr_+sizeof(petr_)/sizeof(*petr_)); aae12c19ff 2015-07-23 kinaba: int snuke_[] = {3, 1}; aae12c19ff 2015-07-23 kinaba: vector <int> snuke(snuke_, snuke_+sizeof(snuke_)/sizeof(*snuke_)); aae12c19ff 2015-07-23 kinaba: int _ = 3; aae12c19ff 2015-07-23 kinaba: END aae12c19ff 2015-07-23 kinaba: CASE(1) aae12c19ff 2015-07-23 kinaba: int petr_[] = {1, 1, 1, 1, 1}; aae12c19ff 2015-07-23 kinaba: vector <int> petr(petr_, petr_+sizeof(petr_)/sizeof(*petr_)); aae12c19ff 2015-07-23 kinaba: int snuke_[] = {1, 1, 1, 1, 1}; aae12c19ff 2015-07-23 kinaba: vector <int> snuke(snuke_, snuke_+sizeof(snuke_)/sizeof(*snuke_)); aae12c19ff 2015-07-23 kinaba: int _ = 1; aae12c19ff 2015-07-23 kinaba: END aae12c19ff 2015-07-23 kinaba: CASE(2) aae12c19ff 2015-07-23 kinaba: int petr_[] = {1, 4, 6, 7, 3}; aae12c19ff 2015-07-23 kinaba: vector <int> petr(petr_, petr_+sizeof(petr_)/sizeof(*petr_)); aae12c19ff 2015-07-23 kinaba: int snuke_[] = {1, 7, 1, 5, 7}; aae12c19ff 2015-07-23 kinaba: vector <int> snuke(snuke_, snuke_+sizeof(snuke_)/sizeof(*snuke_)); aae12c19ff 2015-07-23 kinaba: int _ = 6; aae12c19ff 2015-07-23 kinaba: END aae12c19ff 2015-07-23 kinaba: CASE(3) aae12c19ff 2015-07-23 kinaba: int petr_[] = {19, 99, 86, 30, 98, 68, 73, 92, 37, 69, 93, 28, 58, 36, 86, 32, 46, 34, 71, 29}; aae12c19ff 2015-07-23 kinaba: vector <int> petr(petr_, petr_+sizeof(petr_)/sizeof(*petr_)); aae12c19ff 2015-07-23 kinaba: int snuke_[] = {28, 29, 22, 75, 78, 75, 39, 41, 5, 14, 100, 28, 51, 42, 9, 25, 12, 59, 98, 83}; aae12c19ff 2015-07-23 kinaba: vector <int> snuke(snuke_, snuke_+sizeof(snuke_)/sizeof(*snuke_)); aae12c19ff 2015-07-23 kinaba: int _ = 28; aae12c19ff 2015-07-23 kinaba: END aae12c19ff 2015-07-23 kinaba: CASE(4) aae12c19ff 2015-07-23 kinaba: int petr_[] = {69,32,81,40,44,69,80,13,46,34,100,93,81,46,72,10,98,27,27,82,25,52,3,87,77,4,70,87,82,24,4,5,41,97,41,28,12,49,78,37,37,20,70,68,20,76,53,59,25,77}; aae12c19ff 2015-07-23 kinaba: vector <int> petr(petr_, petr_+sizeof(petr_)/sizeof(*petr_)); aae12c19ff 2015-07-23 kinaba: int snuke_[] = {52,76,64,82,37,76,31,5,54,53,98,12,26,22,80,8,5,21,48,44,11,82,9,37,21,60,43,63,29,73,66,46,20,70,83,60,41,61,41,48,11,32,14,39,89,18,74,8,11,84}; aae12c19ff 2015-07-23 kinaba: vector <int> snuke(snuke_, snuke_+sizeof(snuke_)/sizeof(*snuke_)); aae12c19ff 2015-07-23 kinaba: int _ = -1; aae12c19ff 2015-07-23 kinaba: END aae12c19ff 2015-07-23 kinaba: CASE(5) aae12c19ff 2015-07-23 kinaba: int petr_[] = {7,2,7,5,8,1,3,5,7,4,4,8,6,5,8,8,8,4,1,2,1,2,6,3,1,5,2,3,8,2,6,4,3,2,5,4,7,3,6,3,2,6,4,4,3,4,1,1,3,4}; aae12c19ff 2015-07-23 kinaba: vector <int> petr(petr_, petr_+sizeof(petr_)/sizeof(*petr_)); aae12c19ff 2015-07-23 kinaba: int snuke_[] = {7,3,6,7,8,4,7,3,2,1,3,6,8,7,8,2,8,8,7,7,8,5,7,6,2,1,8,5,4,4,8,8,1,4,7,7,7,4,1,8,6,6,1,3,8,4,1,4,2,5}; aae12c19ff 2015-07-23 kinaba: vector <int> snuke(snuke_, snuke_+sizeof(snuke_)/sizeof(*snuke_)); aae12c19ff 2015-07-23 kinaba: int _ = -1; aae12c19ff 2015-07-23 kinaba: END aae12c19ff 2015-07-23 kinaba: } aae12c19ff 2015-07-23 kinaba: // END CUT HERE