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 <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: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: #include <bitset> 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: static const int INF = 3000; 4fd800b3a8 2011-02-23 kinaba: class ReversalChain 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: map<pair<pair<LL,LL>,int>,int> memo; 4fd800b3a8 2011-02-23 kinaba: int minReversal(string init, string goal) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL s=0, sb=0; for(int i=0; i<init.size(); ++i) if(init[i]=='1') s |= (1LL<<i), sb++; 4fd800b3a8 2011-02-23 kinaba: LL g=0, gb=0; for(int i=0; i<goal.size(); ++i) if(goal[i]=='1') g |= (1LL<<i), gb++; 4fd800b3a8 2011-02-23 kinaba: if( sb != gb ) 4fd800b3a8 2011-02-23 kinaba: return -1; 4fd800b3a8 2011-02-23 kinaba: int a = mrev(s, g, init.size()); 4fd800b3a8 2011-02-23 kinaba: return a>=INF ? -1 : a; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: int mrev(LL s, LL g, int m) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: #define mask(x) ((x) & (1LL<<(m))-1) 4fd800b3a8 2011-02-23 kinaba: #define ith(x,i) (((x)>>(i))&1) 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( mask(s) == mask(g) ) 4fd800b3a8 2011-02-23 kinaba: return 0; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: pair<pair<LL,LL>,int> key(make_pair(mask(s),mask(g)),m); 4fd800b3a8 2011-02-23 kinaba: if( memo.count(key) ) 4fd800b3a8 2011-02-23 kinaba: return memo[key]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int a = INF; 4fd800b3a8 2011-02-23 kinaba: if( ith(s,m-1)==ith(g,m-1) ) a = min(a, mrev(s, g, m-1)); 4fd800b3a8 2011-02-23 kinaba: if( ith(s,0)==ith(g,0) ) a = min(a, mrev(s>>1, g>>1, m-1)); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: s = bitrev(s,m); 4fd800b3a8 2011-02-23 kinaba: if( ith(s,m-1)==ith(g,m-1) ) a = min(a, 1+mrev(s, g, m-1)); 4fd800b3a8 2011-02-23 kinaba: if( ith(s,0)==ith(g,0) ) a = min(a, 1+mrev(s>>1, g>>1, m-1)); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: return memo[key] = a; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: LL bitrev(LL x, LL m) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL y = 0; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<m; ++i) 4fd800b3a8 2011-02-23 kinaba: y |= (((x>>i)&1) << (m-1-i)); 4fd800b3a8 2011-02-23 kinaba: return y; 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 init = "1100"; 4fd800b3a8 2011-02-23 kinaba: string goal = "0110"; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 1; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ReversalChain().minReversal(init, goal)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: string init = "111000"; 4fd800b3a8 2011-02-23 kinaba: string goal = "101010"; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 2; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ReversalChain().minReversal(init, goal)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: string init = "0"; 4fd800b3a8 2011-02-23 kinaba: string goal = "1"; 4fd800b3a8 2011-02-23 kinaba: int RetVal = -1; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ReversalChain().minReversal(init, goal)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: string init = "10101"; 4fd800b3a8 2011-02-23 kinaba: string goal = "10101"; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ReversalChain().minReversal(init, goal)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: string init = "111000111000"; 4fd800b3a8 2011-02-23 kinaba: string goal = "001100110011"; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 4; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ReversalChain().minReversal(init, goal)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<5>) { 4fd800b3a8 2011-02-23 kinaba: string init = "001000011000000111000000001111"; 4fd800b3a8 2011-02-23 kinaba: string goal = "100100100100100100100100100100"; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 9; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ReversalChain().minReversal(init, goal)); } 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: