Problem p02618 (C++)

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

static const int D = 365;
static const int ntype = 26;

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

Contest contest[27];

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;
    }
  }
}

int v[D + 1];

int gene[50][D + 1];
int next_gene[50][D + 1];
int elete[D + 1];
vector<pair<double, int>> cumfreq;

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

void Init() {
  for (int i = 0; i < 50; ++i) {
    randGene(i);
  }
}

void Greedy(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;
  }
}

void decoding(int i) {
  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];
  }
  v[D] = v[D] < 0 ? 1 : v[D];
  cumfreq.push_back(make_pair((double)v[D], i));
}

void CrossOver(int ni, int ci, int cj) {
  for (int i = 1; i <= D; ++i) {
    double p = rand() / RAND_MAX;
    if (p > 0.5)
      next_gene[ni][i] = gene[ci][i];
    else
      next_gene[ni][i] = gene[cj][i];
  }
}

void Mutation(int i) {
  /*cout<<i<<endl;
  reverse(next_gene+i+1,next_gene+i+D+1);
  cout<<"mutation"<<endl;*/
  int tgene[D + 1];
  for (int d = 1; d <= D; ++d)
    tgene[d] = next_gene[i][d];
  for (int d = 1; d <= D; ++d)
    next_gene[i][d] = tgene[D - d + 1];
}

void calcCumFreq() {
  double sum = 0;
  sort(cumfreq.begin(), cumfreq.end());
  // for(int i=0;i<cumfreq.size();++i) cout<<i<<"個体: "
  // <<cumfreq[i].first<<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;
  }
}

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

void GA() {
  static const double crossRate = 0.7;
  static const double mutationRate = 0.6;
  for (int i = 0; i < 50; ++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);
    }
  }
  cumfreq.resize(0);
  for (int i = 0; i < 50; ++i) {
    for (int j = 1; j <= D; ++j) {
      gene[i][j] = next_gene[i][j];
    }
    decoding(i);
  }
  calcCumFreq();
}

int main() {
  srand(700);
  Input();
  Init();
  for (int i = 0; i < 5; ++i)
    Greedy(i);
  for (int i = 0; i < 50; ++i) {
    decoding(i);
  }
  calcCumFreq();
  for (int i = 0; i < 5000; ++i) {
    GA();
  }
  for (int i = 0; i < 50; ++i) {
    decoding(i);
  }
  calcCumFreq();
  int e = cumfreq[49].second;

  for (int i = 1; i <= D; ++i)
    cout << gene[e][i] << endl;
}


Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

static const int D = 365;
static const int ntype = 26;

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

Contest contest[27];

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;
    }
  }
}

int v[D + 1];

int gene[50][D + 1];
int next_gene[50][D + 1];
int elete[D + 1];
vector<pair<double, int>> cumfreq;

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

void Init() {
  for (int i = 0; i < 50; ++i) {
    randGene(i);
  }
}

void Greedy(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;
  }
}

void decoding(int i) {
  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];
  }
  v[D] = v[D] < 0 ? 1 : v[D];
  cumfreq.push_back(make_pair((double)v[D], i));
}

void CrossOver(int ni, int ci, int cj) {
  for (int i = 1; i <= D; ++i) {
    double p = rand() / RAND_MAX;
    if (p > 0.5)
      next_gene[ni][i] = gene[ci][i];
    else
      next_gene[ni][i] = gene[cj][i];
  }
}

void Mutation(int i) {
  /*cout<<i<<endl;
  reverse(next_gene+i+1,next_gene+i+D+1);
  cout<<"mutation"<<endl;*/
  int tgene[D + 1];
  for (int d = 1; d <= D; ++d)
    tgene[d] = next_gene[i][d];
  for (int d = 1; d <= D; ++d)
    next_gene[i][d] = tgene[D - d + 1];
}

void calcCumFreq() {
  double sum = 0;
  sort(cumfreq.begin(), cumfreq.end());
  // for(int i=0;i<cumfreq.size();++i) cout<<i<<"個体: "
  // <<cumfreq[i].first<<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;
  }
}

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

void GA() {
  static const double crossRate = 0.7;
  static const double mutationRate = 0.6;
  for (int i = 0; i < 50; ++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);
    }
  }
  cumfreq.resize(0);
  for (int i = 0; i < 50; ++i) {
    for (int j = 1; j <= D; ++j) {
      gene[i][j] = next_gene[i][j];
    }
    decoding(i);
  }
  calcCumFreq();
}

int main() {
  srand(700);
  Input();
  Init();
  for (int i = 0; i < 5; ++i)
    Greedy(i);
  for (int i = 0; i < 50; ++i) {
    decoding(i);
  }
  calcCumFreq();
  for (int i = 0; i < 4500; ++i) {
    GA();
  }
  for (int i = 0; i < 50; ++i) {
    decoding(i);
  }
  calcCumFreq();
  int e = cumfreq[49].second;

  for (int i = 1; i <= D; ++i)
    cout << gene[e][i] << endl;
}