Problem p02618 (C++) — buggy submission, verdict: Runtime Error

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

long long s[400][30];
const int Shurui = 26;    // 今回は26種類
int Date;                 // 今回は1年間なので365日
double Seigenjikan = 1.8; // 実行時間

int main() {
  clock_t start = clock();
  cin >> Date;
  vector<int> c(Shurui);
  vector<int> last(Date, 0); // 前回の日にち
  vector<int> t(Date, 0);    // 開催する順番
  for (int i = 0; i < Date; i++)
    t[i] = i % Shurui; // とりあえずの値
  long long wa = 0;
  for (int i = 0; i < Shurui; i++) {
    cin >> c[i];
    wa += c[i];
  }
  for (int i = 0; i < Date; i++) {
    for (int j = 0; j < Shurui; j++) {
      cin >> s[i][j];
    }
  }
  long long kotae = 0;
  long long hikukazu = 0;
  long long atai = 0;
  for (int i = 0; i < Date; i++) {
    atai = 0;
    atai = s[i][t[i]];
    hikukazu += wa - c[t[i]] * (i + 1 - last[t[i]]);
    last[t[i]] = i + 1;
    kotae += atai - hikukazu;
  }
  clock_t end = clock();
  double time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0;
  while (time < Seigenjikan) {
    int d = rand() % Date;
    int q = rand() % Shurui;
    int old = t[d];
    t[d] = q;
    long long new_kotae = 0;
    hikukazu = 0;
    for (int i = 0; i < Date; i++) {
      atai = 0;
      atai = s[i][t[i]];
      hikukazu += wa - c[t[i]] * (i + 1 - last[t[i]]);
      last[t[i]] = i + 1;
      new_kotae += atai - hikukazu;
    }
    if (new_kotae < kotae) {
      t[d] = old;
    } else {
      kotae = new_kotae;
    }
    end = clock();
    time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0;
  }
  for (int i = 0; i < Date; i++) {
    cout << t[i] + 1 << endl;
  }
  return 0;
}

Error: -6
double free or corruption (out)


Fixed submission:

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

long long s[400][30];
const int Shurui = 26;    // 今回は26種類
int Date;                 // 今回は1年間なので365日
double Seigenjikan = 1.8; // 実行時間

int main() {
  clock_t start = clock();
  cin >> Date;
  vector<int> c(Shurui);
  vector<int> last(Shurui, 0); // 前回の日にち
  vector<int> t(Date, 0);      // 開催する順番
  for (int i = 0; i < Date; i++)
    t[i] = i % Shurui; // とりあえずの値
  long long wa = 0;
  for (int i = 0; i < Shurui; i++) {
    cin >> c[i];
    wa += c[i];
  }
  for (int i = 0; i < Date; i++) {
    for (int j = 0; j < Shurui; j++) {
      cin >> s[i][j];
    }
  }
  long long kotae = 0;
  long long hikukazu = 0;
  long long atai = 0;
  for (int i = 0; i < Date; i++) {
    atai = 0;
    atai = s[i][t[i] - 1];
    hikukazu += wa - c[t[i] - 1] * (i + 1 - last[t[i] - 1]);
    last[t[i] - 1] = i + 1;
    kotae += atai - hikukazu;
  }
  clock_t end = clock();
  double time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0;
  while (time < Seigenjikan) {
    int d = rand() % Date;
    int q = rand() % Shurui;
    int old = t[d];
    t[d] = q;
    long long new_kotae = 0;
    hikukazu = 0;
    for (int i = 0; i < Date; i++) {
      atai = 0;
      atai = s[i][t[i]];
      hikukazu += wa - c[t[i]] * (i + 1 - last[t[i]]);
      last[t[i]] = i + 1;
      new_kotae += atai - hikukazu;
    }
    if (new_kotae < kotae) {
      t[d] = old;
    } else {
      kotae = new_kotae;
    }
    end = clock();
    time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0;
  }
  for (int i = 0; i < Date; i++) {
    cout << t[i] + 1 << endl;
  }
  return 0;
}