Artifact Content
Not logged in

Artifact 24617375bbc033b8291aa5db0d5a11d47d64bed3


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

class DoorsGame { public:
	int determineOutcome(string doors, int trophy) 
	{
		vector<int> score(2<<16);
		vector<bool> computed(2<<16);
		return rec(score, computed, 0, doors, trophy);
	}
	int rec(vector<int>& score, vector<bool>& computed, int mask, string& doors, int trophy)
	{
		if( computed[mask] )
			return score[mask];
		computed[mask] = true;

		bool Lwin = true;
		for(int i=0; i<trophy; ++i)
			if( !(mask & (1<<(doors[i]-'A'))) )
				Lwin = false;
		bool Rwin = true;
		for(int i=doors.size()-1; i>=trophy; --i)
			if( !(mask & (1<<(doors[i]-'A'))) )
				Rwin = false;

		if( Lwin && Rwin )
			return score[mask] = 0;
		if( Lwin )
			return score[mask] = +bitcnt(mask);
		if( Rwin )
			return score[mask] = -bitcnt(mask);

		vector<int> ss;
		for(int c=0; c<16; ++c)
			if( !(mask & (1<<c)) )
				ss.push_back( rec(score,computed,mask|(1<<c),doors,trophy) );

		bool L = !(bitcnt(mask)&1);
		if(!L)
			for(int i=0; i<ss.size(); ++i)
				ss[i] = -ss[i];

		int Ms = *max_element(ss.begin(),ss.end());
		if( Ms == 0 )
			return score[mask] = 0;
		if( Ms > 0 ) {
			int posMin = 99999;
			for(int i=0; i<ss.size(); ++i)
				if(ss[i]>0)
					posMin = min(posMin,ss[i]);
			return score[mask] = (L ? posMin : -posMin);
		}
		int me = *min_element(ss.begin(), ss.end());
		return score[mask] = (L ? me : -me);
	}
	int bitcnt(int mask)
	{
		int cnt = 0;
		for(; mask; mask>>=1)
			cnt += mask&1;
		return cnt;
	}
};

// 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(_, DoorsGame().determineOutcome(doors, trophy));}
int main(){

CASE(0)
	string doors = "ABCD"; 
	int trophy = 2; 
	int _ = 3; 
END
CASE(1)
	string doors = "ABCC"; 
	int trophy = 2; 
	int _ = -2; 
END
CASE(2)
	string doors = "ABABAB"; 
	int trophy = 3; 
	int _ = 0; 
END
CASE(3)
	string doors = "ABAPDCAA"; 
	int trophy = 5; 
	int _ = -4; 
END
CASE(4)
	string doors = "MOCFDCE"; 
	int trophy = 3; 
	int _ = 5; 
END
CASE(5)
	string doors = "ABCCDE"; 
	int trophy = 3; 
	int _ = 0; 
END
CASE(6)
	string doors = "ABCCD"; 
	int trophy = 3; 
	int _ = 0; 
END
CASE(7)
	string doors = "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPCC"; 
	int trophy = 25; 
	int _ = 0; 
END
CASE(8)
	string doors = "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPCC"; 
	int trophy = 7; 
	int _ = 0; 
END
CASE(8)
	string doors = "AAAAAAAAAAAAAAAAABCDEFGHIJKLMNOPPPPPPPPPPPPPPPPPP"; 
	int trophy = 25; 
	int _ = 0; 
END
}
// END CUT HERE