Problem p02618 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
constexpr int CONTEST_NUM = 26;

int score_function(const vector<int> &t, const vector<vector<int>> &s,
                   const vector<int> &c, const int &d) {
  vector<int> last(27);
  int manzoku = 0;
  for (int i = 1; i <= d; i++) {
    manzoku += s[i][t[i]];
    last[t[i]] = i;
    int sum = 0;
    for (int j = 1; j <= CONTEST_NUM; j++) {
      sum += c[j] * (i - last[j]);
    }
    manzoku -= sum;
  }
  return manzoku;
}

void set_ans(const vector<int> &t, vector<int> &ans, const int &d) {
  for (int i = 1; i <= d; i++) {
    ans[i] = t[i];
  }
}

int main() {
  int d;
  cin >> d;
  vector<int> c(27);
  for (int i = 1; i <= CONTEST_NUM; i++) {
    cin >> c[i];
  }
  vector<vector<int>> s(d + 1, vector<int>(27));
  for (int i = 1; i <= d; i++) {
    for (int j = 1; j <= CONTEST_NUM; j++) {
      cin >> s[i][j];
    }
  }
  vector<int> t(d + 1);
  vector<int> ans(d + 1);
  int old_ans = -1000000000;
  std::random_device rnd;
  std::mt19937 mt(rnd());
  std::uniform_int_distribution<> rand26(1, 26);
  std::uniform_int_distribution<> randd(1, d);

  for (int i = 1; i <= d; i++) {
    int max_contest = -1;
    for (int j = 1; j <= CONTEST_NUM; j++) {
      if (max_contest < s[i][j]) {
        max_contest = s[i][j];
        t[i] = j;
      }
    }
  }
  int k = 0;
  while (k < 320000) {
    int che = randd(mt);
    int old_f = t[che];
    t[che] = rand26(mt);
    int tmp = score_function(t, s, c, d);
    if (tmp > old_ans) {
      old_ans = tmp;
    } else {
      t[che] = old_f;
    }
    k++;
  }
  // cout<<old_ans<<endl;
  for (int i = 1; i <= d; i++) {
    cout << t[i] << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int CONTEST_NUM = 26;

int score_function(const vector<int> &t, const vector<vector<int>> &s,
                   const vector<int> &c, const int &d) {
  vector<int> last(27);
  int manzoku = 0;
  for (int i = 1; i <= d; i++) {
    manzoku += s[i][t[i]];
    last[t[i]] = i;
    int sum = 0;
    for (int j = 1; j <= CONTEST_NUM; j++) {
      sum += c[j] * (i - last[j]);
    }
    manzoku -= sum;
  }
  return manzoku;
}

void set_ans(const vector<int> &t, vector<int> &ans, const int &d) {
  for (int i = 1; i <= d; i++) {
    ans[i] = t[i];
  }
}

int main() {
  int d;
  cin >> d;
  vector<int> c(27);
  for (int i = 1; i <= CONTEST_NUM; i++) {
    cin >> c[i];
  }
  vector<vector<int>> s(d + 1, vector<int>(27));
  for (int i = 1; i <= d; i++) {
    for (int j = 1; j <= CONTEST_NUM; j++) {
      cin >> s[i][j];
    }
  }
  vector<int> t(d + 1);
  vector<int> ans(d + 1);
  int old_ans = -1000000000;
  std::random_device rnd;
  std::mt19937 mt(rnd());
  std::uniform_int_distribution<> rand26(1, 26);
  std::uniform_int_distribution<> randd(1, d);

  for (int i = 1; i <= d; i++) {
    int max_contest = -1;
    for (int j = 1; j <= CONTEST_NUM; j++) {
      if (max_contest < s[i][j]) {
        max_contest = s[i][j];
        t[i] = j;
      }
    }
  }
  int k = 0;
  while (k < 290000) {
    int che = randd(mt);
    int old_f = t[che];
    t[che] = rand26(mt);
    int tmp = score_function(t, s, c, d);
    if (tmp > old_ans) {
      // set_ans(t,ans,d);
      old_ans = tmp;
    } else {
      t[che] = old_f;
    }
    k++;
  }
  set_ans(t, ans, d);
  // cout<<score_function(t,s,c,d)<<endl;
  for (int i = 1; i <= d; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}