Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdlib>
#include <iostream>

using namespace std;
#define N 10000
#define C 150
#define CHANGE_RATE 0.5

int main() {
  int d = 0;
  int c[26];
  int s[365][26];
  int t_c[C][365];
  int last[26] = {0};
  int score_c[C] = {0};
  int score = 0;
  int max_c = 0;

  cin >> d;

  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 < C; i++) {
    for (int j = 0; j < 365; j++) {
      t_c[i][j] = rand() % 26;
    }
  }

  for (int n = 0; n < N; n++) {
    score = 0;
    max_c = 0;
    for (int ca = 0; ca < C; ca++) {
      score_c[ca] = 0;
      for (int i = 0; i < 26; i++)
        last[i] = 0;

      for (int i = 0; i < d; i++) {
        for (int j = 0; j < 26; j++) {
          if (t_c[ca][i] == j) {
            last[j] = 0;
            score_c[ca] += s[i][j];
          } else {
            last[j]++;
            score_c[ca] -= c[j] * last[j];
          }
        }
      }
    }

    for (int ca = 0; ca < C; ca++) {
      if (score < score_c[ca]) {
        score = score_c[ca];
        max_c = ca;
      }
    }
    // cout << score << endl;

    for (int ca = 0; ca < C; ca++) {
      if (ca == max_c)
        continue;
      for (int i = 0; i < d; i++) {
        if ((rand() / (double)RAND_MAX) < CHANGE_RATE) {
          t_c[ca][i] = rand() % 26;
        } else {
          t_c[ca][i] = t_c[max_c][i];
        }
      }
    }
  }

  for (int i = 0; i < d; i++) {
    cout << ((t_c[max_c][i] % 26) + 1) << endl;
  }

  return 0;
}

Fixed submission:
#include <cstdlib>
#include <iostream>

using namespace std;
#define N 5000
#define C 15
#define CHANGE_RATE 0.1

int main() {
  int d = 0;
  int c[26];
  int s[365][26];
  int t_c[C][365];
  int last[26] = {0};
  int score_c[C] = {0};
  int score = 0;
  int max_c = 0;

  cin >> d;

  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 < C; i++) {
    for (int j = 0; j < 365; j++) {
      t_c[i][j] = rand() % 26;
    }
  }

  for (int n = 0; n < N; n++) {
    score = 0;
    max_c = 0;
    for (int ca = 0; ca < C; ca++) {
      score_c[ca] = 0;
      for (int i = 0; i < 26; i++)
        last[i] = 0;

      for (int i = 0; i < d; i++) {
        for (int j = 0; j < 26; j++) {
          if (t_c[ca][i] == j) {
            last[j] = 0;
            score_c[ca] += s[i][j];
          } else {
            last[j]++;
            score_c[ca] -= c[j] * last[j];
          }
        }
      }
    }

    for (int ca = 0; ca < C; ca++) {
      if (score < score_c[ca]) {
        score = score_c[ca];
        max_c = ca;
      }
    }
    // cout << score << endl;

    for (int ca = 0; ca < C; ca++) {
      if (ca == max_c)
        continue;
      for (int i = 0; i < d; i++) {
        if ((rand() / (double)RAND_MAX) < CHANGE_RATE) {
          t_c[ca][i] = rand() % 26;
        } else {
          t_c[ca][i] = t_c[max_c][i];
        }
      }
    }
  }

  for (int i = 0; i < d; i++) {
    cout << ((t_c[max_c][i] % 26) + 1) << endl;
  }

  return 0;
}