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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vec;
typedef vector<bool> veb;
typedef vector<string> ves;
typedef vector<vector<ll>> vvl;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvec;
typedef vector<vector<bool>> vveb;
typedef vector<vector<string>> vves;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define rep2(i, n) for (int i = 2; i < (int)(n); i++)
#define repk(i, k, n) for (int i = k; i < (int)(n); i++)
#define rev(v) reverse(v.begin(), v.end())
#define fs first
#define sc second
#define pb push_back
#define pp pop_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(a) *max_element(all(a))
#define minel(a) *min_element(all(a))
#define acc accumulate
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define mod (1000000007)
typedef long long int64;
// const int64 INF = 1LL << 58;
#define sz(v) ((int)(v).size())
#define uniq(a)                                                                \\
  sort(all(a));                                                                \\
  (a).resize(unique(all(a)) - a.begin());
#define dab(v)                                                                 \\
  for (auto i : v)                                                             \\
    cout << i << ' ';                                                          \\
  cout << endl;
#define vin(v)                                                                 \\
  for (ll i = 0; i < v.size(); ++i)                                            \\
    cin >> v[i];
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> void out(T a) { cout << a << endl; }
template <class T> void die(T a) {
  cout << a << endl;
  exit(0);
}

double GetRandomd(double min, double max) {
  return min + (double)(rand() * (max - min + 1.0) / (1.0 + RAND_MAX));
}
int GetRandomi(int min, int max) {
  return min + (int)(rand() * (max - min + 1.0) / (1.0 + RAND_MAX));
}

class MyRandom {
public:
  MyRandom() : gen(rd()) {}

  int nextInt(const int &max) {
    std::uniform_int_distribution<> dis(0, max);
    return dis(gen);
  }

private:
  std::random_device rd;
  std::mt19937_64 gen;
  std::uniform_int_distribution<> setdis;
  bool disSet = false;
};
int D;
void crossover(std::vector<int> &parent1, std::vector<int> &parent2) {
  int size = int(parent1.size());

  MyRandom rand;
  int number1 = rand.nextInt(D);
  int number2 = rand.nextInt(D);

  int start = fmin(number1, number2);
  int end = fmax(number1, number2);

  std::vector<int> child1;
  std::vector<int> child2;

  for (int i = start; i < end; i++) {
    child1.push_back(parent1[i]);
    child2.push_back(parent2[i]);
  }

  int geneIndex = 0;
  int geneInparent1 = 0;
  int geneInparent2 = 0;

  for (int i = 0; i < size; i++) {
    geneIndex = (end + i) % size;
    geneInparent1 = parent1[geneIndex];
    geneInparent2 = parent2[geneIndex];

    bool is_there = false;
    for (int i1 = 0; i1 < child1.size(); i1++) {
      if (child1[i1] == geneInparent2) {
        is_there = true;
      }
    }
    if (!is_there) {
      child1.push_back(geneInparent2);
    }

    bool is_there1 = false;
    for (int i1 = 0; i1 < child2.size(); i1++) {
      if (child2[i1] == geneInparent1) {
        is_there1 = true;
      }
    }
    if (!is_there1) {
      child2.push_back(geneInparent1);
    }
  }

  std::rotate(child1.begin(), child1.begin() + start, child1.end());
  std::rotate(child2.begin(), child2.begin() + start, child2.end());

  for (int i = 0; i < size; i++) {
    parent1[i] = child2[i];
    parent2[i] = child1[i];
  }
}

int main() {

  random_device seed_gen;
  mt19937 engine(seed_gen());

  // srand(time(NULL)); //初期化
  // rand()             //乱数取得
  //(double)rand()/RAND_MAX; //0-1乱数取得

  int popu = 101;
  vector<int> f(popu);         // 目的関数
  double crossover_rate = 1.0; // 交叉率
  double mutation_rate = 0.95; // 突然変異率
  cin >> D;
  vi c(26);
  rep(i, 26) cin >> c[i];
  vvi s(D, vi(26));
  rep(i, D) rep(j, 26) cin >> s[i][j];
  vi t(D); // コンテストタイプ
  vi last(26, 0);
  rep(i, D) cout << (rand() % 26) + 1 << endl;
  /*
  vvi par(popu, vi(D));
  rep(i,popu){
      rep(j,D){
          par[i][j] = (rand() % 26) + 1;
          //cout << par[i][j] << endl;
      }
  }

  rep(i,popu){
      f[i] = 0;
      rep(j,D) {
          f[i] += s[j][par[i][j]-1];
          last[t[j]-1] = j+1;
          rep(j,26) f[i] -= c[j] * (i+1 - last[j]);
      }
  }

  vvi child(popu, vi(D));
  rep(gene,100){
      //elite choose
      int eliteid = 0;
      rep(i,D) child[0][i] = par[eliteid][i];

      //次世代生成
      rep(loop,(popu-1)/2){
          //int id1 = Roulette(f);
          int id1 = 0;
          int id2 = GetRandomi(1, 100);
          crossover(par[id1], par[id2]);
          child[1+loop] = par[id1];
          child[1+loop+(popu-1)/2] = par[id2];
      }

      rep1(i,popu){
          f[i] = 0;
          rep(j,D) {
              f[i] += s[j][child[i][j]-1];
              last[child[i][j]-1] = j+1;
              rep(k,26) f[i] -= c[k] * (j+1 - last[k]);
          }
      }
      //par = child;
      rep(num,popu) {
          rep(day,D) {
              par[num][day] = child[num][day];
          }
      }
  }
  int ansid = -1;
  int tmp = -100100100;
  rep(i,f.size()) {
      if(tmp < f[i]) { ansid = i; tmp = f[i]; }
  }
  rep(i,D){
      cout << child[ansid][i] << endl;
  }
  return 0;*/
}

Error: -6
double free or corruption (out)


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vec;
typedef vector<bool> veb;
typedef vector<string> ves;
typedef vector<vector<ll>> vvl;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvec;
typedef vector<vector<bool>> vveb;
typedef vector<vector<string>> vves;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define rep2(i, n) for (int i = 2; i < (int)(n); i++)
#define repk(i, k, n) for (int i = k; i < (int)(n); i++)
#define rev(v) reverse(v.begin(), v.end())
#define fs first
#define sc second
#define pb push_back
#define pp pop_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(a) *max_element(all(a))
#define minel(a) *min_element(all(a))
#define acc accumulate
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define mod (1000000007)
typedef long long int64;
// const int64 INF = 1LL << 58;
#define sz(v) ((int)(v).size())
#define uniq(a)                                                                \\
  sort(all(a));                                                                \\
  (a).resize(unique(all(a)) - a.begin());
#define dab(v)                                                                 \\
  for (auto i : v)                                                             \\
    cout << i << ' ';                                                          \\
  cout << endl;
#define vin(v)                                                                 \\
  for (ll i = 0; i < v.size(); ++i)                                            \\
    cin >> v[i];
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> void out(T a) { cout << a << endl; }
template <class T> void die(T a) {
  cout << a << endl;
  exit(0);
}

double GetRandomd(double min, double max) {
  return min + (double)(rand() * (max - min + 1.0) / (1.0 + RAND_MAX));
}
int GetRandomi(int min, int max) {
  return min + (int)(rand() * (max - min + 1.0) / (1.0 + RAND_MAX));
}

int Roulette(vector<int> &f) {
  ld total = 0.0 / 100;
  rep(i, f.size()) {
    if (f[i] < 0)
      continue;
    total += (ld)f[i];
  }
  ld num = GetRandomd(0.0, total);
  ld tmp = 0.0;
  rep(i, f.size()) {
    tmp += f[i];
    if (tmp >= num)
      return i;
  }
  return f.size() - 1;
}

void Mutation(vector<int> &jobflow, int jobnum) {
  int id = GetRandomi(0, jobflow.size() - 1); // 変異index
  int ato;
  while (true) {
    ato = GetRandomi(1, jobnum);
    if (ato != jobflow[id])
      break;
  }
  for (int i = 0; i < jobflow.size(); i++) {
    if (jobflow[i] == ato) {
      int tmp = jobflow[i];
      jobflow[i] = jobflow[id];
      jobflow[id] = tmp;
      break;
    }
  }
}

class MyRandom {
public:
  MyRandom() : gen(rd()) {}

  int nextInt(const int &max) {
    std::uniform_int_distribution<> dis(0, max);
    return dis(gen);
  }

private:
  std::random_device rd;
  std::mt19937_64 gen;
  std::uniform_int_distribution<> setdis;
  bool disSet = false;
};
int D;
void crossover(std::vector<int> &parent1, std::vector<int> &parent2) {
  int size = int(parent1.size());

  MyRandom rand;
  int number1 = rand.nextInt(D);
  int number2 = rand.nextInt(D);

  int start = fmin(number1, number2);
  int end = fmax(number1, number2);

  std::vector<int> child1;
  std::vector<int> child2;

  for (int i = start; i < end; i++) {
    child1.push_back(parent1[i]);
    child2.push_back(parent2[i]);
  }

  int geneIndex = 0;
  int geneInparent1 = 0;
  int geneInparent2 = 0;

  for (int i = 0; i < size; i++) {
    geneIndex = (end + i) % size;
    geneInparent1 = parent1[geneIndex];
    geneInparent2 = parent2[geneIndex];

    bool is_there = false;
    for (int i1 = 0; i1 < child1.size(); i1++) {
      if (child1[i1] == geneInparent2) {
        is_there = true;
      }
    }
    if (!is_there) {
      child1.push_back(geneInparent2);
    }

    bool is_there1 = false;
    for (int i1 = 0; i1 < child2.size(); i1++) {
      if (child2[i1] == geneInparent1) {
        is_there1 = true;
      }
    }
    if (!is_there1) {
      child2.push_back(geneInparent1);
    }
  }

  std::rotate(child1.begin(), child1.begin() + start, child1.end());
  std::rotate(child2.begin(), child2.begin() + start, child2.end());

  for (int i = 0; i < size; i++) {
    parent1[i] = child2[i];
    parent2[i] = child1[i];
  }
}

int main() {

  random_device seed_gen;
  mt19937 engine(seed_gen());

  // srand(time(NULL)); //初期化
  // rand()             //乱数取得
  //(double)rand()/RAND_MAX; //0-1乱数取得

  int popu = 101;
  vector<int> f(popu);         // 目的関数
  double crossover_rate = 1.0; // 交叉率
  double mutation_rate = 0.95; // 突然変異率
  cin >> D;
  vi c(26);
  rep(i, 26) cin >> c[i];
  vvi s(D, vi(26));
  rep(i, D) rep(j, 26) cin >> s[i][j];
  vi t(D); // コンテストタイプ
  vi last(26, 0);

  vvi par(popu, vi(D));
  rep(i, popu) {
    rep(j, D) {
      par[i][j] = (rand() % 26) + 1;
      // cout << par[i][j] << endl;
    }
  }

  rep(i, popu) {
    f[i] = 0;
    rep(j, D) {
      f[i] += s[j][par[i][j] - 1];
      last[t[j] - 1] = j + 1;
      rep(j, 26) f[i] -= c[j] * (i + 1 - last[j]);
    }
  }

  vvi child(popu, vi(D));
  rep(gene, 100) {
    // elite choose
    int eliteid = Roulette(f);
    rep(i, D) child[0][i] = par[eliteid][i];

    // 次世代生成
    rep(loop, (popu - 1) / 2) {
      int id1 = Roulette(f);
      int id2 = id1;
      while (id2 == id1)
        id2 = Roulette(f);
      // cout << "come" << endl;
      crossover(par[id1], par[id2]);
      child[loop] = par[id1];
      // shuffle(all(child[loop]), engine);
      child[loop + (popu - 1) / 2] = par[id2];
      // shuffle(all(child[loop+(popu-1)/2]), engine);
      // cout << "come" << endl;
    }

    rep1(i, popu) {
      f[i] = 0;
      rep(j, D) {
        f[i] += s[j][child[i][j] - 1];
        last[child[i][j] - 1] = j + 1;
        rep(k, 26) f[i] -= c[k] * (j + 1 - last[k]);
      }
    }
    // par = child;
    rep(num, popu) {
      rep(day, D) { par[num][day] = child[num][day]; }
    }
  }
  int ansid = -1;
  int tmp = -100100100;
  rep(i, f.size()) {
    if (tmp < f[i]) {
      ansid = i;
      tmp = f[i];
    }
  }
  rep(i, D) { cout << child[ansid][i] << endl; }
  return 0;
}