Problem p02618 (C++) — buggy submission, verdict: 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 = 1e5;
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 solve(int time) {
  bool seen[D + 1][ntype + 1];
  max_score = evaluate();
  for (int i = 0; i < time; ++i) {
    int p = rand() % D + 1;
    int change = rand() % ntype + 1;
    if (seen[p][change])
      continue;
    seen[p][change] = true;
    int temp = ans[p];
    ans[p] = change;
    int temp_score = evaluate();
    if (max_score < temp_score) {
      max_score = temp_score;
    } else
      ans[p] = temp;
  }
}

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

Error: TLE

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 = 1e6;
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 solve(int time) {
  vector<bool> seen(D + 1, false);
  max_score = evaluate();
  for (int i = 0; i < time; ++i) {
    int p = rand() % D + 1;
    if (seen[p])
      continue;
    int temp = ans[p];
    ans[p] = rand() % ntype + 1;
    int temp_score = evaluate();
    if (max_score < temp_score) {
      max_score = temp_score;
      seen[p] = true;
    } else
      ans[p] = temp;
  }
}

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