4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class LampsGrid { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: int curMax; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int mostLit(vector <string> initial, int K) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // atama-warui 4fd800b3a8 2011-02-23 kinaba: // only meaningful way to flip lights are 4fd800b3a8 2011-02-23 kinaba: // that: exists i {j | initial[i][j]=='0'} 4fd800b3a8 2011-02-23 kinaba: // so at most initial.size() try is sufficient... 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<LL> s; 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<initial[0].size(); ++j) { 4fd800b3a8 2011-02-23 kinaba: LL x = 0; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<initial.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: if( initial[i][j]=='1' ) 4fd800b3a8 2011-02-23 kinaba: x |= (1LL << i); 4fd800b3a8 2011-02-23 kinaba: s.push_back(x); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: curMax = 0; 4fd800b3a8 2011-02-23 kinaba: for(; K>=0; K-=2) 4fd800b3a8 2011-02-23 kinaba: if( K <= initial[0].size() ) 4fd800b3a8 2011-02-23 kinaba: rec((1LL<<initial.size())-1, s, 0, K); 4fd800b3a8 2011-02-23 kinaba: return curMax; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: void rec(LL lit, vector<LL> s, int i, int K) // exact K flips 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int c = 0; 4fd800b3a8 2011-02-23 kinaba: for(LL j=1; j<=lit; j<<=1) 4fd800b3a8 2011-02-23 kinaba: if( lit&j ) 4fd800b3a8 2011-02-23 kinaba: ++c; 4fd800b3a8 2011-02-23 kinaba: if( c<=curMax ) 4fd800b3a8 2011-02-23 kinaba: return; 4fd800b3a8 2011-02-23 kinaba: if( i == s.size() ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: curMax = c; 4fd800b3a8 2011-02-23 kinaba: return; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( i+K < s.size() ) // noflip 4fd800b3a8 2011-02-23 kinaba: rec( lit&s[i], s, i+1, K ); 4fd800b3a8 2011-02-23 kinaba: if( K ) // flip 4fd800b3a8 2011-02-23 kinaba: rec( lit&~s[i], s, i+1, K-1 ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: 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(); } 4fd800b3a8 2011-02-23 kinaba: 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;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} }; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: string initial_[] = {"01", 4fd800b3a8 2011-02-23 kinaba: "10", 4fd800b3a8 2011-02-23 kinaba: "10"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); 4fd800b3a8 2011-02-23 kinaba: int K = 1; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 2; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, LampsGrid().mostLit(initial, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: string initial_[] = {"101010"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); 4fd800b3a8 2011-02-23 kinaba: int K = 2; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, LampsGrid().mostLit(initial, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: string initial_[] = {"00", "11"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); 4fd800b3a8 2011-02-23 kinaba: int K = 999; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, LampsGrid().mostLit(initial, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: string initial_[] = {"0", "1", "0", "1", "0"} 4fd800b3a8 2011-02-23 kinaba: ; 4fd800b3a8 2011-02-23 kinaba: vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); 4fd800b3a8 2011-02-23 kinaba: int K = 1000; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 2; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, LampsGrid().mostLit(initial, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: string initial_[] = {"001", "101", "001", "000", "111", "001", "101", "111", "110", "000", "111", "010", "110", "001"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); 4fd800b3a8 2011-02-23 kinaba: int K = 6; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 4; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, LampsGrid().mostLit(initial, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<5>) { 4fd800b3a8 2011-02-23 kinaba: string initial_[] = {"01", "10", "01", "01", "10"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); 4fd800b3a8 2011-02-23 kinaba: int K = 1; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 3; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, LampsGrid().mostLit(initial, K)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: