Artifact Content
Not logged in

Artifact 124cec5a706621e02718f60a854b2b049d48748f


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

class MagicalHats { public:
	int NC, NH, H, W, NG;
	vector<int> gain;
	vector< pair<int,int> > hats;
	vector<int> tate_base, yoko_base;

	int findMaximumReward(vector <string> board, vector <int> coins, int numGuesses)
	{
		NC = coins.size();
		sort(coins.begin(), coins.end());
		gain.assign(NC+1, 0);
		partial_sum(coins.begin(), coins.end(), gain.begin()+1);

		H = board.size();
		W = board[0].size();
		hats.clear();
		tate_base.resize(W);
		yoko_base.resize(H);
		for(int y=0; y<H; ++y)
		for(int x=0; x<W; ++x)
			if(board[y][x] == 'H') {
				hats.push_back( make_pair(y,x) );
				tate_base[x]++;
				yoko_base[y]++;
			}
		NH = hats.size();
		NG = numGuesses;

		int m = 0;
		for(int i=0; i<NH; ++i)
			m = m*3 + 2;
		p_memo.resize(m+1);
		p_memo_set.resize(m+1);
		b_memo.resize(m+1);
		b_memo_set.resize(m+1);
		return is_possible(m) ? gain[best_num(m, NG)] : -1;
	}

	vector<bool> p_memo;
	vector<bool> p_memo_set;
	bool is_possible(int state)
	{
		if(p_memo_set[state])
			return p_memo[state];
		p_memo_set[state] = true;

		vector<int> yoko = yoko_base, tate = tate_base, next;
		int cnt = 0;
		for(int i=0,m=1; i<NH; ++i,m*=3)
		{
			int v = (state/m)%3;
			if( v == 1 ) {
				int y = hats[i].first;
				int x = hats[i].second;
				yoko[y]++;
				tate[x]++;
				++cnt;
			} else if( v == 2 ) {
				next.push_back(m);
			}
		}
		if( next.empty() )
		{
			if( cnt != NC )
				return false;
			for(int y=0; y<yoko.size(); ++y)
				if(yoko[y]%2 != 0)
					return p_memo[state] = false;
			for(int x=0; x<tate.size(); ++x)
				if(tate[x]%2 != 0)
					return p_memo[state] = false;
			return p_memo[state] = true;
		}
		else
		{
			int m = next[0];
			int s[] = {state-2*m, state-m};
			return p_memo[state] = ( is_possible(s[0]) || is_possible(s[1]) );
		}
	}

	vector<char> b_memo;
	vector<bool> b_memo_set;
	char best_num(int state, int guess)
	{
		if( guess==0 )
			return 0;
		if(b_memo_set[state])
			return b_memo[state];
		b_memo_set[state] = true;

		char best = 0;
		for(int i=0,m=1; i<NH; ++i,m*=3)
		{
			int v = (state/m)%3;
			if( v == 2 )
			{
				int s[] = {state-2*m, state-m};
				char score = 13;
				if( is_possible(s[0]) )
					score = min(score, best_num(s[0], guess-1));
				if( is_possible(s[1]) )
					score = min<char>(score, best_num(s[1], guess-1)+1);
				best = max(best, score);
			}
		}
		return b_memo[state] = 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(_, MagicalHats().findMaximumReward(board, coins, numGuesses));}
int main(){

CASE(0)
	string board_[] = {"H"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int coins_[] = {1};
	  vector <int> coins(coins_, coins_+sizeof(coins_)/sizeof(*coins_)); 
	int numGuesses = 1; 
	int _ = 1; 
END
CASE(1)
	string board_[] = {"HHH",
 "H.H",
 "HH."};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int coins_[] = {9};
	  vector <int> coins(coins_, coins_+sizeof(coins_)/sizeof(*coins_)); 
	int numGuesses = 1; 
	int _ = 9; 
END
CASE(2)
	string board_[] = {"HH",
 "HH"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int coins_[] = {1,2,3,4};
	  vector <int> coins(coins_, coins_+sizeof(coins_)/sizeof(*coins_)); 
	int numGuesses = 3; 
	int _ = 6; 
END
CASE(3)
	string board_[] = {"HHH",
 "HHH",
 "H.H"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int coins_[] = {13,13,13,13};
	  vector <int> coins(coins_, coins_+sizeof(coins_)/sizeof(*coins_)); 
	int numGuesses = 2; 
	int _ = 13; 
END
CASE(4)
	string board_[] = {"HHH",
 "HHH",
 "H.H"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int coins_[] = {13,13,13,13};
	  vector <int> coins(coins_, coins_+sizeof(coins_)/sizeof(*coins_)); 
	int numGuesses = 3; 
	int _ = 26; 
END
CASE(5)
	string board_[] = {"H.H.",
 ".H.H",
 "H.H."};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int coins_[] = {17};
	  vector <int> coins(coins_, coins_+sizeof(coins_)/sizeof(*coins_)); 
	int numGuesses = 6; 
	int _ = -1; 
END
CASE(6)
	string board_[] = {"HHH..........",
 "H.H..........",
 "HHH..........",
 "H.H..........",
 "HHH.........."
".............",
".............",
".............",
".............",
".............",
".............",
".............",
".............",
};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int coins_[] = {33,337,1007,2403,5601,6003,9999};
	  vector <int> coins(coins_, coins_+sizeof(coins_)/sizeof(*coins_)); 
	int numGuesses = 5; 
	int _ = 1377; 
END
CASE(7)
	string board_[] = {".............",
 ".............",
 ".............",
 ".............",
 ".............",
 ".............",
 ".....H.H.....",
 "......H......",
 ".....H.H.....",
 ".............",
 ".............",
 ".............",
 "............."};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int coins_[] = {22};
	  vector <int> coins(coins_, coins_+sizeof(coins_)/sizeof(*coins_)); 
	int numGuesses = 3; 
	int _ = 22; 
END
/*
CASE(8)
	string board_[] = ;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int coins_[] = ;
	  vector <int> coins(coins_, coins_+sizeof(coins_)/sizeof(*coins_)); 
	int numGuesses = ; 
	int _ = ; 
END
CASE(9)
	string board_[] = ;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int coins_[] = ;
	  vector <int> coins(coins_, coins_+sizeof(coins_)/sizeof(*coins_)); 
	int numGuesses = ; 
	int _ = ; 
END
*/
}
// END CUT HERE