Artifact Content
Not logged in

Artifact ea7a7045ff580752863e28e32e69eb3ee59bb32e


#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 <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class FlippingBitsDiv1 { public:
	int getmin(vector <string> Sv, int M)
	{
		string S = accumulate(Sv.begin(), Sv.end(), string());
		vector< vector<int> > Z(M);
		for(int i=0; i<S.size(); ++i)
			Z[i%M].push_back(S[i]-'0');

		int best = S.size();
		if(M <= 16)
		{
			for(int mask=0; mask<(1<<M); ++mask)
			{
				vector<int> target(M);
				for(int i=0; i<M; ++i)
					target[i] = (mask & (1<<i) ? 1 : 0);
				best = min(best, solve1(Z, target, M));
			}
		}
		else
		{
			int X = Z.back().size();
			for(int mask=0; mask<(1<<X); ++mask)
			{
				vector<int> flip_idx;
				for(int i=0; i<X; ++i)
					if(mask & (1<<i))
						flip_idx.push_back(i);
				best = min(best, solve2(Z, flip_idx, M));
			}
		}
		return best;
	}

	int solve1(const vector<vector<int>>& Z, const vector<int>& T, int M)
	{
		const int X = Z.front().size();
		vector<int> nc(X+1), fc(X+1);
		for(int y=0; y<M; ++y)
		{
			vector<int> ncy(X+1);
			vector<int> fcy(X+1);
			for(int x=0; x<X; ++x) {
				ncy[x+1] = ncy[x] + (x>=Z[y].size() ? 0 : (Z[y][x]==T[y] ? 0 : 1));
				fcy[x+1] = fcy[x] + (x>=Z[y].size() ? 0 : (Z[y][x]==T[y] ? 1 : 0));
				nc[x+1] += ncy[x+1];
				fc[x+1] += fcy[x+1];
			}
		}

		int INF = 999999;
		vector<vector<int>> dp(X+1, vector<int>(2));
		enum {N, F};
		dp[X][N] = INF;
		dp[X][F] = -1;
		for(int x=X-1; x>=0; --x)
		{
			dp[x][N] = INF;
			dp[x][F] = INF;
			for(int q=x+1; q<=X; ++q) {
				dp[x][N] = min(dp[x][N], 1+dp[q][F]+(nc[q]-nc[x]));
				dp[x][F] = min(dp[x][F], 1+dp[q][N]+(fc[q]-fc[x]));
			}
		}
		return min(dp[0][N], dp[0][F]);
	}

	int solve2(const vector<vector<int>>& Z, const vector<int>& flip_idx, int M)
	{
		return -178116;
	}
};

// 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(_, FlippingBitsDiv1().getmin(S, M));}
int main(){

CASE(0)
	string S_[] = {"00111000"};
	  vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 
	int M = 1; 
	int _ = 2; 
END
CASE(1)
	string S_[] = {"101100001101"};
	  vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 
	int M = 3; 
	int _ = 2; 
END
CASE(2)
	string S_[] = {"11111111"};
	  vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 
	int M = 4; 
	int _ = 0; 
END
CASE(3)
	string S_[] = {"1101001000"};
	  vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 
	int M = 8; 
	int _ = 1; 
END
CASE(4)
	string S_[] = {"1","10","11","100","101","110"};
	  vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 
	int M = 5; 
	int _ = 4; 
END
CASE(5)
	string S_[] = {"1001011000101010001111111110111011011100110001001"};
	  vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 
	int M = 2; 
	int _ = 16; 
END
	/*
CASE(6)
	string S_[] = ;
	  vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 
	int M = ; 
	int _ = ; 
END
CASE(7)
	string S_[] = ;
	  vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 
	int M = ; 
	int _ = ; 
END
	*/
}
// END CUT HERE