8ca6d3c288 2013-12-15 kinaba: #include <iostream> 8ca6d3c288 2013-12-15 kinaba: #include <sstream> 8ca6d3c288 2013-12-15 kinaba: #include <iomanip> 8ca6d3c288 2013-12-15 kinaba: #include <vector> 8ca6d3c288 2013-12-15 kinaba: #include <string> 8ca6d3c288 2013-12-15 kinaba: #include <map> 8ca6d3c288 2013-12-15 kinaba: #include <set> 8ca6d3c288 2013-12-15 kinaba: #include <algorithm> 8ca6d3c288 2013-12-15 kinaba: #include <numeric> 8ca6d3c288 2013-12-15 kinaba: #include <iterator> 8ca6d3c288 2013-12-15 kinaba: #include <functional> 8ca6d3c288 2013-12-15 kinaba: #include <complex> 8ca6d3c288 2013-12-15 kinaba: #include <queue> 8ca6d3c288 2013-12-15 kinaba: #include <stack> 8ca6d3c288 2013-12-15 kinaba: #include <cmath> 8ca6d3c288 2013-12-15 kinaba: #include <cassert> 8ca6d3c288 2013-12-15 kinaba: #include <tuple> 8ca6d3c288 2013-12-15 kinaba: using namespace std; 8ca6d3c288 2013-12-15 kinaba: typedef long long LL; 8ca6d3c288 2013-12-15 kinaba: typedef complex<double> CMP; 8ca6d3c288 2013-12-15 kinaba: 8ca6d3c288 2013-12-15 kinaba: class ORSolitaire { public: 8ca6d3c288 2013-12-15 kinaba: int getMinimum(vector <int> numbers, int goal) 8ca6d3c288 2013-12-15 kinaba: { 8ca6d3c288 2013-12-15 kinaba: vector<int> n2; 8ca6d3c288 2013-12-15 kinaba: for(int u: numbers) 8ca6d3c288 2013-12-15 kinaba: if((goal&u) == u) 8ca6d3c288 2013-12-15 kinaba: n2.push_back(u); 8ca6d3c288 2013-12-15 kinaba: numbers.swap(n2); 8ca6d3c288 2013-12-15 kinaba: 8ca6d3c288 2013-12-15 kinaba: int best = 9999999; 8ca6d3c288 2013-12-15 kinaba: for(int v=1; v<=goal; v<<=1) if(goal&v) { 8ca6d3c288 2013-12-15 kinaba: int cnt = 0; 8ca6d3c288 2013-12-15 kinaba: for(int u: numbers) 8ca6d3c288 2013-12-15 kinaba: if(u&v) 8ca6d3c288 2013-12-15 kinaba: ++cnt; 8ca6d3c288 2013-12-15 kinaba: best = min(best, cnt); 8ca6d3c288 2013-12-15 kinaba: } 8ca6d3c288 2013-12-15 kinaba: return best; 8ca6d3c288 2013-12-15 kinaba: } 8ca6d3c288 2013-12-15 kinaba: }; 8ca6d3c288 2013-12-15 kinaba: 8ca6d3c288 2013-12-15 kinaba: // BEGIN CUT HERE 8ca6d3c288 2013-12-15 kinaba: #include <ctime> 8ca6d3c288 2013-12-15 kinaba: double start_time; string timer() 8ca6d3c288 2013-12-15 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 8ca6d3c288 2013-12-15 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 8ca6d3c288 2013-12-15 kinaba: { os << "{ "; 8ca6d3c288 2013-12-15 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 8ca6d3c288 2013-12-15 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 8ca6d3c288 2013-12-15 kinaba: void verify_case(const int& Expected, const int& Received) { 8ca6d3c288 2013-12-15 kinaba: bool ok = (Expected == Received); 8ca6d3c288 2013-12-15 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 8ca6d3c288 2013-12-15 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 8ca6d3c288 2013-12-15 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 8ca6d3c288 2013-12-15 kinaba: #define END verify_case(_, ORSolitaire().getMinimum(numbers, goal));} 8ca6d3c288 2013-12-15 kinaba: int main(){ 8ca6d3c288 2013-12-15 kinaba: 8ca6d3c288 2013-12-15 kinaba: CASE(0) 8ca6d3c288 2013-12-15 kinaba: int numbers_[] = {1, 2, 4}; 8ca6d3c288 2013-12-15 kinaba: vector <int> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 8ca6d3c288 2013-12-15 kinaba: int goal = 7; 8ca6d3c288 2013-12-15 kinaba: int _ = 1; 8ca6d3c288 2013-12-15 kinaba: END 8ca6d3c288 2013-12-15 kinaba: CASE(1) 8ca6d3c288 2013-12-15 kinaba: int numbers_[] = {1, 2, 4, 7, 8}; 8ca6d3c288 2013-12-15 kinaba: vector <int> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 8ca6d3c288 2013-12-15 kinaba: int goal = 7; 8ca6d3c288 2013-12-15 kinaba: int _ = 2; 8ca6d3c288 2013-12-15 kinaba: END 8ca6d3c288 2013-12-15 kinaba: CASE(2) 8ca6d3c288 2013-12-15 kinaba: int numbers_[] = {12571295, 2174218, 2015120}; 8ca6d3c288 2013-12-15 kinaba: vector <int> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 8ca6d3c288 2013-12-15 kinaba: int goal = 1; 8ca6d3c288 2013-12-15 kinaba: int _ = 0; 8ca6d3c288 2013-12-15 kinaba: END 8ca6d3c288 2013-12-15 kinaba: CASE(3) 8ca6d3c288 2013-12-15 kinaba: int numbers_[] = {5,2,4,52,62,9,8,3,1,11,6}; 8ca6d3c288 2013-12-15 kinaba: vector <int> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 8ca6d3c288 2013-12-15 kinaba: int goal = 11; 8ca6d3c288 2013-12-15 kinaba: int _ = 3; 8ca6d3c288 2013-12-15 kinaba: END 8ca6d3c288 2013-12-15 kinaba: CASE(4) 8ca6d3c288 2013-12-15 kinaba: int numbers_[] = {503, 505, 152, 435, 491, 512, 1023, 355, 510, 500, 502, 255, 63, 508, 509, 511, 60, 250, 254, 346}; 8ca6d3c288 2013-12-15 kinaba: vector <int> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 8ca6d3c288 2013-12-15 kinaba: int goal = 510; 8ca6d3c288 2013-12-15 kinaba: int _ = 5; 8ca6d3c288 2013-12-15 kinaba: END 8ca6d3c288 2013-12-15 kinaba: CASE(5) 8ca6d3c288 2013-12-15 kinaba: int numbers_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50}; 8ca6d3c288 2013-12-15 kinaba: vector <int> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 8ca6d3c288 2013-12-15 kinaba: int goal = 63; 8ca6d3c288 2013-12-15 kinaba: int _ = 19; 8ca6d3c288 2013-12-15 kinaba: END 8ca6d3c288 2013-12-15 kinaba: /* 8ca6d3c288 2013-12-15 kinaba: CASE(6) 8ca6d3c288 2013-12-15 kinaba: int numbers_[] = ; 8ca6d3c288 2013-12-15 kinaba: vector <int> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 8ca6d3c288 2013-12-15 kinaba: int goal = ; 8ca6d3c288 2013-12-15 kinaba: int _ = ; 8ca6d3c288 2013-12-15 kinaba: END 8ca6d3c288 2013-12-15 kinaba: */ 8ca6d3c288 2013-12-15 kinaba: } 8ca6d3c288 2013-12-15 kinaba: // END CUT HERE