d3b1273780 2014-06-12 kinaba: #include <iostream> d3b1273780 2014-06-12 kinaba: #include <sstream> d3b1273780 2014-06-12 kinaba: #include <iomanip> d3b1273780 2014-06-12 kinaba: #include <vector> d3b1273780 2014-06-12 kinaba: #include <string> d3b1273780 2014-06-12 kinaba: #include <map> d3b1273780 2014-06-12 kinaba: #include <set> d3b1273780 2014-06-12 kinaba: #include <algorithm> d3b1273780 2014-06-12 kinaba: #include <numeric> d3b1273780 2014-06-12 kinaba: #include <iterator> d3b1273780 2014-06-12 kinaba: #include <functional> d3b1273780 2014-06-12 kinaba: #include <complex> d3b1273780 2014-06-12 kinaba: #include <queue> d3b1273780 2014-06-12 kinaba: #include <stack> d3b1273780 2014-06-12 kinaba: #include <cmath> d3b1273780 2014-06-12 kinaba: #include <cassert> d3b1273780 2014-06-12 kinaba: #include <tuple> d3b1273780 2014-06-12 kinaba: using namespace std; d3b1273780 2014-06-12 kinaba: typedef long long LL; d3b1273780 2014-06-12 kinaba: typedef complex<double> CMP; d3b1273780 2014-06-12 kinaba: d3b1273780 2014-06-12 kinaba: class SwitchingGame { public: d3b1273780 2014-06-12 kinaba: int timeToWin(vector <string> states) d3b1273780 2014-06-12 kinaba: { d3b1273780 2014-06-12 kinaba: const int W = states[0].size(); d3b1273780 2014-06-12 kinaba: string cur(W, '-'); d3b1273780 2014-06-12 kinaba: d3b1273780 2014-06-12 kinaba: int sec = 0; d3b1273780 2014-06-12 kinaba: for(int i=0; i<states.size(); ++i) d3b1273780 2014-06-12 kinaba: { d3b1273780 2014-06-12 kinaba: bool need_flip_to_minus = false; d3b1273780 2014-06-12 kinaba: bool need_flip_to_plus = false; d3b1273780 2014-06-12 kinaba: for(int k=0; k<W; ++k) { d3b1273780 2014-06-12 kinaba: if(states[i][k]=='-' && cur[k]=='+') d3b1273780 2014-06-12 kinaba: need_flip_to_minus = true; d3b1273780 2014-06-12 kinaba: if(states[i][k]=='+' && cur[k]=='-') d3b1273780 2014-06-12 kinaba: need_flip_to_plus = true; d3b1273780 2014-06-12 kinaba: } d3b1273780 2014-06-12 kinaba: d3b1273780 2014-06-12 kinaba: for(int k=0; k<W; ++k) { d3b1273780 2014-06-12 kinaba: switch(states[i][k]) d3b1273780 2014-06-12 kinaba: { d3b1273780 2014-06-12 kinaba: case '-': d3b1273780 2014-06-12 kinaba: cur[k] = '-'; d3b1273780 2014-06-12 kinaba: break; d3b1273780 2014-06-12 kinaba: case '+': d3b1273780 2014-06-12 kinaba: cur[k] = '+'; d3b1273780 2014-06-12 kinaba: break; d3b1273780 2014-06-12 kinaba: case '?': d3b1273780 2014-06-12 kinaba: for(int j=i+1; j<states.size(); ++j) { d3b1273780 2014-06-12 kinaba: if(states[j][k]=='-') {if(need_flip_to_minus)cur[k]='-';break;} d3b1273780 2014-06-12 kinaba: if(states[j][k]=='+') {if(need_flip_to_plus)cur[k]='+';break;} d3b1273780 2014-06-12 kinaba: } d3b1273780 2014-06-12 kinaba: break; d3b1273780 2014-06-12 kinaba: } d3b1273780 2014-06-12 kinaba: } d3b1273780 2014-06-12 kinaba: sec += 1 + need_flip_to_plus + need_flip_to_minus; d3b1273780 2014-06-12 kinaba: } d3b1273780 2014-06-12 kinaba: return sec; d3b1273780 2014-06-12 kinaba: } d3b1273780 2014-06-12 kinaba: }; d3b1273780 2014-06-12 kinaba: d3b1273780 2014-06-12 kinaba: // BEGIN CUT HERE d3b1273780 2014-06-12 kinaba: #include <ctime> d3b1273780 2014-06-12 kinaba: double start_time; string timer() d3b1273780 2014-06-12 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } d3b1273780 2014-06-12 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) d3b1273780 2014-06-12 kinaba: { os << "{ "; d3b1273780 2014-06-12 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) d3b1273780 2014-06-12 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } d3b1273780 2014-06-12 kinaba: void verify_case(const int& Expected, const int& Received) { d3b1273780 2014-06-12 kinaba: bool ok = (Expected == Received); d3b1273780 2014-06-12 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; d3b1273780 2014-06-12 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } d3b1273780 2014-06-12 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); d3b1273780 2014-06-12 kinaba: #define END verify_case(_, SwitchingGame().timeToWin(states));} d3b1273780 2014-06-12 kinaba: int main(){ d3b1273780 2014-06-12 kinaba: d3b1273780 2014-06-12 kinaba: CASE(1) d3b1273780 2014-06-12 kinaba: string states_[] = {"+-++", d3b1273780 2014-06-12 kinaba: "+-++"}; d3b1273780 2014-06-12 kinaba: vector <string> states(states_, states_+sizeof(states_)/sizeof(*states_)); d3b1273780 2014-06-12 kinaba: int _ = 3; d3b1273780 2014-06-12 kinaba: END d3b1273780 2014-06-12 kinaba: CASE(0) d3b1273780 2014-06-12 kinaba: string states_[] = {"++--", d3b1273780 2014-06-12 kinaba: "--++"}; d3b1273780 2014-06-12 kinaba: vector <string> states(states_, states_+sizeof(states_)/sizeof(*states_)); d3b1273780 2014-06-12 kinaba: int _ = 5; d3b1273780 2014-06-12 kinaba: END d3b1273780 2014-06-12 kinaba: CASE(2) d3b1273780 2014-06-12 kinaba: string states_[] = {"++", d3b1273780 2014-06-12 kinaba: "+?", d3b1273780 2014-06-12 kinaba: "?+", d3b1273780 2014-06-12 kinaba: "++"}; d3b1273780 2014-06-12 kinaba: vector <string> states(states_, states_+sizeof(states_)/sizeof(*states_)); d3b1273780 2014-06-12 kinaba: int _ = 5; d3b1273780 2014-06-12 kinaba: END d3b1273780 2014-06-12 kinaba: CASE(3) d3b1273780 2014-06-12 kinaba: string states_[] = {"+", d3b1273780 2014-06-12 kinaba: "?", d3b1273780 2014-06-12 kinaba: "?", d3b1273780 2014-06-12 kinaba: "?", d3b1273780 2014-06-12 kinaba: "-"}; d3b1273780 2014-06-12 kinaba: vector <string> states(states_, states_+sizeof(states_)/sizeof(*states_)); d3b1273780 2014-06-12 kinaba: int _ = 7; d3b1273780 2014-06-12 kinaba: END d3b1273780 2014-06-12 kinaba: CASE(4) d3b1273780 2014-06-12 kinaba: string states_[] = {"+??+++", d3b1273780 2014-06-12 kinaba: "++??+-", d3b1273780 2014-06-12 kinaba: "?++??+", d3b1273780 2014-06-12 kinaba: "?-+-??", d3b1273780 2014-06-12 kinaba: "??+?++", d3b1273780 2014-06-12 kinaba: "++-?+?", d3b1273780 2014-06-12 kinaba: "?++?-+", d3b1273780 2014-06-12 kinaba: "?--+++", d3b1273780 2014-06-12 kinaba: "-??-?+"} d3b1273780 2014-06-12 kinaba: ; d3b1273780 2014-06-12 kinaba: vector <string> states(states_, states_+sizeof(states_)/sizeof(*states_)); d3b1273780 2014-06-12 kinaba: int _ = 20; d3b1273780 2014-06-12 kinaba: END d3b1273780 2014-06-12 kinaba: /* d3b1273780 2014-06-12 kinaba: CASE(5) d3b1273780 2014-06-12 kinaba: string states_[] = ; d3b1273780 2014-06-12 kinaba: vector <string> states(states_, states_+sizeof(states_)/sizeof(*states_)); d3b1273780 2014-06-12 kinaba: int _ = ; d3b1273780 2014-06-12 kinaba: END d3b1273780 2014-06-12 kinaba: CASE(6) d3b1273780 2014-06-12 kinaba: string states_[] = ; d3b1273780 2014-06-12 kinaba: vector <string> states(states_, states_+sizeof(states_)/sizeof(*states_)); d3b1273780 2014-06-12 kinaba: int _ = ; d3b1273780 2014-06-12 kinaba: END d3b1273780 2014-06-12 kinaba: */ d3b1273780 2014-06-12 kinaba: } d3b1273780 2014-06-12 kinaba: // END CUT HERE