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

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int tx[] = {0, 1, 0, -1};
int ty[] = {-1, 0, 1, 0};

static const double EPS = 1e-8;

string MakeSeq(int N) {
  string res = "";
  for (int i = 0; i < N; i++) {
    int random_variable = std::rand() % 2; // 0 or 1
    res.push_back('0' + random_variable);
  }
  return res;
}

bool isClear(const vector<vector<int>> &cards, const string &bits, int N) {
  for (int card_pos = 0; card_pos < cards.size(); card_pos++) {
    int sum = 0;
    for (int i = 0; i < cards[card_pos].size(); i++) {
      if (bits[cards[card_pos][i]] == '1') {
        sum++;
      }
    }
    if (sum < N / 8 || sum > 3 * N / 8)
      return false;
  }
  return true;
}

int main() {
  int N, K;
  srand(time(0)); // use current time as seed for random generator

  while (~scanf("%d %d", &N, &K)) {
    vector<vector<int>> cards;
    for (int card_pos = 0; card_pos < K; card_pos++) {
      vector<int> card;
      for (int i = 0; i < N / 2; i++) {
        int seq_pos;
        scanf("%d", &seq_pos);
        card.push_back(seq_pos - 1);
      }
      cards.push_back(card);
    }

    for (;;) {
      string seq = MakeSeq(N);
      if (isClear(cards, seq, N)) {
        printf("%s\\n", seq.c_str());
        break;
      }
    }
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int tx[] = {0, 1, 0, -1};
int ty[] = {-1, 0, 1, 0};

static const double EPS = 1e-8;

string MakeSeq(int N) {
  srand(time(0)); // use current time as seed for random generator

  string res = "";
  for (int i = 0; i < N; i++) {
    int random_variable = std::rand() % 2; // 0 or 1
    res.push_back('0' + random_variable);
  }
  return res;
}

bool isClear(const vector<vector<int>> &cards, const string &bits, int N) {
  for (int card_pos = 0; card_pos < cards.size(); card_pos++) {
    int sum = 0;
    for (int i = 0; i < cards[card_pos].size(); i++) {
      if (bits[cards[card_pos][i]] == '1') {
        sum++;
      }
    }
    if (sum < N / 8 || sum > 3 * N / 8)
      return false;
  }
  return true;
}

int main() {
  int N, K;
  while (~scanf("%d %d", &N, &K)) {
    vector<vector<int>> cards;
    for (int card_pos = 0; card_pos < K; card_pos++) {
      vector<int> card;
      for (int i = 0; i < N / 2; i++) {
        int seq_pos;
        scanf("%d", &seq_pos);
        card.push_back(seq_pos - 1);
      }
      cards.push_back(card);
    }

    for (;;) {
      string seq = MakeSeq(N);
      if (isClear(cards, seq, N)) {
        printf("%s\\n", seq.c_str());
        break;
      }
    }
  }
}