Artifact Content
Not logged in

Artifact 329bdbe17af21f45e95526b5e2f303ca67516b53


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;

class LampsGrid {
public:
	int curMax;

	int mostLit(vector <string> initial, int K) 
	{
		// atama-warui
		//   only meaningful way to flip lights are
		//   that: exists i {j | initial[i][j]=='0'}
		//   so at most initial.size() try is sufficient...

		vector<LL> s;
		for(int j=0; j<initial[0].size(); ++j) {
			LL x = 0;
			for(int i=0; i<initial.size(); ++i)
				if( initial[i][j]=='1' )
					x |= (1LL << i);
			s.push_back(x);
		}

		curMax = 0;
		for(; K>=0; K-=2)
			if( K <= initial[0].size() )
				rec((1LL<<initial.size())-1, s, 0, K);
		return curMax;
	}

	void rec(LL lit, vector<LL> s, int i, int K) // exact K flips
	{
		int c = 0;
		for(LL j=1; j<=lit; j<<=1)
			if( lit&j )
				++c;
		if( c<=curMax )
			return;
		if( i == s.size() )
		{
			curMax = c;
			return;
		}

		if( i+K < s.size() ) // noflip
			rec( lit&s[i], s, i+1, K );
		if( K ) // flip
			rec( lit&~s[i], s, i+1, K-1 );
	}
};

// 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> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	string initial_[] = {"01",
 "10",
 "10"};
	  vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); 
	int K = 1; 
	int RetVal = 2; 
	return verify_case(RetVal, LampsGrid().mostLit(initial, K)); }
int Test_(Case_<1>) {
	string initial_[] = {"101010"};
	  vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); 
	int K = 2; 
	int RetVal = 0; 
	return verify_case(RetVal, LampsGrid().mostLit(initial, K)); }
int Test_(Case_<2>) {
	string initial_[] = {"00", "11"};
	  vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); 
	int K = 999; 
	int RetVal = 0; 
	return verify_case(RetVal, LampsGrid().mostLit(initial, K)); }
int Test_(Case_<3>) {
	string initial_[] = {"0", "1", "0", "1", "0"}
;
	  vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); 
	int K = 1000; 
	int RetVal = 2; 
	return verify_case(RetVal, LampsGrid().mostLit(initial, K)); }
int Test_(Case_<4>) {
	string initial_[] = {"001", "101", "001", "000", "111", "001", "101", "111", "110", "000", "111", "010", "110", "001"};
	  vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); 
	int K = 6; 
	int RetVal = 4; 
	return verify_case(RetVal, LampsGrid().mostLit(initial, K)); }
int Test_(Case_<5>) {
	string initial_[] = {"01", "10", "01", "01", "10"};
	  vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); 
	int K = 1; 
	int RetVal = 3; 
	return verify_case(RetVal, LampsGrid().mostLit(initial, K)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE