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

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

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n)-1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\\n'
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline void prn(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << ' ';
}
template <class T> inline void printv(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' ');
}
template <class T> inline void printvv(vector<vector<T>> &v) {
  for (auto u : v)
    printv(u);
}
template <class T> inline void printlnv(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << endl;
}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;

void solve();

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(numeric_limits<double>::max_digits10);
  solve();
  return 0;
}

void solve() {
  int d;
  cin >> d;
  vi c(26);
  REP(i, 26) cin >> c[i];
  vvl s(d, vl(26));
  REP(i, d) REP(j, 26) cin >> s[i][j];

  vi last(26);
  ll ans = 0;
  vi a(d);
  REP(i, d) {
    ll ans2 = -LINF;
    ll ans_i = -1;
    REP(x, 26) {
      ll tmp = s[i][x];
      REP(j, 26) {
        if (x != j)
          tmp -= c[j] * ((i + 1) - last[j]);
      }
      if (chmax(ans2, tmp))
        ans_i = x;
    }
    last[ans_i] = i + 1;
    ans += ans2;
    // cout << ans_i + 1 << endl;
    a[i] = ans_i + 1;
  }
  mt19937 mt{random_device{}()};
  uniform_int_distribution<int> dist_d(1, d);
  uniform_int_distribution<int> dist_c(1, 10);
  REP(_, 200000) {
    vi lst(26);
    ll ans_n = 0;
    int x = dist_d(mt);
    int y = dist_c(mt);
    int x2 = x + (x + 10 > d ? -y : y);
    swap(a[x - 1], a[x2 - 1]);
    REP(i, d) {
      int t = a[i];
      ans_n += s[i][t - 1];
      lst[t - 1] = i + 1;
      REP(j, 26) if (t - 1 != j) ans_n -= c[j] * ((i + 1) - lst[j]);
    }

    if (ans_n > ans) {
      ans = ans_n;
    } else {
      swap(a[x - 1], a[x2 - 1]);
    }
  }
  printlnv(a);
}


Error: -6
double free or corruption (out)


Fixed submission:

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

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n)-1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\\n'
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline void prn(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << ' ';
}
template <class T> inline void printv(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' ');
}
template <class T> inline void printvv(vector<vector<T>> &v) {
  for (auto u : v)
    printv(u);
}
template <class T> inline void printlnv(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << endl;
}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;

void solve();

int main() {
  // ios::sync_with_stdio(false);
  // cin.tie(nullptr);
  cout << fixed << setprecision(numeric_limits<double>::max_digits10);
  solve();
  return 0;
}

void solve() {
  int d;
  cin >> d;
  vi c(26);
  REP(i, 26) cin >> c[i];
  vvl s(d, vl(26));
  REP(i, d) REP(j, 26) cin >> s[i][j];

  vi last(26);
  ll ans = 0;
  vi a(d);
  REP(i, d) {
    ll ans2 = -LINF;
    ll ans_i = -1;
    REP(x, 26) {
      ll tmp = s[i][x];
      REP(j, 26) {
        if (x != j)
          tmp -= c[j] * ((i + 1) - last[j]);
      }
      if (chmax(ans2, tmp))
        ans_i = x;
    }
    last[ans_i] = i + 1;
    ans += ans2;
    // cout << ans_i + 1 << endl;
    a[i] = ans_i + 1;
  }
  mt19937 mt{random_device{}()};
  uniform_int_distribution<int> dist_d(1, d);
  uniform_int_distribution<int> dist_c(1, 26);
  uniform_int_distribution<int> dist_s(1, 10);
  REP(_, 200000) {
    vi lst(26);
    ll ans_n = 0;
    int x = dist_d(mt);
    int z = dist_s(mt);
    int x2 = x + (x + 12 > d ? -z : z);
    swap(a[x], a[x2]);
    REP(i, d) {
      int t = a[i];
      ans_n += s[i][t - 1];
      lst[t - 1] = i + 1;
      REP(j, 26) if (t - 1 != j) ans_n -= c[j] * ((i + 1) - lst[j]);
    }

    if (ans_n > ans) {
      ans = ans_n;
    } else {
      swap(a[x], a[x2]);
    }
  }
  printlnv(a);
}