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 seed = 6;
static const int D = 365;
static const int ntype = 26;
static int ans[D + 1];
static int max_score = -INF;
static int t = 130000;
class Contest {
public:
  Contest() { last = 0; }
  int s[D + 1];
  int c;
  int last;
};

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

int evaluate() {
  int sum = 0;
  vector<int> l(ntype + 1, 0);
  for (int day = 1; day <= D; ++day) {
    int type = ans[day];
    sum += contest[type].s[day];
    l[type] = day;
    for (int t = 1; t <= ntype; ++t) {
      sum -= contest[t].c * (day - l[t]);
    }
  }
  return sum;
}

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

void Init() {
  for (int d = 1; d <= D; ++d)
    ans[d] = rand() % ntype + 1;
}

void Greedy() {
  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;
      }
    }
    ans[d] = t;
    l[t] = d;
  }
}

void change() {
  int p = rand() % D + 1;
  int temp = ans[p];
  ans[p] = rand() % ntype + 1;
  int temp_score = evaluate();
  if (max_score < temp_score)
    max_score = temp_score;
  else
    ans[p] = temp;
}

void swap() {
  int p1 = rand() % D + 1;
  int p2 = rand() % D + 1;
  while (p1 == p2)
    p2 = rand() % D + 1;
  int temp = ans[p1];
  ans[p1] = ans[p2];
  ans[p2] = temp;
  int temp_score = evaluate();
  if (max_score < temp_score)
    max_score = temp_score;
  else {
    ans[p2] = ans[p1];
    ans[p1] = temp;
  }
}

void solve(int times) {
  max_score = evaluate();
  for (int i = 0; i < times; ++i) {
    swap();
    change();
  }
}

int main() {
  srand(seed);
  Input();
  Greedy();
  solve(t);
  Output();
}


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

static const int seed = 6;
static const int D = 365;
static const int ntype = 26;
static int ans[D + 1];
static int max_score = -INF;
static int t = 110000;
class Contest {
public:
  Contest() { last = 0; }
  int s[D + 1];
  int c;
  int last;
};

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

int evaluate() {
  int sum = 0;
  vector<int> l(ntype + 1, 0);
  for (int day = 1; day <= D; ++day) {
    int type = ans[day];
    sum += contest[type].s[day];
    l[type] = day;
    for (int t = 1; t <= ntype; ++t) {
      sum -= contest[t].c * (day - l[t]);
    }
  }
  return sum;
}

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

void Init() {
  for (int d = 1; d <= D; ++d)
    ans[d] = rand() % ntype + 1;
}

void Greedy() {
  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;
      }
    }
    ans[d] = t;
    l[t] = d;
  }
}

void change() {
  int p = rand() % D + 1;
  int temp = ans[p];
  ans[p] = rand() % ntype + 1;
  int temp_score = evaluate();
  if (max_score < temp_score)
    max_score = temp_score;
  else
    ans[p] = temp;
}

void swap() {
  int p1 = rand() % D + 1;
  int p2 = rand() % D + 1;
  while (p1 == p2)
    p2 = rand() % D + 1;
  int temp = ans[p1];
  ans[p1] = ans[p2];
  ans[p2] = temp;
  int temp_score = evaluate();
  if (max_score < temp_score)
    max_score = temp_score;
  else {
    ans[p2] = ans[p1];
    ans[p1] = temp;
  }
}

void solve(int times) {
  max_score = evaluate();
  for (int i = 0; i < times; ++i) {
    swap();
    change();
  }
}

int main() {
  srand(seed);
  Input();
  Greedy();
  solve(t);
  Output();
}