Problem p02618 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#define F first
#define S second
using namespace std;

int Q = 1e7, h1, h2, cut = 5;
int n, s, D, l, r, up, mx, cnt, mxx, xx, a[30], b[400][30], la[400], u[400],
    ans[400], o[400];
pair<int, int> c[400][30];
bool v[400][30];

int f(int p, int q1, int q2) {
  int sz = q2 - q1 + 1;
  return p * sz * (sz + 1) / 2;
}

int main() {
  int i, j, k, t, x, t1, t2;
  freopen("input.txt", "r", stdin);
  srand(time(NULL));
  cin >> D;
  for (i = 0; i < 26; i++) {
    scanf("%d", &a[i]);
  }
  for (i = 1; i <= D; i++) {
    mxx = -2e9;
    for (j = 0; j < 26; j++) {
      scanf("%d", &b[i][j]);
    }
    for (j = 0; j < 26; j++) {
      cnt = b[i][j];
      for (k = 0; k < 26; k++) {
        if (k == j)
          continue;
        cnt -= (i - o[k]) * a[k];
      }
      if (cnt > mxx) {
        xx = j;
        mxx = cnt;
      }
    }
    o[xx] = i;
    ans[i] = u[i] = xx;
  }
  //	for (i = 1; i <= D; i++) {
  //		ans[i] = u[i] = 1;
  //	}
  //	for(i = 1; i <= D; i++) {
  //		printf("%d\n", ans[i] + 1);
  //	}

  for (i = 1; i <= D; i++) {
    //		scanf ("%d", &t);
    t = u[i];
    s += b[i][t];
    la[t] = i;

    for (j = 0; j < 26; j++) {
      s -= a[j] * (i - la[j]);
    }
    v[i][t] = 1;
  }
  //	cout << s << endl;
  //	cin >> Q;
  for (i = 0; i < 26; i++) {
    v[0][i] = 1;
    v[D + 1][i] = 1;
  }
  mx = s;
  while (Q--) {
    //		scanf ("%d %d", &t1, &t2);
    t1 = rand() % D + 1;
    t2 = rand() % 26;
    if (u[t1] == t2)
      t2 = (t2 + 1) % 26;
    x = u[t1];
    s -= b[t1][x];
    v[t1][x] = 0;
    v[t1][t2] = 1;
    u[t1] = t2;
    s += b[t1][t2];
    for (l = t1 - 1; l >= 0; l--)
      if (v[l][x])
        break;
    for (r = t1 + 1; r <= D + 1; r++)
      if (v[r][x])
        break;
    s += f(a[x], l + 1, t1 - 1);
    s += f(a[x], t1 + 1, r - 1);
    s -= f(a[x], l + 1, r - 1);
    for (l = t1 - 1; l >= 0; l--)
      if (v[l][t2])
        break;
    for (r = t1 + 1; r <= D + 1; r++)
      if (v[r][t2])
        break;
    s += f(a[t2], l + 1, r - 1);
    s -= f(a[t2], l + 1, t1 - 1);
    s -= f(a[t2], t1 + 1, r - 1);
    if (s > mx) {
      up = 0;
      for (i = 1; i <= D; i++) {
        ans[i] = u[i];
      }
      mx = s;
    } else
      up++;
    if (up == cut) {
      up = 0;
      memset(v, 0, sizeof(v));
      for (i = 0; i < 26; i++) {
        v[0][i] = 1;
        v[D + 1][i] = 1;
      }
      for (i = 1; i <= D; i++) {
        u[i] = ans[i];
        v[i][ans[i]] = 1;
      }
      s = mx;
    }
  }
  for (i = 1; i <= D; i++) {
    printf("%d\n", ans[i] + 1);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#define F first
#define S second
using namespace std;

int Q = 1e7, h1, h2, cut = 5;
int n, s, D, l, r, up, mx, cnt, mxx, xx, a[30], b[400][30], la[400], u[400],
    ans[400], o[400];
pair<int, int> c[400][30];
bool v[400][30];

int f(int p, int q1, int q2) {
  int sz = q2 - q1 + 1;
  return p * sz * (sz + 1) / 2;
}

int main() {
  int i, j, k, t, x, t1, t2;
  srand(time(NULL));
  cin >> D;
  for (i = 0; i < 26; i++) {
    scanf("%d", &a[i]);
  }
  for (i = 1; i <= D; i++) {
    mxx = -2e9;
    for (j = 0; j < 26; j++) {
      scanf("%d", &b[i][j]);
    }
    for (j = 0; j < 26; j++) {
      cnt = b[i][j];
      for (k = 0; k < 26; k++) {
        if (k == j)
          continue;
        cnt -= (i - o[k]) * a[k];
      }
      if (cnt > mxx) {
        xx = j;
        mxx = cnt;
      }
    }
    o[xx] = i;
    ans[i] = u[i] = xx;
  }
  //	for (i = 1; i <= D; i++) {
  //		ans[i] = u[i] = 1;
  //	}
  //	for(i = 1; i <= D; i++) {
  //		printf("%d\n", ans[i] + 1);
  //	}

  for (i = 1; i <= D; i++) {
    //		scanf ("%d", &t);
    t = u[i];
    s += b[i][t];
    la[t] = i;

    for (j = 0; j < 26; j++) {
      s -= a[j] * (i - la[j]);
    }
    v[i][t] = 1;
  }
  //	cout << s << endl;
  //	cin >> Q;
  for (i = 0; i < 26; i++) {
    v[0][i] = 1;
    v[D + 1][i] = 1;
  }
  mx = s;
  while (Q--) {
    //		scanf ("%d %d", &t1, &t2);
    t1 = rand() % D + 1;
    t2 = rand() % 26;
    if (u[t1] == t2)
      t2 = (t2 + 1) % 26;
    x = u[t1];
    s -= b[t1][x];
    v[t1][x] = 0;
    v[t1][t2] = 1;
    u[t1] = t2;
    s += b[t1][t2];
    for (l = t1 - 1; l >= 0; l--)
      if (v[l][x])
        break;
    for (r = t1 + 1; r <= D + 1; r++)
      if (v[r][x])
        break;
    s += f(a[x], l + 1, t1 - 1);
    s += f(a[x], t1 + 1, r - 1);
    s -= f(a[x], l + 1, r - 1);
    for (l = t1 - 1; l >= 0; l--)
      if (v[l][t2])
        break;
    for (r = t1 + 1; r <= D + 1; r++)
      if (v[r][t2])
        break;
    s += f(a[t2], l + 1, r - 1);
    s -= f(a[t2], l + 1, t1 - 1);
    s -= f(a[t2], t1 + 1, r - 1);
    if (s > mx) {
      up = 0;
      for (i = 1; i <= D; i++) {
        ans[i] = u[i];
      }
      mx = s;
    } else
      up++;
    if (up == cut) {
      up = 0;
      memset(v, 0, sizeof(v));
      for (i = 0; i < 26; i++) {
        v[0][i] = 1;
        v[D + 1][i] = 1;
      }
      for (i = 1; i <= D; i++) {
        u[i] = ans[i];
        v[i][ans[i]] = 1;
      }
      s = mx;
    }
  }
  for (i = 1; i <= D; i++) {
    printf("%d\n", ans[i] + 1);
  }
  return 0;
}