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 <functional> 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: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class CarolsSinging { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: int choose(vector <string> lyrics) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int nP = lyrics.size(); 4fd800b3a8 2011-02-23 kinaba: int nS = lyrics[0].size(); 4fd800b3a8 2011-02-23 kinaba: vector<int> people_who_know_the_song(nS); 4fd800b3a8 2011-02-23 kinaba: for(int s=0; s<nS; ++s) 4fd800b3a8 2011-02-23 kinaba: for(int p=0; p<nP; ++p) 4fd800b3a8 2011-02-23 kinaba: people_who_know_the_song[s] |= (lyrics[p][s]=='Y')<<p; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int minPop = nS; 4fd800b3a8 2011-02-23 kinaba: for(int m=1; m<(1<<nS); ++m) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int canSing = 0; 4fd800b3a8 2011-02-23 kinaba: for(int s=0; (1<<s)<=m; ++s) 4fd800b3a8 2011-02-23 kinaba: if( (1<<s) & m ) 4fd800b3a8 2011-02-23 kinaba: canSing |= people_who_know_the_song[s]; 4fd800b3a8 2011-02-23 kinaba: if( canSing == (1<<nP)-1 ) 4fd800b3a8 2011-02-23 kinaba: minPop = min(minPop, __builtin_popcount(m)); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return minPop; 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 lyrics_[] = {"YN","NY"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> lyrics(lyrics_, lyrics_+sizeof(lyrics_)/sizeof(*lyrics_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 2; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, CarolsSinging().choose(lyrics)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: string lyrics_[] = {"YN","YY","YN"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> lyrics(lyrics_, lyrics_+sizeof(lyrics_)/sizeof(*lyrics_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 1; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, CarolsSinging().choose(lyrics)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: string lyrics_[] = {"YNN","YNY","YNY","NYY","NYY","NYN"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> lyrics(lyrics_, lyrics_+sizeof(lyrics_)/sizeof(*lyrics_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 2; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, CarolsSinging().choose(lyrics)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: string lyrics_[] = {"YNNYYY","YYNYYY","YNNYYN","NYYNNN","YYYNNN","YYYNNY","NYYYYY","NYNYYY","NNNNYY", 4fd800b3a8 2011-02-23 kinaba: "YYYYYY","YNNNNN","YYYYNY","YYNNNN","NNYYYN","NNNNYY","YYYNNN","NYNNYN","YNNYYN", 4fd800b3a8 2011-02-23 kinaba: "YYNNNY","NYYNNY","NNYYYN","YNYYYN","NNNYNY","YYYYNN","YYNYNN","NYYNYY","YYNYYN"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> lyrics(lyrics_, lyrics_+sizeof(lyrics_)/sizeof(*lyrics_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 4; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, CarolsSinging().choose(lyrics)); } 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: