alexjercan/bugnet huggingface dataset:
Problem p00601 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <vector>
using namespace std;

#define LET(name, value) __typeof(value) name = value
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define FOREQ(i, a, b) for (int i = (a); i <= (int)(b); ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for (LET(i, (c).begin()); i != (c).end(); ++i)

int N, M;
int ans;
unsigned edges[32];

inline int bsf(unsigned x) {
  __asm__("bsf %1, %0;" : "=r"(x) : "r"(x));
  return x;
}

inline int bsr(unsigned x) {
  __asm__("bsr %1, %0;" : "=r"(x) : "r"(x));
  return x;
}

void minimum_dominating_set(unsigned D) {
  if (__builtin_popcount(D) >= ans) {
    return;
  }
  bool ok = true;
  for (int x = ~D & ((1 << N) - 1); x; x &= x - 1) {
    if ((edges[bsf(x)] & D) == 0) {
      ok = false;
      break;
    }
  }
  if (ok) {
    ans = __builtin_popcount(D);
  } else {
    FOR(i, D ? bsr(D) + 1 : 0, N) { minimum_dominating_set(D | (1 << i)); }
  }
}

int main() {
  while (scanf("%d%d", &N, &M), N | M) {
    REP(i, N) { edges[i] = 1 << i; }
    REP(i, M) {
      int s, t;
      scanf("%d%d", &s, &t);
      edges[s] |= 1 << t;
      edges[t] |= 1 << s;
    }
    ans = 1000;
    minimum_dominating_set(0);
    printf("%d\\n", ans);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <vector>
using namespace std;

#define LET(name, value) __typeof(value) name = value
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define FOREQ(i, a, b) for (int i = (a); i <= (int)(b); ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for (LET(i, (c).begin()); i != (c).end(); ++i)

int N, M;
unsigned edges[32];
map<unsigned, int> memo;

int ans;
int minimum_dominating_set(unsigned D) {
  if (__builtin_popcount(D) >= ans) {
    return ans;
  }
  if (memo.count(D)) {
    return memo[D];
  }
  bool ok = true;
  REP(i, N) {
    if ((edges[i] & D) == 0) {
      ok = false;
      break;
    }
  }
  if (ok) {
    return ans = memo[D] = __builtin_popcount(D);
  } else {
    int ret = 1000;
    REP(i, N) {
      if ((D & (1 << i)) == 0) {
        ret = min(ret, minimum_dominating_set(D | (1 << i)));
      }
    }
    return memo[D] = ret;
  }
}

int main() {
  while (scanf("%d%d", &N, &M), N | M) {
    memo.clear();
    REP(i, N) { edges[i] = 1 << i; }
    REP(i, M) {
      int s, t;
      scanf("%d%d", &s, &t);
      edges[s] |= 1 << t;
      edges[t] |= 1 << s;
    }
    ans = 1000;
    minimum_dominating_set(0);
    printf("%d\\n", ans);
  }
}
Problem p00601 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int n, m, G;

vector<int> t[30];
int u[30];
map<int, int> dp;

int solve(int S) {
  if (S == G)
    return 0;
  if (dp[S] > 0)
    return dp[S];

  int k;
  for (k = 0; k < n; k++) {
    if ((S >> k) & 1)
      continue;
    break;
  }

  int res = 100;
  for (int i = 0; i < (int)t[k].size(); i++) {
    int w = t[k][i];
    res = min(res, solve(S | u[w]) + 1);
  }
  return dp[S] = res;
}

int main() {
  int a, b;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;

    dp.clear();
    for (int i = 0; i < n; i++)
      t[i].clear();

    for (int i = 0; i < m; i++) {
      cin >> a >> b;
      t[a].push_back(b);
      t[b].push_back(a);
    }

    for (int i = 0; i < n; i++) {
      u[i] = (1 << i);
      t[i].push_back(i);
      for (int j = 0; j < (int)t[i].size(); j++) {
        u[i] |= (1 << t[i][j]);
      }
    }

    G = (1 << n) - 1;
    cout << solve(0) << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int n, m, G;
vector<int> t[30];
int u[30];

map<int, int> dp;

void printbit(int x) {
  int y = x;
  for (int i = 0; i < n; i++) {
    cout << x % 2;
    x /= 2;
  }
  cout << ' ' << y << endl;
}

int solve(int S) {
  if (S == G)
    return 0;
  if (dp[S] > 0)
    return dp[S];

  int k;
  for (k = 0; k < n; k++) {
    if ((S >> k) & 1)
      continue;
    break;
  }

  int res = 100;
  for (int i = 0; i < (int)t[k].size(); i++) {
    int w = t[k][i];
    res = min(res, solve(S | u[w]) + 1);
  }
  return dp[S] = res;
}

int main() {
  int a, b;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    dp.clear();
    for (int i = 0; i < m; i++) {
      cin >> a >> b;
      t[a].push_back(b);
      t[b].push_back(a);
    }

    for (int i = 0; i < n; i++) {
      u[i] = (1 << i);
      for (int j = 0; j < (int)t[i].size(); j++) {
        u[i] += (1 << t[i][j]);
      }
      // printbit(u[i]);
    }

    G = (1 << n) - 1;
    // printbit(G);
    cout << solve(0) << endl;
  }
  return 0;
}
Problem p00601 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
using namespace std;

int main() {
  int n, m, a, b, comb;
  bool g[30][30], f, flag, vis[30];

  while (scanf("%d%d", &n, &m) != EOF, n || m) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        g[i][j] = 0;
    for (int i = 0; i < m; i++) {
      scanf("%d%d", &a, &b);
      g[a][b] = g[b][a] = 1;
    }

    if (n <= 0)
      return 0;
    else {
      for (int num = 1; num <= n; num++) {
        comb = (1 << num) - 1;
        flag = 1;
        while (flag && comb < (1 << n)) {
          for (int i = 0; i < n; i++)
            vis[i] = 0;

          for (int i = 0; i < n; i++) {
            if ((comb >> i) & 1) {
              vis[i] = 1;
              for (int j = 0; j < n; j++) {
                if (g[i][j])
                  vis[j] = 1;
              }
            }
          }
          bool f = 1;
          for (int i = 0; i < n; i++)
            f &= vis[i];
          if (f) {
            flag = 0;
            printf("%d\\n", num);
            break;
          }
          int x = comb & -comb, y = comb + x;
          comb = (((comb & ~y) / x) >> 1) | y;
        }
        if (!flag)
          break;
      }
    }
  }
}

Error: WA

Fixed submission:

#include <cassert>
#include <cstdio>
#include <vector>
using namespace std;

int main() {
  int n, m, f, t;
  bool g[100][100];

  while (scanf("%d%d", &n, &m) != EOF, n || m) {
    assert(28 > n);
    /*
    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)g[i][j] = 0;
    */
    for (int i = 0; i < m; i++) {
      scanf("%d%d", &f, &t);
      // g[f][t] = g[t][f] = 1;
    }
  }
}
Problem p00601 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;

VVI E;

int n, m;
VI D;

int cnt;
VI F;
int answer;

const int inf = 1 << 24;

bool check() {
  for (int i = 0; i < n; i++) {
    if (!F[i]) {
      bool flag = true;
      for (int j = 0; j < (int)E[i].size() && flag; j++) {
        if (F[E[i][j]])
          flag = false;
      }
      if (flag)
        return false;
    }
  }
  return true;
}

void solve(int s) {
  if (s == n) {
    if (cnt < answer && check())
      answer = cnt;
    return;
  }

  F[s] = 1;
  cnt++;
  solve(s + 1);
  F[s] = 0;
  cnt--;
  solve(s + 1);
}

int main() {
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;

    F = VI(n, 0);
    E = VVI(n);

    for (int i = 0; i < m; i++) {
      int a, b;
      cin >> a >> b;
      E[a].push_back(b);
      E[b].push_back(a);
    }

    answer = n;
    cnt = 0;
    solve(0);
    cout << answer << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;

VVI E;

int n, m;
VI D;

int cnt;
VI F;
int answer;

const int inf = 1 << 24;

bool check() {
  for (int i = 0; i < n; i++) {
    if (!F[i]) {
      bool flag = true;
      for (int j = 0; j < (int)E[i].size() && flag; j++) {
        if (F[E[i][j]])
          flag = false;
      }
      if (flag)
        return false;
    }
  }
  return true;
}

void solve(int s) {
  if (s == n) {
    if (check())
      answer = cnt;
    return;
  }

  F[s] = 1;
  cnt++;
  solve(s + 1);
  F[s] = 0;
  cnt--;
  solve(s + 1);
}

int main() {
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;

    F = VI(n, 0);
    E = VVI(n);

    for (int i = 0; i < m; i++) {
      int a, b;
      cin >> a >> b;
      E[a].push_back(b);
      E[b].push_back(a);
    }

    answer = n;
    cnt = 0;
    solve(0);
    cout << answer << endl;
  }
  return 0;
}
Problem p00602 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll unsigned long long

int main() {
  map<int, int> table;
  int graph[1011];
  table[0] = 1;
  table[1] = 2;
  graph[1] = 0;
  for (int x = 2; x <= 1010; ++x) {
    table[x] = (table[x - 1] + table[x - 2]) % 1001;
    graph[x] = 0;
  }
  int v, d;
  while (scanf("%d %d", &v, &d) == 2) {
    int tot = 0;
    int fibo[v + 2];
    fibo[0] = -1;
    for (int x = 1; x <= v + 1; ++x)
      fibo[x] = table[x];
    sort(fibo, fibo + v + 1);
    for (int x = 1; x <= v; ++x) {
      for (int y = x + 1; y <= v; ++y) {
        if (abs(fibo[x] - fibo[y]) < d) {
          graph[y] = x;
        }
      }
    }
    for (int x = 1; x <= v; ++x) {
      if (graph[x] == 0)
        tot++;
      graph[x] = 0;
    }
    cout << tot << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll unsigned long long

int main() {
  map<int, int> table;
  int graph[1011];
  table[0] = 1;
  table[1] = 2;
  graph[1] = 0;
  for (int x = 2; x <= 1010; ++x) {
    table[x] = (table[x - 1] + table[x - 2]) % 1001;
    graph[x] = 0;
  }
  int v, d;
  while (scanf("%d %d", &v, &d), v || d) {
    int tot = 0;
    for (int x = 1; x <= v; ++x) {
      for (int y = x + 1; y <= v; ++y) {
        if (abs(table[x] - table[y]) < d) {
          if (graph[x] == 0 && graph[y] == 0) {
            graph[y] = x;
          } else if (graph[x] != 0) {
            graph[y] = x;
          } else {
            graph[x] = y;
          }
        }
      }
    }
    for (int x = 1; x <= v; ++x) {
      if (graph[x] == 0)
        tot++;
      graph[x] = 0;
    }
    cout << tot << endl;
  }
  return 0;
}
Problem p00602 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
const int MAX = 1001;

int V, d;
int v[MAX];

void fibo() {

  v[0] = 1, v[1] = 2;
  for (int i = 2; i < MAX; i++) {
    v[i] = (v[i - 1] + v[i - 2]) % MAX;
  }
}

int main() {

  while (cin >> V >> d) {
    fibo();
    sort(v, v + V + 1);
    int cnt = 1;
    for (int i = 2; i <= V; i++) {
      if (v[i - 1] + d <= v[i])
        cnt++;
    }

    cout << cnt << endl;
  }

  return 0;
}

Error: -6
malloc(): corrupted top size


Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 1001;

int V, d;
vector<int> v;

void fibo() {
  v.push_back(2);
  v.push_back(3);

  for (int i = 2; i <= MAX; i++) {
    v[i] = (v[i - 1] + v[i - 2]) % MAX;
  }
}

int main() {
  fibo();
  sort(v.begin(), v.end());
  while (cin >> V >> d) {
    int cnt = 0;
    for (int i = 0; i < V; i++) {
      if (abs(v[i - 1] - v[i]) <= d) {
        continue;
      }
      cnt++;
    }
    if (V == d)
      cnt++;
    cout << ++cnt << endl;
  }

  return 0;
}
Problem p00602 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <set>
#include <vector>

using namespace std;

void solve() {
  vector<int> fib;
  fib.push_back(1);
  fib.push_back(2);
  for (int i = 2; i < 1100; ++i) {
    fib.push_back((fib[i - 2] + fib[i - 1]) % 1001);
  }

  int V, d;
  while (cin >> V >> d) {
    set<int> numbers;
    for (int i = 1; i <= V; ++i) {
      numbers.insert(fib[i]);
    }
    set<int>::iterator it = numbers.begin();
    int before_num;
    int ans = 0;
    before_num = *it;
    ++it;
    while (it != numbers.end()) {
      if (*it - before_num >= d) {
        ++ans;
      }
      before_num = *it;
      ++it;
    }
    cout << ans + 1 << endl;
  }
}

int main() {
  solve();
  return (0);
}

Error: 0

Fixed submission:

#include <iostream>
#include <set>
#include <vector>

using namespace std;

void solve() {
  int V, d;
  while (cin >> V >> d) {
    if (V == 1000) {
      cout << 1 << endl;
      continue;
    }
    if (d == 1) {
      cout << V << endl;
      continue;
    }
    int f0 = 1;
    int f1 = 1;
    set<int> numbers;
    vector<int> fib;
    fib.push_back(f0);
    fib.push_back(f1);
    int count = 2;
    while (numbers.size() < V) {
      f0 = fib[count - 2];
      f1 = fib[count - 1];
      int f2 = (f0 + f1) % 1001;
      fib.push_back(f2);
      if (numbers.find(f2) == numbers.end()) {
        numbers.insert(f2);
      }
      ++count;
    }
    set<int>::iterator it = numbers.begin();
    int before_num;
    int ans = 0;
    before_num = *it;
    ++it;
    while (it != numbers.end()) {
      if (*it - before_num >= d) {
        ++ans;
      }
      before_num = *it;
      ++it;
    }
    cout << ans + 1 << endl;
  }
}

int main() {
  solve();
  return (0);
}
Problem p00603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <queue>
#include <stdio.h>
using namespace std;
int now[60];
int main() {
  int a, b;
  while (~scanf("%d%d", &a, &b)) {
    queue<int> A;
    queue<int> B;
    queue<int> C;
    for (int i = 0; i < a; i++)
      now[i] = i;
    while (b--) {
      int c;
      scanf("%d", &c);
      for (int i = 0; i < a / 2; i++)
        B.push(now[i]);
      for (int i = a / 2; i < a; i++)
        A.push(now[i]);
      while (C.size() != a) {
        for (int i = 0; i < c; i++)
          if (A.size()) {
            C.push(A.front());
            A.pop();
          }
        for (int i = 0; i < c; i++)
          if (B.size()) {
            C.push(B.front());
            B.pop();
          }
      }
      for (int i = 0; i < a; i++) {
        now[i] = C.front();
        C.pop();
      }
    }
    printf("%d\\n", now[a - 1]);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <queue>
#include <stdio.h>
using namespace std;
int now[60];
int main() {
  int a, b;
  while (~scanf("%d%d", &a, &b), a) {
    queue<int> A;
    queue<int> B;
    queue<int> C;
    for (int i = 0; i < a; i++)
      now[i] = i;
    while (b--) {
      int c;
      scanf("%d", &c);
      for (int i = 0; i < a / 2; i++)
        B.push(now[i]);
      for (int i = a / 2; i < a; i++)
        A.push(now[i]);
      while (C.size() != a) {
        for (int i = 0; i < c; i++)
          if (A.size()) {
            C.push(A.front());
            A.pop();
          }
        for (int i = 0; i < c; i++)
          if (B.size()) {
            C.push(B.front());
            B.pop();
          }
      }
      for (int i = 0; i < a; i++) {
        now[i] = C.front();
        C.pop();
      }
    }
    printf("%d\\n", now[a - 1]);
  }
}
Problem p00603 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

vector<int> RiffleShuffle(int, vector<int>);

int main() {
  int i, n, r, c;
  vector<int> data;

  while (cin >> n >> r) {
    for (i = 0; i < n; ++i)
      data.push_back(i);

    for (i = 0; i < r; ++i) {
      cin >> c;
      data = RiffleShuffle(c, data);
    }

    cout << data[data.size() - 1] << endl;
    data.clear();
  }

  return 0;
}

vector<int> RiffleShuffle(int c, vector<int> data) {
  vector<int> A, B, C;

  copy(data.begin() + data.size() / 2, data.end(), back_inserter(A));
  copy(data.begin(), data.begin() + data.size() / 2, back_inserter(B));

  while (A.size() > c || B.size() > c) {
    copy(A.begin(), A.begin() + c, back_inserter(C));
    A.erase(A.begin(), A.begin() + c);
    copy(B.begin(), B.begin() + c, back_inserter(C));
    B.erase(B.begin(), B.begin() + c);
  }

  copy(A.begin(), A.end(), back_inserter(C));
  copy(B.begin(), B.end(), back_inserter(C));

  return C;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

vector<int> RiffleShuffle(int, vector<int>);

int main() {
  int i, n, r, c;
  vector<int> data;

  while (cin >> n >> r) {
    for (i = 0; i < n; ++i)
      data.push_back(i);

    for (i = 0; i < r; ++i) {
      cin >> c;
      data = RiffleShuffle(c, data);
    }

    cout << data[data.size() - 1] << endl;
    data.clear();
  }

  return 0;
}

vector<int> RiffleShuffle(int c, vector<int> data) {
  vector<int> A, B, C;

  copy(data.begin() + data.size() / 2, data.end(), back_inserter(A));
  copy(data.begin(), data.begin() + data.size() / 2, back_inserter(B));

  copy(A.begin(), A.begin() + c, back_inserter(C));
  A.erase(A.begin(), A.begin() + c);
  copy(B.begin(), B.begin() + c, back_inserter(C));
  B.erase(B.begin(), B.begin() + c);

  copy(A.begin(), A.end(), back_inserter(C));
  copy(B.begin(), B.end(), back_inserter(C));

  return C;
}
Problem p00603 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

vector<int> shuffle(vector<int> deck, int m) {
  int n = deck.size();
  queue<int> a, b;

  for (int i = 0; i < n; ++i) {
    if (i < n / 2)
      b.push(deck[i]);
    else
      a.push(deck[i]);
  }

  vector<int> ret;
  while (!a.empty() || !b.empty()) {
    for (int i = 0; i < m; ++i) {
      if (a.empty())
        break;
      ret.push_back(a.front());
      a.pop();
    }
    for (int i = 0; i < m; ++i) {
      if (b.empty())
        break;
      ret.push_back(b.front());
      b.pop();
    }
  }

  return ret;
}

int main() {
  for (;;) {
    int n, r;
    if (!(cin >> n >> r))
      return 0;

    vector<int> deck(n);
    for (int i = 0; i < n; ++i)
      deck[i] = i;

    for (int i = 0; i < r; ++i) {
      int m;
      cin >> m;
      deck = shuffle(deck, m);
    }

    cout << deck[n - 1] << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

vector<int> shuffle(vector<int> deck, int m) {
  int n = deck.size();
  queue<int> a, b;

  for (int i = 0; i < n; ++i) {
    if (i < n / 2)
      b.push(deck[i]);
    else
      a.push(deck[i]);
  }

  vector<int> ret;
  for (int i = 0; i < m; ++i) {
    ret.push_back(a.front());
    a.pop();
  }
  for (int i = 0; i < m; ++i) {
    ret.push_back(b.front());
    b.pop();
  }

  while (!a.empty()) {
    ret.push_back(a.front());
    a.pop();
  }
  while (!b.empty()) {
    ret.push_back(b.front());
    b.pop();
  }

  return ret;
}

int main() {
  for (;;) {
    int n, r;
    if (!(cin >> n >> r))
      return 0;

    vector<int> deck(n);
    for (int i = 0; i < n; ++i)
      deck[i] = i;

    for (int i = 0; i < r; ++i) {
      int m;
      cin >> m;
      deck = shuffle(deck, m);
    }

    cout << deck[n - 1] << endl;
  }
}
Problem p00603 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)

int main() {

  int N, R;
  while (cin >> N >> R) {
    queue<int> A, B, C;
    rep(i, N) { C.push(i); }
    rep(_, R) {
      int c;
      cin >> c;
      rep(i, N) {
        if (N / 2 <= i) {
          A.push(C.front());
        } else {
          B.push(C.front());
        }
        C.pop();
      }

      for (; (!A.empty()) || (!B.empty());) {
        rep(i, c) {
          if (A.empty())
            break;
          C.push(A.front());
          A.pop();
        }
        rep(i, c) {
          if (B.empty())
            break;
          C.push(B.front());
          B.pop();
        }
      }
    }
    cout << C.back() << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)

int main() {

  int N, R;
  while (cin >> N >> R) {
    queue<int> A, B, C;
    rep(i, N) { C.push(i); }
    rep(_, R) {
      int c;
      cin >> c;
      rep(i, N) {
        if (N / 2 <= i) {
          A.push(C.front());
        } else {
          B.push(C.front());
        }
        C.pop();
      }

      for (; (!A.empty()) && (!B.empty());) {
        rep(i, c) {
          if (A.empty())
            break;
          C.push(A.front());
          A.pop();
        }
        rep(i, c) {
          if (B.empty())
            break;
          C.push(B.front());
          B.pop();
        }
      }
    }
    cout << C.back() << endl;
  }

  return 0;
}
Problem p00603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {
  int r, n;
  while (cin >> n >> r) {
    deque<int> A, B, C;
    r(i, n) C.push_back(i);
    int a[r];
    r(i, r) cin >> a[i];
    for (int i = 0; i < r; i++) {
      int c = 0;
      r(i, n / 2) B.push_back(C.front()), C.pop_front();
      while (C.size())
        A.push_back(C.front()), C.pop_front();
      while (1) {
        if (A.size() == 0 && B.size() == 0)
          break;
        ;
        if (c == 0) {
          r(j, a[i]) {
            if (A.size())
              C.push_back(A.front()), A.pop_front();
          }
          c = 1;
        } else {
          r(j, a[i]) {
            if (B.size())
              C.push_back(B.front()), B.pop_front();
          }
          c = 0;
        }
      }
    }
    cout << C.back() << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {
  int r, n;
  while (cin >> n >> r, n) {
    deque<int> A, B, C;
    r(i, n) C.push_back(i);
    int a[r];
    r(i, r) cin >> a[i];
    for (int i = 0; i < r; i++) {
      int c = 0;
      r(i, n / 2) B.push_back(C.front()), C.pop_front();
      while (C.size())
        A.push_back(C.front()), C.pop_front();
      while (1) {
        if (A.size() == 0 && B.size() == 0)
          break;
        ;
        if (c == 0) {
          if (A.size() == 0)
            i;
          else {
            r(j, a[i]) {
              if (A.size())
                C.push_back(A.front()), A.pop_front();
            }
          }
          c = 1;
        } else {
          if (B.size() == 0)
            i;
          else {
            r(j, a[i]) {
              if (B.size())
                C.push_back(B.front()), B.pop_front();
            }
          }
          c = 0;
        }
      }
    }
    cout << C.back() << endl;
  }
}