Problem p00448 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
#include <sstream>
// #include <fstream>

using namespace std;

int osenbei[10][10000];
bool upsidedown[10];

int count_one(int col, int size) {
  int i;
  int res = 0;

  for (i = 0; i < size; ++i) {
    if ((upsidedown[i] && osenbei[i][col] == 0) ||
        (!upsidedown[i] && osenbei[i][col] == 1)) {
      ++res;
    } // end if
  }   // end for

  return res;
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.osenbei2", "r", stdin );
  int r, c;

  while (1) {
    cin >> r >> c;

    if (r == 0 && c == 0) {
      break;
    } // end if
    memset(osenbei, 0, sizeof(osenbei));

    int i, j;

    for (i = 0; i < r; ++i) {
      for (j = 0; j < c; ++j) {
        cin >> osenbei[i][j];
      } // end for
    }   // end for

    int res = 1000000;

    // each row is all-search
    for (i = 0; i < (1 << r); ++i) {
      memset(upsidedown, false, sizeof(upsidedown));
      for (j = 0; j < r; ++j) {
        if (i & (1 << j)) {
          upsidedown[j] = true;
        } // end if
      }   // end for

      // eash col
      // count number of 1,  automatically get number of 0.
      // return min number of 1 and number of 0.
      int sum = 0;
      for (j = 0; j < c; ++j) {
        int one = count_one(j, r);
        int zero = r - one;
        sum += min(one, zero);
      } // end for
      res = min(res, sum);
    } // end for
    cout << (r * c - res) << endl;

  } // end loop

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

void disp_bit(int n, int count) {
  int i;
  for (i = 0; i < 10; ++i) {
    if (n & (1 << i)) {
      cout << '1';
    } else {
      cout << '0';
    } // end if
  }   // end for

  cout << " :" << count << endl;
}
void disp_t(vector<int> t) {
  int size = t.size();
  int i;

  for (i = 0; i < size; ++i) {
    cout << t[i] << ' ';
  } // end for
  cout << endl;
}
int itosl(int n) {
  stringstream ss;
  ss << n;
  string str = ss.str();
  int res = str.length();

  return res;
}

int min_wight(vector<int> o, vector<int> wight, int len) {
  int size = o.size();
  int res = 0;
  int i, j;

  for (j = 0; j < len; ++j) {
    int count = 0;
    vector<bool> used(size, false);
    for (i = 0; i < size; ++i) {
      if (o[i] & (1 << j)) {
        used[i] = true;
        ++count;
      }                      // end if
    }                        // end for
    if (count >= size / 2) { // adventage 1
      for (i = 0; i < size; ++i) {
        if (!used[i]) {
          res += wight[i];
        }    // end if
      }      // end if
    } else { // adventage 0
      for (i = 0; i < size; ++i) {
        if (used[i]) {
          res += wight[i];
        } // end if
      }   // end for
    }     // end if
  }       // end for

  return res;
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.osenbei", "r", stdin );

  string str = "";
  int r, c;

  while (getline(cin, str)) {
    stringstream ssn(str);
    ssn >> r >> c;

    if (r == 0 && c == 0) {
      break;
    } // end if
    vector<int> t(c, 0);

    int i, j;
    int p = 1;
    for (i = 0; i < r; ++i) {
      getline(cin, str);
      stringstream ss(str);
      for (j = 0; j < c; ++j) {
        int bit = 0;
        ss >> bit;
        if (bit == 1) {
          t[j] += p;
        } // end if
      }   // end for
      p *= 2;
    } // end for

    int mask = p - 1;
    map<int, int> pattern;
    vector<bool> used(c, false);
    for (i = 0; i < c; ++i) {
      if (used[i]) {
        continue;
      } // end if
      used[i] = true;
      ++pattern[t[i]];
      for (j = i + 1; j < c; ++j) {
        if (!used[j] && (t[i] == t[j] || t[i] == (mask ^ t[j]))) {
          ++pattern[t[i]];
          used[j] = true;
          continue;
        } // end if
      }   // end for
    }     // end for
    int size = pattern.size();
    vector<int> order(size, 0);
    vector<int> wight(size, 0);
    map<int, int>::iterator it = pattern.begin();
    for (i = 0; it != pattern.end(); ++it, ++i) {
      order[i] = (*it).first;
      wight[i] = (*it).second;
    } // end for

    int res = INT_MAX;
    for (i = 0; i < (1 << size); ++i) {
      vector<int> o = order;
      for (j = 0; j < size; ++j) {
        if (i & (1 << j)) {
          o[j] = o[j] ^ mask;
        } // end if
      }   // end for
      res = min(res, min_wight(o, wight, mask + 1));
    } // end for
    cout << (r * c - res) << endl;

  } // end loop

  return 0;
}