Artifact Content
Not logged in

Artifact d9b27fe6bd32c9ac3cecc692cd12713b4503d801


#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 <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class YetAnotherCardGame { public:
	int maxCards(vector <int> petr, vector <int> snuke)
	{
		sort(petr.begin(), petr.end());
		sort(snuke.begin(), snuke.end());

		return rec(&petr, &snuke, 0, 0, 0, 0, 0);
	}

	map<tuple<int,int,int,int,int>,int> memo;
	int rec(const vector<int>* me, const vector<int>* you,
		int turn, int m_next, int y_next, int m_musteat, int y_musteat)
	{
		if(m_musteat>=0 && m_next+m_musteat==me->size())
			return 0;

		tuple<int,int,int,int,int> key(turn,m_next,y_next,m_musteat,y_musteat);
		if(memo.count(key))
			return memo[key];

		// eat
		int best = rec(you,me,turn^1,y_next,m_next,y_musteat,m_musteat+1);

		// play
		if(m_next < me->size()) {
			int v = (*me)[m_next];
			m_next++;
			while(m_next<me->size() && (*me)[m_next]<=v)
				m_next++, m_musteat--;
			while(y_next<you->size() && (*you)[y_next]<=v)
				y_next++, y_musteat--;
			best = max(best, 1+rec(you,me,turn^1,y_next,m_next,y_musteat,m_musteat));
		}

		return memo[key] = best;
	}
};

// 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 int& Expected, const 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(_, YetAnotherCardGame().maxCards(petr, snuke));}
int main(){

CASE(0)
	int petr_[] = {2, 5};
	  vector <int> petr(petr_, petr_+sizeof(petr_)/sizeof(*petr_)); 
	int snuke_[] = {3, 1};
	  vector <int> snuke(snuke_, snuke_+sizeof(snuke_)/sizeof(*snuke_)); 
	int _ = 3; 
END
CASE(1)
	int petr_[] = {1, 1, 1, 1, 1};
	  vector <int> petr(petr_, petr_+sizeof(petr_)/sizeof(*petr_)); 
	int snuke_[] = {1, 1, 1, 1, 1};
	  vector <int> snuke(snuke_, snuke_+sizeof(snuke_)/sizeof(*snuke_)); 
	int _ = 1; 
END
CASE(2)
	int petr_[] = {1, 4, 6, 7, 3};
	  vector <int> petr(petr_, petr_+sizeof(petr_)/sizeof(*petr_)); 
	int snuke_[] = {1, 7, 1, 5, 7};
	  vector <int> snuke(snuke_, snuke_+sizeof(snuke_)/sizeof(*snuke_)); 
	int _ = 6; 
END
CASE(3)
	int petr_[] = {19, 99, 86, 30, 98, 68, 73, 92, 37, 69, 93, 28, 58, 36, 86, 32, 46, 34, 71, 29};
	  vector <int> petr(petr_, petr_+sizeof(petr_)/sizeof(*petr_)); 
	int snuke_[] = {28, 29, 22, 75, 78, 75, 39, 41, 5, 14, 100, 28, 51, 42, 9, 25, 12, 59, 98, 83};
	  vector <int> snuke(snuke_, snuke_+sizeof(snuke_)/sizeof(*snuke_)); 
	int _ = 28; 
END
CASE(4)
	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};
	  vector <int> petr(petr_, petr_+sizeof(petr_)/sizeof(*petr_)); 
	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};
	  vector <int> snuke(snuke_, snuke_+sizeof(snuke_)/sizeof(*snuke_)); 
	int _ = -1; 
END
CASE(5)
	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};
	  vector <int> petr(petr_, petr_+sizeof(petr_)/sizeof(*petr_)); 
	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};
	  vector <int> snuke(snuke_, snuke_+sizeof(snuke_)/sizeof(*snuke_)); 
	int _ = -1; 
END
}
// END CUT HERE