be86f38991 2013-09-07 kinaba: #include <iostream> be86f38991 2013-09-07 kinaba: #include <sstream> be86f38991 2013-09-07 kinaba: #include <iomanip> be86f38991 2013-09-07 kinaba: #include <vector> be86f38991 2013-09-07 kinaba: #include <string> be86f38991 2013-09-07 kinaba: #include <map> be86f38991 2013-09-07 kinaba: #include <set> be86f38991 2013-09-07 kinaba: #include <algorithm> be86f38991 2013-09-07 kinaba: #include <numeric> be86f38991 2013-09-07 kinaba: #include <iterator> be86f38991 2013-09-07 kinaba: #include <functional> be86f38991 2013-09-07 kinaba: #include <complex> be86f38991 2013-09-07 kinaba: #include <queue> be86f38991 2013-09-07 kinaba: #include <stack> be86f38991 2013-09-07 kinaba: #include <cmath> be86f38991 2013-09-07 kinaba: #include <cassert> be86f38991 2013-09-07 kinaba: #include <tuple> be86f38991 2013-09-07 kinaba: using namespace std; be86f38991 2013-09-07 kinaba: typedef long long LL; be86f38991 2013-09-07 kinaba: typedef complex<double> CMP; be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: class FlippingBitsDiv1 { public: be86f38991 2013-09-07 kinaba: int getmin(vector <string> Sv, int M) be86f38991 2013-09-07 kinaba: { be86f38991 2013-09-07 kinaba: string S = accumulate(Sv.begin(), Sv.end(), string()); be86f38991 2013-09-07 kinaba: vector< vector<int> > Z(M); be86f38991 2013-09-07 kinaba: for(int i=0; i<S.size(); ++i) be86f38991 2013-09-07 kinaba: Z[i%M].push_back(S[i]-'0'); be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: int best = S.size(); be86f38991 2013-09-07 kinaba: if(M <= 16) be86f38991 2013-09-07 kinaba: { be86f38991 2013-09-07 kinaba: for(int mask=0; mask<(1<<M); ++mask) be86f38991 2013-09-07 kinaba: { be86f38991 2013-09-07 kinaba: vector<int> target(M); be86f38991 2013-09-07 kinaba: for(int i=0; i<M; ++i) be86f38991 2013-09-07 kinaba: target[i] = (mask & (1<<i) ? 1 : 0); be86f38991 2013-09-07 kinaba: best = min(best, solve1(Z, target, M)); be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: else be86f38991 2013-09-07 kinaba: { be86f38991 2013-09-07 kinaba: int X = Z.back().size(); be86f38991 2013-09-07 kinaba: for(int mask=0; mask<(1<<X); ++mask) be86f38991 2013-09-07 kinaba: { be86f38991 2013-09-07 kinaba: vector<int> flip_idx; be86f38991 2013-09-07 kinaba: for(int i=0; i<X; ++i) be86f38991 2013-09-07 kinaba: if(mask & (1<<i)) be86f38991 2013-09-07 kinaba: flip_idx.push_back(i); be86f38991 2013-09-07 kinaba: best = min(best, solve2(Z, flip_idx, M)); be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: return best; be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: int solve1(const vector<vector<int>>& Z, const vector<int>& T, int M) be86f38991 2013-09-07 kinaba: { be86f38991 2013-09-07 kinaba: const int X = Z.front().size(); be86f38991 2013-09-07 kinaba: vector<int> nc(X+1), fc(X+1); be86f38991 2013-09-07 kinaba: for(int y=0; y<M; ++y) be86f38991 2013-09-07 kinaba: { be86f38991 2013-09-07 kinaba: vector<int> ncy(X+1); be86f38991 2013-09-07 kinaba: vector<int> fcy(X+1); be86f38991 2013-09-07 kinaba: for(int x=0; x<X; ++x) { be86f38991 2013-09-07 kinaba: ncy[x+1] = ncy[x] + (x>=Z[y].size() ? 0 : (Z[y][x]==T[y] ? 0 : 1)); be86f38991 2013-09-07 kinaba: fcy[x+1] = fcy[x] + (x>=Z[y].size() ? 0 : (Z[y][x]==T[y] ? 1 : 0)); be86f38991 2013-09-07 kinaba: nc[x+1] += ncy[x+1]; be86f38991 2013-09-07 kinaba: fc[x+1] += fcy[x+1]; be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: int INF = 999999; be86f38991 2013-09-07 kinaba: vector<vector<int>> dp(X+1, vector<int>(2)); be86f38991 2013-09-07 kinaba: enum {N, F}; be86f38991 2013-09-07 kinaba: dp[X][N] = INF; be86f38991 2013-09-07 kinaba: dp[X][F] = -1; be86f38991 2013-09-07 kinaba: for(int x=X-1; x>=0; --x) be86f38991 2013-09-07 kinaba: { be86f38991 2013-09-07 kinaba: dp[x][N] = INF; be86f38991 2013-09-07 kinaba: dp[x][F] = INF; be86f38991 2013-09-07 kinaba: for(int q=x+1; q<=X; ++q) { be86f38991 2013-09-07 kinaba: dp[x][N] = min(dp[x][N], 1+dp[q][F]+(nc[q]-nc[x])); be86f38991 2013-09-07 kinaba: dp[x][F] = min(dp[x][F], 1+dp[q][N]+(fc[q]-fc[x])); be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: return min(dp[0][N], dp[0][F]); be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: int solve2(const vector<vector<int>>& Z, const vector<int>& flip_idx, int M) be86f38991 2013-09-07 kinaba: { be86f38991 2013-09-07 kinaba: return -178116; be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: }; be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: // BEGIN CUT HERE be86f38991 2013-09-07 kinaba: #include <ctime> be86f38991 2013-09-07 kinaba: double start_time; string timer() be86f38991 2013-09-07 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } be86f38991 2013-09-07 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) be86f38991 2013-09-07 kinaba: { os << "{ "; be86f38991 2013-09-07 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) be86f38991 2013-09-07 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } be86f38991 2013-09-07 kinaba: void verify_case(const int& Expected, const int& Received) { be86f38991 2013-09-07 kinaba: bool ok = (Expected == Received); be86f38991 2013-09-07 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; be86f38991 2013-09-07 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } be86f38991 2013-09-07 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); be86f38991 2013-09-07 kinaba: #define END verify_case(_, FlippingBitsDiv1().getmin(S, M));} be86f38991 2013-09-07 kinaba: int main(){ be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: CASE(0) be86f38991 2013-09-07 kinaba: string S_[] = {"00111000"}; be86f38991 2013-09-07 kinaba: vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); be86f38991 2013-09-07 kinaba: int M = 1; be86f38991 2013-09-07 kinaba: int _ = 2; be86f38991 2013-09-07 kinaba: END be86f38991 2013-09-07 kinaba: CASE(1) be86f38991 2013-09-07 kinaba: string S_[] = {"101100001101"}; be86f38991 2013-09-07 kinaba: vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); be86f38991 2013-09-07 kinaba: int M = 3; be86f38991 2013-09-07 kinaba: int _ = 2; be86f38991 2013-09-07 kinaba: END be86f38991 2013-09-07 kinaba: CASE(2) be86f38991 2013-09-07 kinaba: string S_[] = {"11111111"}; be86f38991 2013-09-07 kinaba: vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); be86f38991 2013-09-07 kinaba: int M = 4; be86f38991 2013-09-07 kinaba: int _ = 0; be86f38991 2013-09-07 kinaba: END be86f38991 2013-09-07 kinaba: CASE(3) be86f38991 2013-09-07 kinaba: string S_[] = {"1101001000"}; be86f38991 2013-09-07 kinaba: vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); be86f38991 2013-09-07 kinaba: int M = 8; be86f38991 2013-09-07 kinaba: int _ = 1; be86f38991 2013-09-07 kinaba: END be86f38991 2013-09-07 kinaba: CASE(4) be86f38991 2013-09-07 kinaba: string S_[] = {"1","10","11","100","101","110"}; be86f38991 2013-09-07 kinaba: vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); be86f38991 2013-09-07 kinaba: int M = 5; be86f38991 2013-09-07 kinaba: int _ = 4; be86f38991 2013-09-07 kinaba: END be86f38991 2013-09-07 kinaba: CASE(5) be86f38991 2013-09-07 kinaba: string S_[] = {"1001011000101010001111111110111011011100110001001"}; be86f38991 2013-09-07 kinaba: vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); be86f38991 2013-09-07 kinaba: int M = 2; be86f38991 2013-09-07 kinaba: int _ = 16; be86f38991 2013-09-07 kinaba: END be86f38991 2013-09-07 kinaba: /* be86f38991 2013-09-07 kinaba: CASE(6) be86f38991 2013-09-07 kinaba: string S_[] = ; be86f38991 2013-09-07 kinaba: vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); be86f38991 2013-09-07 kinaba: int M = ; be86f38991 2013-09-07 kinaba: int _ = ; be86f38991 2013-09-07 kinaba: END be86f38991 2013-09-07 kinaba: CASE(7) be86f38991 2013-09-07 kinaba: string S_[] = ; be86f38991 2013-09-07 kinaba: vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); be86f38991 2013-09-07 kinaba: int M = ; be86f38991 2013-09-07 kinaba: int _ = ; be86f38991 2013-09-07 kinaba: END be86f38991 2013-09-07 kinaba: */ be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: // END CUT HERE