Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
#define INF 1e9

static const int seeds = 630;
static const int D = 365;
static const int ntype = 26;
static const int population_size = 50;
int gene[population_size][D + 1];
int next_gene[population_size][D + 1];
static int elete[D + 1];
static int elete_score = -INF;
static const int init_n_greedyGene = 1;
static const double crossRate = 0.5;
static const double mutationRate = 0.5;
vector<pair<double, int>> cumfreq;

class Contest {
public:
  Contest() { last = 0; }
  int s[D + 1];
  int c;
  int last;
};

Contest contest[ntype + 1];

void Input() {
  int d;
  cin >> d;
  for (int i = 1; i <= ntype; ++i) {
    int t;
    cin >> t;
    contest[i].c = t;
  }
  for (int day = 1; day <= D; ++day) {
    for (int type = 1; type <= ntype; ++type) {
      int t;
      cin >> t;
      contest[type].s[day] = t;
    }
  }
}

void randGene(int p) {
  for (int day = 1; day <= D; ++day) {
    gene[p][day] = rand() % ntype + 1;
  }
}

void greedyGene(int p) {
  vector<int> l(ntype + 1, 0);
  int score = 0;
  for (int d = 1; d <= D; ++d) {
    for (int i = 1; i <= ntype; ++i)
      score -= contest[i].c * (d - l[i]);
    int temp = score;
    int t = -1;
    for (int i = 1; i <= ntype; ++i) {
      int k = contest[i].s[d] + contest[i].c * (d - l[i]) + score;
      if (k > temp) {
        temp = k;
        t = i;
      }
    }
    gene[p][d] = t;
    l[t] = d;
    score += temp;
  }
  /*
  cout<<endl<<"elete"<<endl;
  for(int d=1;d<=D;++d) cout<<elete[d]<<endl;

  cout<<endl<<"greedy"<<endl;
  for(int d=1;d<=D;++d) cout<<gene[p][d]<<endl;
  */
}

int evaluate(int i) {
  int v[D + 1];
  v[0] = 0;
  for (int day = 1; day <= D; ++day) {
    v[day] = 0;
    int type = gene[i][day];
    v[day] += contest[type].s[day];
    contest[type].last = day;
    for (int t = 1; t <= ntype; ++t) {
      v[day] -= contest[t].c * (day - contest[t].last);
    }
    v[day] += v[day - 1];
  }
  return v[D];
}

void calcCumFreq() {
  double sum = 0;
  int last_n = cumfreq.size();
  --last_n;
  sort(cumfreq.begin(), cumfreq.end());
  // change elete
  /*
  cout<<endl<<"Gene"<<endl<<endl;
  for(int i=0;i<cumfreq.size();++i) cout<<cumfreq[i].first<<endl;
  */
  if (elete_score < cumfreq[last_n].first) {
    elete_score = cumfreq[last_n].first;
    // cout<<elete_score<<endl;
    for (int d = 1; d <= D; ++d) {
      elete[d] = gene[cumfreq[last_n].second][d];
      // cout<<elete[d]<<endl;
    }
  }
  for (int i = 0; i < 50; ++i) {
    sum += cumfreq[i].first;
  }
  for (int i = 0; i < 50; ++i) {
    cumfreq[i].first /= sum;
  }
  for (int i = 0; i < 49; ++i) {
    cumfreq[i + 1].first += cumfreq[i].first;
  }
}

void CrossOver(int gene_i, int ci, int cj) {
  int l = rand() % D + 1;
  int r = rand() % D + 1;
  while (r == l) {
    l = rand() % D + 1;
    r = rand() % D + 1;
  }
  if (r < l)
    swap(r, l);
  for (int d = 1; d < l; ++d)
    next_gene[gene_i][d] = gene[ci][d];
  for (int d = l; d <= r; ++d)
    next_gene[gene_i][d] = gene[cj][d];
  for (int d = r + 1; d <= D; ++d)
    next_gene[gene_i][d] = gene[ci][d];
}

void Mutation(int ngene_i) {
  int n = rand() % 3 + 1;
  for (int i = 0; i < n; ++i) {
    int p = rand() % D + 1;
    next_gene[ngene_i][p] = rand() % ntype + 1;
  }
}

int Select() {
  double a = (double)rand() / RAND_MAX;
  int t = cumfreq.size() - 1;
  for (int i = 0; i < t; ++i) {
    if (cumfreq[i + 1].first > a)
      return cumfreq[i].second;
  }
  return cumfreq[t].second;
}

// init
void Init(int nGreedy) {
  for (int i = 0; i < nGreedy; ++i)
    greedyGene(i);
  for (int i = nGreedy; i < population_size; ++i)
    randGene(i);
}

void makeCumFreq() {
  for (int i = 0; i < population_size; ++i) {
    int e = evaluate(i);
    // cout<<e<<endl;
    cumfreq.push_back(make_pair(e, i));
  }
  calcCumFreq();
}

void makeNextGeneration() {
  for (int d = 0; d <= D; ++d)
    next_gene[0][d] = elete[d];
  for (int i = 1; i < population_size; ++i) {
    int pi = Select();
    double crossrand = (double)rand() / RAND_MAX;
    if (crossrand < crossRate) {
      int pj = Select();
      CrossOver(i, pi, pj);
    } else {
      for (int j = 1; j <= D; ++j)
        next_gene[i][j] = gene[pi][j];
    }
    double murand = (double)rand() / RAND_MAX;
    if (murand < mutationRate) {
      Mutation(i);
    }
  }
  for (int i = 0; i < 50; ++i) {
    for (int j = 1; j <= D; ++j) {
      gene[i][j] = next_gene[i][j];
    }
  }
}

void GA(int n) {
  for (int i = 0; i < n; ++i) {
    makeCumFreq();
    makeNextGeneration();
    cumfreq.resize(0);
  }
}

void Output() {
  for (int d = 1; d <= D; ++d)
    cout << elete[d] << endl;
}

int main() {
  srand(seeds);
  Input();
  Init(init_n_greedyGene);
  GA(10000);
  Output();
}


Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
#define INF 1e9

static const int seeds = 630;
static const int D = 365;
static const int ntype = 26;
static const int population_size = 50;
int gene[population_size][D + 1];
int next_gene[population_size][D + 1];
static int elete[D + 1];
static int elete_score = -INF;
static const int init_n_greedyGene = 1;
static const double crossRate = 0.5;
static const double mutationRate = 0.5;
vector<pair<double, int>> cumfreq;

class Contest {
public:
  Contest() { last = 0; }
  int s[D + 1];
  int c;
  int last;
};

Contest contest[ntype + 1];

void Input() {
  int d;
  cin >> d;
  for (int i = 1; i <= ntype; ++i) {
    int t;
    cin >> t;
    contest[i].c = t;
  }
  for (int day = 1; day <= D; ++day) {
    for (int type = 1; type <= ntype; ++type) {
      int t;
      cin >> t;
      contest[type].s[day] = t;
    }
  }
}

void randGene(int p) {
  for (int day = 1; day <= D; ++day) {
    gene[p][day] = rand() % ntype + 1;
  }
}

void greedyGene(int p) {
  vector<int> l(ntype + 1, 0);
  int score = 0;
  for (int d = 1; d <= D; ++d) {
    for (int i = 1; i <= ntype; ++i)
      score -= contest[i].c * (d - l[i]);
    int temp = score;
    int t = -1;
    for (int i = 1; i <= ntype; ++i) {
      int k = contest[i].s[d] + contest[i].c * (d - l[i]) + score;
      if (k > temp) {
        temp = k;
        t = i;
      }
    }
    gene[p][d] = t;
    l[t] = d;
    score += temp;
  }
  /*
  cout<<endl<<"elete"<<endl;
  for(int d=1;d<=D;++d) cout<<elete[d]<<endl;

  cout<<endl<<"greedy"<<endl;
  for(int d=1;d<=D;++d) cout<<gene[p][d]<<endl;
  */
}

int evaluate(int i) {
  int v[D + 1];
  v[0] = 0;
  for (int day = 1; day <= D; ++day) {
    v[day] = 0;
    int type = gene[i][day];
    v[day] += contest[type].s[day];
    contest[type].last = day;
    for (int t = 1; t <= ntype; ++t) {
      v[day] -= contest[t].c * (day - contest[t].last);
    }
    v[day] += v[day - 1];
  }
  return v[D];
}

void calcCumFreq() {
  double sum = 0;
  int last_n = cumfreq.size();
  --last_n;
  sort(cumfreq.begin(), cumfreq.end());
  // change elete
  /*
  cout<<endl<<"Gene"<<endl<<endl;
  for(int i=0;i<cumfreq.size();++i) cout<<cumfreq[i].first<<endl;
  */
  if (elete_score < cumfreq[last_n].first) {
    elete_score = cumfreq[last_n].first;
    // cout<<elete_score<<endl;
    for (int d = 1; d <= D; ++d) {
      elete[d] = gene[cumfreq[last_n].second][d];
      // cout<<elete[d]<<endl;
    }
  }
  for (int i = 0; i < 50; ++i) {
    sum += cumfreq[i].first;
  }
  for (int i = 0; i < 50; ++i) {
    cumfreq[i].first /= sum;
  }
  for (int i = 0; i < 49; ++i) {
    cumfreq[i + 1].first += cumfreq[i].first;
  }
}

void CrossOver(int gene_i, int ci, int cj) {
  int l = rand() % D + 1;
  int r = rand() % D + 1;
  while (r == l) {
    l = rand() % D + 1;
    r = rand() % D + 1;
  }
  if (r < l)
    swap(r, l);
  for (int d = 1; d < l; ++d)
    next_gene[gene_i][d] = gene[ci][d];
  for (int d = l; d <= r; ++d)
    next_gene[gene_i][d] = gene[cj][d];
  for (int d = r + 1; d <= D; ++d)
    next_gene[gene_i][d] = gene[ci][d];
}

void Mutation(int ngene_i) {
  int n = rand() % 3 + 1;
  for (int i = 0; i < n; ++i) {
    int p = rand() % D + 1;
    next_gene[ngene_i][p] = rand() % ntype + 1;
  }
}

int Select() {
  double a = (double)rand() / RAND_MAX;
  int t = cumfreq.size() - 1;
  for (int i = 0; i < t; ++i) {
    if (cumfreq[i + 1].first > a)
      return cumfreq[i].second;
  }
  return cumfreq[t].second;
}

// init
void Init(int nGreedy) {
  for (int i = 0; i < nGreedy; ++i)
    greedyGene(i);
  for (int i = nGreedy; i < population_size; ++i)
    randGene(i);
}

void makeCumFreq() {
  for (int i = 0; i < population_size; ++i) {
    int e = evaluate(i);
    // cout<<e<<endl;
    cumfreq.push_back(make_pair(e, i));
  }
  calcCumFreq();
}

void makeNextGeneration() {
  for (int d = 0; d <= D; ++d)
    next_gene[0][d] = elete[d];
  for (int i = 1; i < population_size; ++i) {
    int pi = Select();
    double crossrand = (double)rand() / RAND_MAX;
    if (crossrand < crossRate) {
      int pj = Select();
      CrossOver(i, pi, pj);
    } else {
      for (int j = 1; j <= D; ++j)
        next_gene[i][j] = gene[pi][j];
    }
    double murand = (double)rand() / RAND_MAX;
    if (murand < mutationRate) {
      Mutation(i);
    }
  }
  for (int i = 0; i < 50; ++i) {
    for (int j = 1; j <= D; ++j) {
      gene[i][j] = next_gene[i][j];
    }
  }
}

void GA(int n) {
  for (int i = 0; i < n; ++i) {
    makeCumFreq();
    makeNextGeneration();
    cumfreq.resize(0);
  }
}

void Output() {
  for (int d = 1; d <= D; ++d)
    cout << elete[d] << endl;
}

int main() {
  srand(seeds);
  Input();
  Init(init_n_greedyGene);
  GA(4000);
  Output();
}