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 BinaryFlips { public: 4fd800b3a8 2011-02-23 kinaba: int minimalMoves(int A, int B, int K) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<int> visitedUpto(A+B+1, -1); 4fd800b3a8 2011-02-23 kinaba: visitedUpto[B] = B; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // Breadth-first search from the initial state (A,B) 4fd800b3a8 2011-02-23 kinaba: queue< pair<int,int> > q; 4fd800b3a8 2011-02-23 kinaba: q.push( make_pair(B, 0) ); 4fd800b3a8 2011-02-23 kinaba: while( !q.empty() ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // pop state (a,b) 4fd800b3a8 2011-02-23 kinaba: int b=q.front().first, a=A+B-b, step=q.front().second; q.pop(); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // if (a,b) == (0,A+B), done 4fd800b3a8 2011-02-23 kinaba: if( b == A+B ) 4fd800b3a8 2011-02-23 kinaba: return step; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // for all possible next values (nb) of b... 4fd800b3a8 2011-02-23 kinaba: int nb_min = abs(b-K), nb_max = A+B-abs(a-K); 4fd800b3a8 2011-02-23 kinaba: for(int nb=nb_min; nb<=nb_max; nb+=2) 4fd800b3a8 2011-02-23 kinaba: if( visitedUpto[nb] == -1 ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // if not visited, push the new state to the queue 4fd800b3a8 2011-02-23 kinaba: q.push( make_pair(nb, step+1) ); 4fd800b3a8 2011-02-23 kinaba: visitedUpto[nb] = nb_max; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: else 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // if visited, skip it 4fd800b3a8 2011-02-23 kinaba: int nb2 = visitedUpto[nb]; 4fd800b3a8 2011-02-23 kinaba: visitedUpto[nb] = max(visitedUpto[nb], nb_max); 4fd800b3a8 2011-02-23 kinaba: nb = nb2; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return -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() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const int& Expected, const int& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = (Expected == Received); 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, BinaryFlips().minimalMoves(A, B, K));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: int A = 3; 4fd800b3a8 2011-02-23 kinaba: int B = 0; 4fd800b3a8 2011-02-23 kinaba: int K = 3; 4fd800b3a8 2011-02-23 kinaba: int _ = 1; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: int A = 4; 4fd800b3a8 2011-02-23 kinaba: int B = 0; 4fd800b3a8 2011-02-23 kinaba: int K = 3; 4fd800b3a8 2011-02-23 kinaba: int _ = 4; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: int A = 4; 4fd800b3a8 2011-02-23 kinaba: int B = 1; 4fd800b3a8 2011-02-23 kinaba: int K = 3; 4fd800b3a8 2011-02-23 kinaba: int _ = 2; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: int A = 3; 4fd800b3a8 2011-02-23 kinaba: int B = 2; 4fd800b3a8 2011-02-23 kinaba: int K = 5; 4fd800b3a8 2011-02-23 kinaba: int _ = -1; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: int A = 100000; 4fd800b3a8 2011-02-23 kinaba: int B = 100000; 4fd800b3a8 2011-02-23 kinaba: int K = 578; 4fd800b3a8 2011-02-23 kinaba: int _ = 174; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: int A = 0; 4fd800b3a8 2011-02-23 kinaba: int B = 0; 4fd800b3a8 2011-02-23 kinaba: int K = 1; 4fd800b3a8 2011-02-23 kinaba: int _ = 0; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(6) 4fd800b3a8 2011-02-23 kinaba: int A = 4; 4fd800b3a8 2011-02-23 kinaba: int B = 44; 4fd800b3a8 2011-02-23 kinaba: int K = 50; 4fd800b3a8 2011-02-23 kinaba: int _ = -1; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(7) 4fd800b3a8 2011-02-23 kinaba: int A = 11; 4fd800b3a8 2011-02-23 kinaba: int B = 99995; 4fd800b3a8 2011-02-23 kinaba: int K = 99999; 4fd800b3a8 2011-02-23 kinaba: int _ = 14285; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(8) 4fd800b3a8 2011-02-23 kinaba: int A = 100000; 4fd800b3a8 2011-02-23 kinaba: int B = 100000; 4fd800b3a8 2011-02-23 kinaba: int K = 15781; 4fd800b3a8 2011-02-23 kinaba: int _ = 8; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(9) 4fd800b3a8 2011-02-23 kinaba: int A = 7; 4fd800b3a8 2011-02-23 kinaba: int B = 99128; 4fd800b3a8 2011-02-23 kinaba: int K = 70613; 4fd800b3a8 2011-02-23 kinaba: int _ = 5; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(10) 4fd800b3a8 2011-02-23 kinaba: int A = 45903; 4fd800b3a8 2011-02-23 kinaba: int B = 89257; 4fd800b3a8 2011-02-23 kinaba: int K = 98937; 4fd800b3a8 2011-02-23 kinaba: int _ = 3; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(11) 4fd800b3a8 2011-02-23 kinaba: int A = 93851; 4fd800b3a8 2011-02-23 kinaba: int B = 72517; 4fd800b3a8 2011-02-23 kinaba: int K = 16031; 4fd800b3a8 2011-02-23 kinaba: int _ = 7; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE