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 const double swap_rate = 0.6;
static const double change_rate = 1.0 - swap_rate;
static int ans[D + 1];
static int max_score = 0;
static const int t = 1050000;
static const int last_swap = 5000;

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

static Contest contest[ntype + 1];

void Input() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  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 Score(int type) {
  int sum = 0;
  contest[type].last = 0;
  for (int day = 1; day <= D; ++day) {
    if (ans[day] == type) {
      sum += contest[type].s[day];
      int n = day - contest[type].last;
      sum -= contest[type].c * (n * (n - 1) / 2);
      contest[type].last = day;
    }
  }
  if (ans[D] != type) {
    int n = D - contest[type].last;
    sum -= contest[type].c * (n * (n + 1) / 2);
  }
  return sum;
}

void Output() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  for (int d = 1; d <= D; ++d)
    cout << ans[d] << '\\n';
}

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 before = ans[p];
  int after = rand() % ntype + 1;
  int before_score = Score(before) + Score(after);
  ans[p] = after;
  int delta = Score(before) + Score(after) - before_score;
  if (delta > 0) {
    max_score += delta;
  } else
    ans[p] = before;
}

void swap() {
  int p1 = rand() % D + 1;
  int p2 = rand() % D + 1;
  while (p1 == p2)
    p2 = rand() % D + 1;
  int temp_score = Score(ans[p1]) + Score(ans[p2]);
  int temp = ans[p1];
  ans[p1] = ans[p2];
  ans[p2] = temp;
  int delta = Score(ans[p2]) + Score(ans[p1]) - temp_score;
  if (delta > 0) {
    max_score += delta;
  } else {
    ans[p2] = ans[p1];
    ans[p1] = temp;
  }
}

void Solve(int times) {
  Greedy();
  for (int i = 1; i <= ntype; ++i)
    max_score += Score(i);
  for (int i = 0; i < times; ++i) {
    double p = (double)rand() / RAND_MAX;
    if (p < swap_rate)
      swap();
    else
      change();
  }
  for (int i = 0; i < last_swap; ++i)
    swap();
}

int main() {
  srand(seed);
  Input();
  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 const double swap_rate = 0.5;
static const double change_rate = 1.0 - swap_rate;
static int ans[D + 1];
static int max_score = 0;
static const int t = 1050000;
static const int last_swap = 500000;

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

static Contest contest[ntype + 1];

void Input() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  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 Score(int type) {
  int sum = 0;
  contest[type].last = 0;
  for (int day = 1; day <= D; ++day) {
    if (ans[day] == type) {
      sum += contest[type].s[day];
      int n = day - contest[type].last;
      sum -= contest[type].c * (n * (n - 1) / 2);
      contest[type].last = day;
    }
  }
  if (ans[D] != type) {
    int n = D - contest[type].last;
    sum -= contest[type].c * (n * (n + 1) / 2);
  }
  return sum;
}

void Output() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  for (int d = 1; d <= D; ++d)
    cout << ans[d] << '\\n';
}

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 before = ans[p];
  int after = rand() % ntype + 1;
  int before_score = Score(before) + Score(after);
  ans[p] = after;
  int delta = Score(before) + Score(after) - before_score;
  if (delta > 0) {
    max_score += delta;
  } else
    ans[p] = before;
}

void swap() {
  int p1 = rand() % D + 1;
  int p2 = rand() % D + 1;
  while (p1 == p2)
    p2 = rand() % D + 1;
  int temp_score = Score(ans[p1]) + Score(ans[p2]);
  int temp = ans[p1];
  ans[p1] = ans[p2];
  ans[p2] = temp;
  int delta = Score(ans[p2]) + Score(ans[p1]) - temp_score;
  if (delta > 0) {
    max_score += delta;
  } else {
    ans[p2] = ans[p1];
    ans[p1] = temp;
  }
}

void Solve(int times) {
  Greedy();
  for (int i = 1; i <= ntype; ++i)
    max_score += Score(i);
  for (int i = 0; i < times; ++i) {
    double p = (double)rand() / RAND_MAX;
    if (p < swap_rate)
      swap();
    else
      change();
  }
  for (int i = 0; i < last_swap; ++i)
    swap();
}

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