Artifact Content
Not logged in

Artifact 1096f6b690aa542dd54c711be9c86cef0972121c


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class BinaryFlips { public:
	int minimalMoves(int A, int B, int K) 
	{
		vector<int> visitedUpto(A+B+1, -1);
		visitedUpto[B] = B;

		// Breadth-first search from the initial state (A,B)
		queue< pair<int,int> > q;
		q.push( make_pair(B, 0) );
		while( !q.empty() )
		{
			// pop state (a,b)
			int b=q.front().first, a=A+B-b, step=q.front().second; q.pop();

			// if (a,b) == (0,A+B), done
			if( b == A+B )
				return step;

			// for all possible next values (nb) of b...
			int nb_min = abs(b-K), nb_max = A+B-abs(a-K);
			for(int nb=nb_min; nb<=nb_max; nb+=2)
				if( visitedUpto[nb] == -1 )
				{
					// if not visited, push the new state to the queue
					q.push( make_pair(nb, step+1) );
					visitedUpto[nb] = nb_max;
				}
				else
				{
					// if visited, skip it
					int nb2 = visitedUpto[nb];
					visitedUpto[nb] = max(visitedUpto[nb], nb_max);
					nb = nb2;
				}
		}
		return -1;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, BinaryFlips().minimalMoves(A, B, K));}
int main(){

CASE(0)
	int A = 3; 
	int B = 0; 
	int K = 3; 
	int _ = 1; 
END
CASE(1)
	int A = 4; 
	int B = 0; 
	int K = 3; 
	int _ = 4; 
END
CASE(2)
	int A = 4; 
	int B = 1; 
	int K = 3; 
	int _ = 2; 
END
CASE(3)
	int A = 3; 
	int B = 2; 
	int K = 5; 
	int _ = -1; 
END
CASE(4)
	int A = 100000; 
	int B = 100000; 
	int K = 578; 
	int _ = 174; 
END
CASE(5)
	int A = 0; 
	int B = 0; 
	int K = 1; 
	int _ = 0; 
END
CASE(6)
	int A = 4; 
	int B = 44; 
	int K = 50; 
	int _ = -1; 
END
CASE(7)
	int A = 11; 
	int B = 99995; 
	int K = 99999; 
	int _ = 14285;
END
CASE(8)
	int A = 100000; 
	int B = 100000; 
	int K = 15781; 
	int _ = 8; 
END
CASE(9)
	int A = 7; 
	int B = 99128; 
	int K = 70613; 
	int _ = 5; 
END
CASE(10)
	int A = 45903; 
	int B = 89257; 
	int K = 98937; 
	int _ = 3; 
END
CASE(11)
	int A = 93851; 
	int B = 72517; 
	int K = 16031; 
	int _ = 7; 
END

}
// END CUT HERE