alexjercan/bugnet huggingface dataset:
Problem p00463 (C++) — buggy submission, verdict: Runtime Error

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

typedef pair<int, int> P;

int N, M, H, K;
int S[2010];
vector<P> V[2010];
int A[100010], B[100010];
int R[10010], R2[10010];
int HS[2010][2010], TS[2010][2010];

int f(int x, int h, int s) {
  auto it = lower_bound(V[x].begin(), V[x].end(), P(h + 1, 0));
  if (it == V[x].end())
    return x;
  P p = *it;
  int d = p.second, nh = p.first;
  HS[x][nh] = s;
  return TS[d][nh] = f(d, nh, s);
}

int main() {
  while (cin >> N >> M >> H >> K) {
    if (N == 0 && M == 0 && H == 0 && K == 0)
      break;
    for (int i = 1; i <= N; i++)
      cin >> S[i];
    for (int i = 0; i < M; i++)
      A[i] = 0, B[i] = 0;
    for (int i = 0; i < M; i++) {
      cin >> A[i] >> B[i];
    }
    for (int i = 1; i <= N; i++)
      V[i].clear();
    for (int i = 0; i < M; i++) {
      V[A[i]].push_back(P(B[i], A[i] + 1));
      V[A[i] + 1].push_back(P(B[i], A[i]));
    }
    for (int i = 1; i <= N; i++)
      sort(V[i].begin(), V[i].end());
    for (int i = 1; i <= N; i++)
      R[i] = f(i, 0, i);
    int m = 0;
    for (int i = 1; i <= K; i++)
      m += S[R[i]];

    for (int i = 0; i < M; i++) {
      int a = A[i], b = B[i];
      for (int j = 1; j <= N; j++)
        R2[j] = R[j];
      R2[HS[a][b]] = TS[a][b];
      R2[HS[a + 1][b]] = TS[a + 1][b];
      int sum = 0;
      for (int j = 1; j <= K; j++)
        sum += S[R2[j]];
      m = min(m, sum);
    }
    cout << m << "\\n";
  }
  return 0;
}

Error: 0

Fixed submission:

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

typedef pair<int, int> P;

int N, M, H, K;
int S[1010];
vector<P> V[1010];
int A[1010], B[1010];
int R[1010], R2[1010];
int HS[1010][1010], TS[1010][1010];

int f(int x, int h, int s) {
  auto it = lower_bound(V[x].begin(), V[x].end(), P(h + 1, 0));
  if (it == V[x].end())
    return x;
  P p = *it;
  int d = p.second, nh = p.first;
  HS[x][nh] = s;
  return TS[d][nh] = f(d, nh, s);
}

int main() {
  while (cin >> N >> M >> H >> K) {
    if (N == 0 && M == 0 && H == 0 && K == 0)
      break;
    for (int i = 1; i <= N; i++) {
      cin >> S[i];
      V[i].clear();
    }
    for (int i = 0; i < M; i++) {
      cin >> A[i] >> B[i];
      V[A[i]].push_back(P(B[i], A[i] + 1));
      V[A[i] + 1].push_back(P(B[i], A[i]));
    }
    for (int i = 1; i <= N; i++)
      sort(V[i].begin(), V[i].end());
    for (int i = 1; i <= N; i++)
      R[i] = f(i, 0, i);
    int m = 0;
    for (int i = 1; i <= K; i++)
      m += S[R[i]];

    for (int i = 0; i < M; i++) {
      for (int i = 1; i <= N; i++)
        R2[i] = R[i];
      int a = A[i], b = B[i];
      R2[HS[a][b]] = TS[a][b];
      R2[HS[a + 1][b]] = TS[a + 1][b];
      int sum = 0;
      for (int i = 1; i <= K; i++)
        sum += S[R2[i]];
      m = min(m, sum);
    }
    cout << m << "\\n";
  }
  return 0;
}
Problem p00463 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define each(itr, c)                                                           \\
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second

typedef pair<int, int> pi;

int main() {
  int n, m, h, k;
  while (scanf(" %d %d %d %d", &n, &m, &h, &k), n) {
    vector<int> s(n);
    rep(i, n) scanf(" %d", &s[i]);

    vector<pi> e(m);
    rep(i, m) {
      scanf(" %d %d", &e[i].se, &e[i].fi);
      --e[i].se;
    }
    sort(all(e));

    vector<int> f(n);
    rep(i, n) f[i] = i;

    vector<pi> sw(m);
    rep(i, m) {
      int a = e[i].se;

      sw[i] = pi(f[a], f[a + 1]);
      swap(f[a], f[a + 1]);
    }

    vector<int> F(n);
    rep(i, n) F[f[i]] = i;

    int sum = 0;
    rep(i, k) sum += s[F[i]];

    int ans = sum;
    rep(i, m) {
      int a = sw[i].fi, b = sw[i].se;
      if (a > b)
        swap(a, b);

      if (a < k && k <= b)
        ans = min(ans, sum - s[F[a]] + s[F[b]]);
    }

    printf("%d\\n", ans);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define each(itr, c)                                                           \\
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second

typedef pair<int, int> pi;

int main() {
  int n, m, h, k;
  while (scanf(" %d %d %d %d", &n, &m, &h, &k), n) {
    vector<int> s(n);
    rep(i, n) scanf(" %d", &s[i]);

    vector<pi> e(m);
    rep(i, m) {
      scanf(" %d %d", &e[i].se, &e[i].fi);
      --e[i].fi;
    }
    sort(all(e));

    vector<int> f(n);
    rep(i, n) f[i] = i;

    vector<pi> sw(m);
    rep(i, m) {
      int a = e[i].se;

      sw[i] = pi(f[a], f[a + 1]);
      swap(f[a], f[a + 1]);
    }

    int sum = 0;
    rep(i, k) sum += s[f[i]];

    int ans = sum;
    rep(i, m) {
      int a = sw[i].fi, b = sw[i].se;
      if (a > b)
        swap(a, b);

      if (a < k && k <= b)
        ans = min(ans, sum - s[f[a]] + s[f[b]]);
    }

    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00463 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

int s[1000], d[1000], y[1000];
P p[100000];
int main() {
  int n, m, h, k;
  while (scanf("%d%d%d%d", &n, &m, &h, &k), n) {
    rep(i, n) scanf("%d", &s[i]);
    rep(i, m) {
      int a, b;
      scanf("%d%d", &a, &b);
      p[i] = {b, a};
    }
    sort(p, p + m);
    iota(d, d + n, 0);
    rep(i, m) {
      swap(d[p[i].second - 1], d[p[i].second]);
      p[i] = {d[p[i].second - 1], d[p[i].second]};
      if (p[i].first > p[i].second)
        swap(p[i].first, p[i].second);
    }
    rep(i, n) y[d[i]] = s[i];
    int M = accumulate(y, y + k, 0);
    int S = M;
    rep(i, m) {
      if (p[i].first < k && p[i].second >= k)
        M = min(M, S + y[p[i].second] - y[p[i].first]);
    }
    printf("%d\\n", M);
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

int s[1000], d[1000], y[1000];
P p[100000];
int main() {
  int n, m, h, k;
  while (scanf("%d%d%d%d", &n, &m, &h, &k), n) {
    rep(i, n) scanf("%d", &s[i]);
    rep(i, m) {
      int a, b;
      scanf("%d%d", &a, &b);
      p[i] = {b, a};
    }
    sort(p, p + m);
    iota(d, d + m, 0);
    rep(i, m) {
      swap(d[p[i].second - 1], d[p[i].second]);
      p[i] = {d[p[i].second - 1], d[p[i].second]};
      if (p[i].first > p[i].second)
        swap(p[i].first, p[i].second);
    }
    rep(i, n) y[d[i]] = s[i];
    int M = accumulate(y, y + k, 0);
    int S = M;
    rep(i, m) {
      if (p[i].first < k && p[i].second >= k)
        M = min(M, S + y[p[i].second] - y[p[i].first]);
    }
    printf("%d\\n", M);
  }
}
Problem p00463 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
typedef pair<int, int> P;
int main() {
  int n, m, h, k;
  for (;;) {
    scanf("%d%d%d%d", &n, &m, &h, &k);
    if (n == 0 && m == 0 && h == 0 && k == 0)
      break;
    vector<int> scs(n);
    for (int i = 0; i < n; i++)
      scanf("%d", &scs[i]);
    vector<vector<bool>> right(n - 1, vector<bool>(h, false));
    for (int i = 0; i < m; i++) {
      int a, b;
      scanf("%d%d", &a, &b);
      right[a - 1][b] = true;
    }
    vector<P> toris;
    vector<int> nown, prev(n);
    for (int i = 0; i < n; i++)
      prev[i] = i;
    for (int i = 1; i < h; i++) {
      nown = vector<int>(n, -1);
      for (int j = 0; j < n - 1; j++) {
        if (right[j][i]) {
          nown[j] = prev[j + 1];
          nown[j + 1] = prev[j];
          toris.push_back(P(nown[j], nown[j + 1]));
        }
      }
      for (int j = 0; j < n; j++)
        if (nown[j] == -1)
          nown[j] = prev[j];
      prev = nown;
    }
    int nor = 0;
    vector<int> ftol(n);
    for (int i = 0; i < n; i++) {
      if (nown[i] < k)
        nor += scs[i];
      ftol[nown[i]] = i;
    }
    int mi = nor;
    for (int i = 0; i < toris.size(); i++) {
      int a = toris[i].first, b = toris[i].second;
      if (a < k && b >= k)
        mi = min(mi, nor - (scs[ftol[a]] - scs[ftol[b]]));
      if (a >= k && b < k)
        mi = min(mi, nor - (scs[ftol[b]] - scs[ftol[a]]));
    }
    printf("%d\\n", mi);
  }
}

Error: TLE

Fixed submission:

#include <climits>
#include <cstdio>
#include <vector>
using namespace std;
typedef pair<int, int> P;
int main() {
  int n, m, h, k;
  for (;;) {
    scanf("%d%d%d%d", &n, &m, &h, &k);
    if (n == 0 && m == 0 && h == 0 && k == 0)
      break;
    vector<int> scs(n);
    for (int i = 0; i < n; i++)
      scanf("%d", &scs[i]);
    vector<vector<bool>> left(n, vector<bool>(h, false)),
        right(n, vector<bool>(h, false));
    vector<P> yokos(m);
    int a, b;
    for (int i = 0; i < m; i++) {
      scanf("%d%d", &a, &b);
      right[a - 1][b] = left[a][b] = true;
      yokos[i] = P(a, b);
    }
    int mi = 0;
    for (int i = 0; i < k; i++) {
      int now = i;
      for (int j = 0; j < h; j++) {
        if (right[now][j])
          now++;
        else if (left[now][j])
          now--;
      }
      mi += scs[now];
    }
    for (int i = 0; i < m; i++) {
      int x = yokos[i].first, y = yokos[i].second;
      right[x - 1][y] = left[x][y] = false;
      int sum = 0;
      for (int j = 0; j < k; j++) {
        int now = j;
        for (int l = 0; l < h; l++) {
          if (right[now][l])
            now++;
          else if (left[now][l])
            now--;
        }
        sum += scs[now];
      }
      mi = min(mi, sum);
      right[x - 1][y] = left[x][y] = true;
    }
    printf("%d\\n", mi);
  }
}
Problem p00463 (C++) — buggy submission, verdict: Time Limit Exceeded

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

struct bar {
  int a, b, c, d;

  void set1(int x, int y) {
    a = x;
    b = y;
  }
  void set2(int z, int w) {
    c = z;
    d = w;
  }
};

bool than(const bar x, const bar y) { return x.b < y.b; }

int main() {
  int n, m, h, k;
  int s[1000], rs[1000], r[1000];
  int max, ans;
  int x, y;
  vector<bar> p;
  bar tmp;

  while (1) {
    cin >> n >> m >> h >> k;
    if (!n && !m && !h && !k)
      break;

    for (int i = 0; i < n; i++) {
      cin >> s[i];
      r[i] = i;
    }

    p.clear();
    for (int i = 0; i < m; i++) {
      cin >> x >> y;
      tmp.set1(x, y);
      p.push_back(tmp);
    }

    sort(p.begin(), p.end(), than);

    for (int i = 0; i < m; i++) {
      p[i].set2(r[p[i].a - 1], r[p[i].a]);
      swap(r[p[i].a - 1], r[p[i].a]);
    }

    for (int i = 0; i < n; i++)
      rs[r[i]] = s[i];

    ans = 0;
    for (int i = 0; i < k; i++)
      ans += rs[i];

    max = 0;
    for (int i = 0; i < m; i++) {
      if (p[i].c < k && p[i].d >= k) {
        if (rs[p[i].c] > rs[p[i].d]) {
          if (max < rs[p[i].c] - rs[p[i].d])
            max = rs[p[i].c] - rs[p[i].d];
        }
      }
      if (p[i].d < k && p[i].c >= k) {
        if (rs[p[i].d] > rs[p[i].c]) {
          if (max < rs[p[i].d] - rs[p[i].c])
            max = rs[p[i].d] - rs[p[i].c];
        }
      }
    }

    cout << ans - max << endl;
  }
}

Error: TLE

Fixed submission:

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

class bar {
public:
  int a, b, c, d;

  bar() {
    a = 0;
    b = 0;
  }
  bar(int x, int y) {
    a = x;
    b = y;
  }
  void set(int x, int y, int z, int w) {
    a = x, b = y;
    c = z, d = w;
  }
};

int main() {
  int n, m, h, k;
  int s[1000];
  int a, b;
  bar tmp;
  vector<bar> per;
  int r[1000];
  int rs[1000], ans;
  int max;

  while (1) {
    cin >> n >> m >> h >> k;
    if (!n && !m && !h && !k)
      break;

    for (int i = 0; i < n; i++) {
      r[i] = i;
      cin >> s[i];
    }

    per.clear();
    for (int i = 0; i < m; i++) {
      cin >> a >> b;
      tmp.set(a, b, -1, -1);
      per.push_back(tmp);
    }

    for (int i = 0; i < m; i++) {
      for (int j = i + 1; j < m; j++) {
        if (per[i].b > per[j].b)
          swap(per[i], per[j]);
      }
    }

    for (int i = 0; i < m; i++) {
      per[i].set(per[i].a, per[i].b, r[per[i].a - 1], r[per[i].a]);
      swap(r[per[i].a - 1], r[per[i].a]);
    }

    for (int i = 0; i < n; i++)
      rs[r[i]] = s[i];

    ans = 0;
    for (int i = 0; i < k; i++)
      ans += rs[i];

    max = 0;
    for (int i = 0; i < m; i++) {
      if (per[i].c < k && per[i].d >= k) {
        if (rs[per[i].c] > rs[per[i].d]) {
          if (max < rs[per[i].c] - rs[per[i].d])
            max = rs[per[i].c] - rs[per[i].d];
        }
      }
      if (per[i].c >= k && per[i].d < k) {
        if (rs[per[i].c] < rs[per[i].d]) {
          if (max < rs[per[i].d] - rs[per[i].c])
            max = rs[per[i].d] - rs[per[i].c];
        }
      }
    }
    cout << ans - max << endl;
  }
}
Problem p00464 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

#define MAX_N 2000

long long x[MAX_N][MAX_N];
long long dp[MAX_N][MAX_N];
int h, w, n, cx, cy;
int main() {
  while (true) {
    memset(dp, 0, sizeof(dp));
    cin >> h >> w >> n;
    if (h == 0 && w == 0 && n == 0) {
      break;
    }
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        scanf("%d", &x[i][j]);
      }
    }
    dp[1][1] = n - 1;
    for (int i = 2; i <= h + w; i++) {
      for (int j = 1; j <= h; j++) {
        int k = i - j;
        if (k >= 1 && k <= w) {
          if (x[j][k] == 0) {
            dp[j][k + 1] += dp[j][k] / 2;
            dp[j + 1][k] += (dp[j][k] + 1) / 2;
          } else {
            dp[j][k + 1] += (dp[j][k] + 1) / 2;
            dp[j + 1][k] += dp[j][k] / 2;
          }
        }
      }
    }
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        x[i][j] += dp[i][j];
        x[i][j] %= 2;
      }
    }
    cx = 1;
    cy = 1;
    while (cx <= w && cy <= h) {
      if (x[cy][cx] == 0) {
        cy++;
      } else {
        cx++;
      }
    }
    cout << cy << ' ' << cx << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

#define MAX_N 2000

long long x[MAX_N][MAX_N];
long long dp[MAX_N][MAX_N];
int h, w, n, cx, cy;
int main() {
  while (true) {
    memset(dp, 0, sizeof(dp));
    cin >> h >> w >> n;
    if (h == 0 && w == 0 && n == 0) {
      break;
    }
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        scanf("%d", x[i][j]);
      }
    }
    dp[1][1] = n - 1;
    for (int i = 2; i <= h + w; i++) {
      for (int j = 1; j <= h; j++) {
        int k = i - j;
        if (k >= 1 && k <= w) {
          if (x[j][k] == 0) {
            dp[j][k + 1] += dp[j][k] / 2;
            dp[j + 1][k] += (dp[j][k] + 1) / 2;
          } else {
            dp[j][k + 1] += (dp[j][k] + 1) / 2;
            dp[j + 1][k] += dp[j][k] / 2;
          }
        }
      }
    }
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        x[i][j] += dp[i][j];
        x[i][j] %= 2;
      }
    }
    cx = 1;
    cy = 1;
    while (cx <= w && cy <= h) {
      if (x[cy][cx] == 0) {
        cy++;
      } else {
        cx++;
      }
    }
    cout << cy << ' ' << cx << endl;
  }
  return 0;
}
Problem p00464 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <cstring>
#include <iostream>
using namespace std;

int main() {
  int h, w, n;
  int maps[1001][1001];
  int cnts[1001][1001];
  for (; cin >> h >> w >> n, h;) {
    memset(cnts, 0, sizeof(cnts));
    cnts[0][0] = n - 1;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        cin >> maps[i][j];
        if (i > 0) {
          cnts[i][j] += cnts[i - 1][j] / 2;
          if (maps[i - 1][j] == 0)
            cnts[i][j] += (cnts[i - 1][j] % 2);
        }
        if (j > 0) {
          cnts[i][j] += cnts[i][j - 1] / 2;
          if (maps[i][j - 1] == 1)
            cnts[i][j] += (cnts[i][j - 1] % 2);
        }
      }
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        //   cout<<cnts[i][j]<<" ";
        if (maps[i][j] == 0 && (cnts[i][j] % 2) != 0)
          maps[i][j] = 1;
        else if (maps[i][j] == 1 && (cnts[i][j] % 2) != 0)
          maps[i][j] = 0;
      }
    int i = 0;
    int j = 0;
    while (true) {
      if (i >= h || j >= w) {
        cout << i + 1 << " " << j + 1 << endl;
        break;
      }
      if (maps[i][j] == 0)
        i++;
      else
        j++;
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int h, w, n;
int maps[1001][1001];
void change(int i, int j, int cnt) {
  // cout<<i<<" "<<j<<" "<<cnt<<endl;
  if (i >= h || j >= w || cnt <= 0)
    return;

  if (maps[i][j] == 0) {
    change(i + 1, j, (cnt / 2) + (cnt % 2));
    change(i, j + 1, (cnt / 2));
    if ((cnt % 2) != 0)
      maps[i][j] = 1;
  } else {
    change(i, j + 1, (cnt / 2) + (cnt % 2));
    change(i + 1, j, (cnt / 2));
    if ((cnt % 2) != 0)
      maps[i][j] = 0;
  }

  //  int cnt1=(cnt/2)+(cnt%2);
  // int cnt2=(cnt/2);
}
void print(int i, int j) {
  if (i >= h || j >= w) {
    cout << i + 1 << " " << j + 1 << endl;
    return;
  }
  if (maps[i][j] == 0)
    print(i + 1, j);
  else
    print(i, j + 1);
}

int main() {
  for (; cin >> h >> w >> n, h;) {
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> maps[i][j];
    change(0, 0, n - 1);
    print(0, 0);
  }
}
Problem p00464 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
int w, h, n;
int num[1005][1005];
int dp[1005][1005] = {};
int main() {
  while (1) {
    scanf("%d %d %d", &h, &w, &n);
    if (h + w + n == 0)
      break;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        scanf("%d", &num[i][j]);
      }
    }
    memset(dp, 0, sizeof(dp));
    dp[1][1] = n - 1;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        if (i != 1) {
          if (dp[i - 1][j] % 2 == 0) {
            dp[i][j] += dp[i - 1][j] / 2;
          } else {
            if (num[i - 1][j] == 0) {
              dp[i][j] += (dp[i - 1][j] + 1) / 2;
            } else {
              dp[i][j] += (dp[i - 1][j] - 1) / 2;
            }
          }
        }
        if (j != 1) {
          if (dp[i][j - 1] % 2 == 0) {
            dp[i][j] += dp[i][j - 1] / 2;
          } else {
            if (num[i][j - 1] == 1) {
              dp[i][j] += (dp[i][j - 1] + 1) / 2;
            } else {
              dp[i][j] += (dp[i][j - 1] - 1) / 2;
            }
          }
        }
      }
    }
    int i = 1, j = 1;
    while (i != h + 1 && j != w + 1) {
      if (dp[i][j] % 2 == 1) {
        if (num[i][j] == 0) {
          j++;
        } else {
          i++;
        }
      } else {
        if (num[i][j] == 0) {
          i++;
        } else {
          j++;
        }
      }
    }
    printf("%d %d\\n", i, j);
  }
}

Error: WA

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
int num[1005][1005], h, w, n;
pair<int, int> p;
int main() {
  while (1) {
    scanf("%d %d %d", &h, &w, &n);
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        scanf("%d", &num[j][i]);
      }
    }
    int y = 1, s = 1, cou = 0;
    while (1) {
      if (num[y][s]) {
        y++;
        num[y][s] = (num[y][s] + 1) % 2;
        if (y == w + 1) {
          cou++;
          p = make_pair(y, s);
        }
      } else {
        num[y][s] = (num[y][s] + 1) % 2;
        s++;
        if (y == h + 1) {
          cou++;
          p = make_pair(y, s);
        }
      }
      if (cou == n) {
        cout << p.first << " " << p.second << endl;
        return 0;
      }
      if (p.first == 1 && p.second == h) {
        cout << p.first << " " << p.second << endl;
        return 0;
      }
      if (p.first == w && p.second == 1) {
        cout << p.first << " " << p.second << endl;
        return 0;
      }
    }
  }
}
Problem p00464 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int d[3005][3005];
bool e[3005][3005];
int a, b, c;
signed main() {
  while (cin >> a >> b >> c, a | b | c) {
    memset(d, 0, sizeof(d));
    memset(e, false, sizeof(e));
    for (int f = 0; f < a; f++) {
      for (int g = 0; g < b; g++) {
        scanf("%d", &e[f][g]);
      }
    }
    d[0][0] = c - 1;
    for (int h = 0; h < a + b - 1; h++) {
      for (int i = 0; i <= h; i++) {
        int x = i, y = h - i;
        if (d[x][y] & 1) {
          if (e[x][y]) {
            d[x + 1][y] += d[x][y] / 2;
            d[x][y + 1] += d[x][y] / 2 + 1;
          } else {
            d[x + 1][y] += d[x][y] / 2 + 1;
            d[x][y + 1] += d[x][y] / 2;
          }
        } else {
          d[x + 1][y] += d[x][y] / 2;
          d[x][y + 1] += d[x][y] / 2;
        }
        e[x][y] = (e[x][y] + d[x][y]) & 1;
      }
    }
    int x = 0, y = 0;
    while (1) {
      if (x == a || y == b) {
        cout << x + 1 << " " << y + 1 << endl;
        break;
      }
      if (e[x][y])
        y++;
      else
        x++;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int d[1005][1005];
bool e[1005][1005];
int a, b, c;
signed main() {
  while (cin >> a >> b >> c, a | b | c) {
    memset(d, 0, sizeof(d));
    memset(e, false, sizeof(e));
    for (int f = 0; f < a; f++) {
      for (int g = 0; g < b; g++) {
        scanf("%d", &e[f][g]);
      }
    }
    d[0][0] = c - 1;
    for (int h = 0; h < a + b - 1; h++) {
      for (int i = 0; i <= h; i++) {
        int x = i, y = h - i;
        if (d[x][y] & 1) {
          if (e[x][y]) {
            d[x + 1][y] += d[x][y] / 2;
            d[x][y + 1] += d[x][y] / 2 + 1;
          } else {
            d[x + 1][y] += d[x][y] / 2 + 1;
            d[x][y + 1] += d[x][y] / 2;
          }
        } else {
          d[x + 1][y] += d[x][y] / 2;
          d[x][y + 1] += d[x][y] / 2;
        }
        e[x][y] = (e[x][y] + d[x][y]) & 1;
      }
    }
    int x = 0, y = 0;
    while (1) {
      if (x == a || y == b) {
        cout << x + 1 << " " << y + 1 << endl;
        break;
      }
      if (e[x][y])
        y++;
      else
        x++;
    }
  }
}