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

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

int main() {
  clock_t start = clock();
  int D;
  cin >> D;

  vector<int> c(26);
  vector<vector<int>> s(D, vector<int>(26));
  vector<int> t(D);
  vector<vector<int>> last(D, vector<int>(26, 0));
  vector<int> sat(D);
  vector<int> sat_tmp(26);

  for (int i = 0; i < 26; i++) {
    cin >> c[i];
  }

  for (int i = 0; i < D; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> s[i][j];
    }
  }

  /*********************************/
  for (int i = 0; i < D; i++) {
    for (int k = 0; k < 26; k++) {
      for (int j = 0; j < 26; j++) {
        if (i == 0) {
          last[i][j] = 0;
        } else {
          last[i][j] = last[i - 1][j];
        }
      }
      last[i][k] = i + 1;
      if (i == 0) {
        sat_tmp[k] = s[i][k];
      } else {
        sat_tmp[k] = s[i][k] + sat_tmp[i - 1];
      }
      int satisfaction = 0;
      for (int j = 0; j < 26; j++) {
        satisfaction += c[j] * (i + 1 - last[i][j]);
      }
      sat_tmp[k] -= satisfaction;
    }
    vector<int>::iterator iter = max_element(sat_tmp.begin(), sat_tmp.end());
    size_t index = distance(sat_tmp.begin(), iter);
    t[i] = index;

    for (int j = 0; j < 26; j++) {
      if (i == 0) {
        last[i][j] = 0;
      } else {
        last[i][j] = last[i - 1][j];
      }
    }
    last[i][t[i]] = i + 1;
    if (i == 0) {
      sat[i] = s[i][t[i]];
    } else {
      sat[i] = s[i][t[i]] + sat[i - 1];
    }
    int satisfaction = 0;
    for (int j = 0; j < 26; j++) {
      satisfaction += c[j] * (i + 1 - last[i][j]);
    }
    sat[i] -= satisfaction;
  }

  // for (int i = 0; i < D; i++) {
  // 	cout << sat[i] << endl;
  // }
  int max_sat = sat[D - 1];
  /*********************************/
  vector<int> t_new(D);
  std::srand(time(NULL));

  while (clock() - start < 1800000) {
    t_new = t;
    t_new[rand() % D] = rand() % 26;

    for (int i = 0; i < D; i++) {
      for (int j = 0; j < 26; j++) {
        if (i == 0) {
          last[i][j] = 0;
        } else {
          last[i][j] = last[i - 1][j];
        }
      }
      last[i][t_new[i]] = i + 1;
      if (i == 0) {
        sat[i] = s[i][t_new[i]];
      } else {
        sat[i] = s[i][t_new[i]] + sat[i - 1];
      }
      int satisfaction = 0;
      for (int j = 0; j < 26; j++) {
        satisfaction += c[j] * (i + 1 - last[i][j]);
      }
      sat[i] -= satisfaction;
    }
    if (sat[D - 1] > max_sat) {
      // cout << sat[D - 1] << endl;
      max_sat = sat[D - 1];
      t = t_new;
    }
  }

  /*********************************/

  for (int i = 0; i < D; i++) {
    cout << t[i] + 1 << endl;
  }
  // cout << sat[D - 1] << endl;
  // cout << clock() - start << endl;

  return 0;
}

Error: -6
double free or corruption (out)


Fixed submission:

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

int main() {
  clock_t start = clock();
  int D;
  cin >> D;

  vector<int> c(26);
  vector<vector<int>> s(D, vector<int>(26));
  vector<int> t(D);
  vector<vector<int>> last(D, vector<int>(26, 0));
  vector<int> sat(D);
  vector<int> sat_tmp(26);

  for (int i = 0; i < 26; i++) {
    cin >> c[i];
  }

  for (int i = 0; i < D; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> s[i][j];
    }
  }

  /*********************************/
  for (int i = 0; i < D; i++) {
    for (int k = 0; k < 26; k++) {
      for (int j = 0; j < 26; j++) {
        if (i == 0) {
          last[i][j] = 0;
        } else {
          last[i][j] = last[i - 1][j];
        }
      }
      last[i][k] = i + 1;
      if (i == 0) {
        sat_tmp[k] = s[i][k];
      } else {
        sat_tmp[k] = s[i][k] + sat_tmp[i - 1];
      }
      int satisfaction = 0;
      for (int j = 0; j < 26; j++) {
        satisfaction += c[j] * (i + 1 - last[i][j]);
      }
      sat_tmp[k] -= satisfaction;
    }
    vector<int>::iterator iter = max_element(sat_tmp.begin(), sat_tmp.end());
    size_t index = distance(sat_tmp.begin(), iter);
    t[i] = index;

    for (int j = 0; j < 26; j++) {
      if (i == 0) {
        last[i][j] = 0;
      } else {
        last[i][j] = last[i - 1][j];
      }
    }
    last[i][t[i]] = i + 1;
    if (i == 0) {
      sat[i] = s[i][t[i]];
    } else {
      sat[i] = s[i][t[i]] + sat[i - 1];
    }
    int satisfaction = 0;
    for (int j = 0; j < 26; j++) {
      satisfaction += c[j] * (i + 1 - last[i][j]);
    }
    sat[i] -= satisfaction;
  }

  // for (int i = 0; i < D; i++) {
  // 	cout << sat[i] << endl;
  // }
  int max_sat = sat[D - 1];
  /*********************************/
  vector<int> t_new(D);
  std::srand(time(NULL));

  while (clock() - start < 1800000) {
    t_new = t;
    t_new[rand() % D] = rand() % 26;

    for (int i = 0; i < D; i++) {
      for (int j = 0; j < 26; j++) {
        if (i == 0) {
          last[i][j] = 0;
        } else {
          last[i][j] = last[i - 1][j];
        }
      }
      last[i][t_new[i] - 1] = i + 1;
      if (i == 0) {
        sat[i] = s[i][t_new[i] - 1];
      } else {
        sat[i] = s[i][t_new[i] - 1] + sat[i - 1];
      }
      int satisfaction = 0;
      for (int j = 0; j < 26; j++) {
        satisfaction += c[j] * (i + 1 - last[i][j]);
      }
      sat[i] -= satisfaction;
    }
    if (sat[D - 1] > max_sat) {
      max_sat = sat[D - 1];
      t = t_new;
    }
  }

  /*********************************/

  for (int i = 0; i < D; i++) {
    cout << t[i] + 1 << endl;
  }
  // cout << sat[D - 1] << endl;
  // cout << clock() - start << endl;

  return 0;
}