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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pull = pair<ull, ll>;
using pis = pair<int, string>;
#define INF 1000000000
#define LLINF 10000000000000000ll
#define MOD 1000000007LL

int score(const int d, const vector<vector<int>> &s, const vector<int> &c,
          vector<int> &contests, vector<int> &last, vector<int> &satis,
          int beg) {
  // vector<int> contests(d);
  // vector<int> last(26, 0);
  for (int i = 0; i < 26; i++)
    last[i] = 0;
  int sat = (beg == 0) ? 0 : satis[beg - 1];
  // int sat = 0;
  // makecontest(s, c, last, contests);

  for (int i = 0; i < d; i++) {
    // cout << d << " " << i << endl;
    last[contests[i] - 1] = i + 1;
    if (i < beg)
      continue;
    sat += s[i][contests[i] - 1];
    for (int j = 0; j < 26; j++) {
      sat -= c[j] * (i + 1 - last[j]);
    }
    // cout << sat << endl;
    satis[i] = sat;
  }
  return sat;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // cout << fixed << setprecision(15);
  clock_t t = clock();

  int d;
  cin >> d;
  vector<int> c(26);
  for (int i = 0; i < 26; i++)
    cin >> c[i];
  vector<vector<int>> s(d, vector<int>(26));
  for (int i = 0; i < d; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> s[i][j];
    }
  }

  vector<int> contests(d);
  vector<int> templast(26, 0);
  // for (int i = 0; i < d; i++) cin >> contests[i];
  for (int i = 0; i < d; i++) {
    int mx = -INF, ct;
    for (int j = 0; j < 26; j++) {
      // mx = max(mx, s[i][j]);
      // if (mx == s[i][j]) ct = j+1;
      int temp = s[i][j];
      for (int k = 0; k < 26; k++) {
        if (j != k)
          temp -= c[k] * (i + 1 - templast[k]);
      }
      if (mx < temp) {
        ct = j + 1;
        mx = temp;
      }
    }
    contests[i] = ct;
    templast[ct - 1] = i + 1;
  }

  vector<int> last(26, 0);
  vector<int> satis(d, 0), rec(d);
  int param1 = 2, param2 = 1;
  int sc = score(d, s, c, contests, last, satis, 0);
  for (int i = 0; i < d; i++)
    rec[i] = satis[i];
  // cout << sc << endl;
  // for (int p = 0; p < 300000; p++) {
  int times = 0;
  while (clock() - t < 1900) {
    times++;
    srand(times);
    int op = rand() % param1;
    if (op < param1) {
      int day = rand() % d;
      int q = rand() % 26;
      // cout << day << " " << q << endl;
      int old = contests[day];
      contests[day] = q + 1;
      int ts = score(d, s, c, contests, last, satis, day);
      // cout << ts << endl;
      if (ts < sc) {
        contests[day] = old;
        for (int i = day; i < d; i++)
          satis[i] = rec[i];
        continue;
      }
      sc = ts;
      for (int i = day; i < d; i++)
        rec[i] = satis[i];
      // cout << sc << endl;
    }
    // cout << sc << endl;
    else {
      int day1 = rand() % d;
      int day2 = rand() % d;
      if (day1 == day2)
        continue;
      // cout << day << " " << q << endl;
      // int old = contests[day];
      // contests[day] = q + 1;
      swap(contests[day1], contests[day2]);
      int day = min(day1, day2);
      int ts = score(d, s, c, contests, last, satis, day);
      // cout << ts << endl;
      if (ts < sc) {
        // contests[day] = old;
        swap(contests[day1], contests[day2]);
        for (int i = day; i < d; i++)
          satis[i] = rec[i];
        continue;
      }
      sc = ts;
      for (int i = day; i < d; i++)
        rec[i] = satis[i];
      // cout << sc << endl;
    }
  }
  for (int i = 0; i < d; i++)
    cout << contests[i] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pull = pair<ull, ll>;
using pis = pair<int, string>;
#define INF 1000000000
#define LLINF 10000000000000000ll
#define MOD 1000000007LL

int score(const int d, const vector<vector<int>> &s, const vector<int> &c,
          vector<int> &contests, vector<int> &last, vector<int> &satis,
          int beg) {
  // vector<int> contests(d);
  // vector<int> last(26, 0);
  for (int i = 0; i < 26; i++)
    last[i] = 0;
  int sat = (beg == 0) ? 0 : satis[beg - 1];
  // int sat = 0;
  // makecontest(s, c, last, contests);

  for (int i = 0; i < d; i++) {
    // cout << d << " " << i << endl;
    last[contests[i] - 1] = i + 1;
    if (i < beg)
      continue;
    sat += s[i][contests[i] - 1];
    for (int j = 0; j < 26; j++) {
      sat -= c[j] * (i + 1 - last[j]);
    }
    // cout << sat << endl;
    satis[i] = sat;
  }
  return sat;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // cout << fixed << setprecision(15);

  int d;
  cin >> d;
  vector<int> c(26);
  for (int i = 0; i < 26; i++)
    cin >> c[i];
  vector<vector<int>> s(d, vector<int>(26));
  for (int i = 0; i < d; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> s[i][j];
    }
  }

  vector<int> contests(d);
  vector<int> templast(26, 0);
  // for (int i = 0; i < d; i++) cin >> contests[i];
  for (int i = 0; i < d; i++) {
    int mx = -INF, ct;
    for (int j = 0; j < 26; j++) {
      // mx = max(mx, s[i][j]);
      // if (mx == s[i][j]) ct = j+1;
      int temp = s[i][j];
      for (int k = 0; k < 26; k++) {
        if (j != k)
          temp -= c[k] * (i + 1 - templast[k]);
      }
      if (mx < temp) {
        ct = j + 1;
        mx = temp;
      }
    }
    contests[i] = ct;
    templast[ct - 1] = i + 1;
  }

  vector<int> last(26, 0);
  vector<int> satis(d, 0), rec(d);
  int param1 = 2, param2 = 1;
  int sc = score(d, s, c, contests, last, satis, 0);
  for (int i = 0; i < d; i++)
    rec[i] = satis[i];
  // cout << sc << endl;
  for (int p = 0; p < 420000; p++) {
    srand(p);
    int op = rand() % param1;
    if (op < param1) {
      int day = rand() % d;
      int q = rand() % 26;
      // cout << day << " " << q << endl;
      int old = contests[day];
      contests[day] = q + 1;
      int ts = score(d, s, c, contests, last, satis, day);
      // cout << ts << endl;
      if (ts < sc) {
        contests[day] = old;
        for (int i = day; i < d; i++)
          satis[i] = rec[i];
        continue;
      }
      sc = ts;
      for (int i = day; i < d; i++)
        rec[i] = satis[i];
      // cout << sc << endl;
    }
    // cout << sc << endl;
    else {
      int day1 = rand() % d;
      int day2 = rand() % d;
      if (day1 == day2)
        continue;
      // cout << day << " " << q << endl;
      // int old = contests[day];
      // contests[day] = q + 1;
      swap(contests[day1], contests[day2]);
      int day = min(day1, day2);
      int ts = score(d, s, c, contests, last, satis, day);
      // cout << ts << endl;
      if (ts < sc) {
        // contests[day] = old;
        swap(contests[day1], contests[day2]);
        for (int i = day; i < d; i++)
          satis[i] = rec[i];
        continue;
      }
      sc = ts;
      for (int i = day; i < d; i++)
        rec[i] = satis[i];
      // cout << sc << endl;
    }
  }
  for (int i = 0; i < d; i++)
    cout << contests[i] << endl;
}