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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int D;
int c[26];
int s[365][26];
int selected[365];
int scores[26];

int calculateScore(int t) {
  int retVal = 0;
  int last = -1;
  for (int i = 0; i < D; i++) {
    if (selected[i] == t) {
      last = i;
      retVal += s[i][t];
    } else {
      retVal -= c[t] * (i - last);
    }
  }
  return retVal;
}

int main() {
  clock_t start = clock();
  cin >> D;
  for (int i = 0; i < 26; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < D; i++)
    for (int j = 0; j < 26; j++) {
      cin >> s[i][j];
    }

  int last[26];
  memset(last, -1, sizeof(last));
  for (int i = 0; i < D; i++) {
    int maxNum = -(1e7);
    int maxArg = 0;
    for (int j = 0; j < 26; j++) {
      if (s[i][j] - c[j] * (i - last[j]) > maxNum) {
        maxNum = s[i][j] - c[j] * (i - last[j]);
        maxArg = j;
      }
    }
    // cerr <<"i=" <<i <<"maxArg=" << maxArg << "\\n";
    selected[i] = maxArg;
    last[maxArg] = i;
  }
  for (int i = 0; i < 26; i++) {
    scores[i] = calculateScore(i);
  }
  int itr = 1;
  while (clock() - start < (CLOCKS_PER_SEC * 2 - 100000)) {
    // cerr <<"itr =" << itr <<"\\n";
    itr++;
    int t = rand() % D;
    int oldq = selected[t];
    int maxdiff = 0;
    int maxArg = oldq;
    int oldQScoreDiff = 0;
    for (int q = 0; q < 26; q++) {
      if (q == oldq) {
        continue;
      }
      int oldscore = scores[q];
      selected[t] = q;
      if (oldQScoreDiff == 0) {
        oldQScoreDiff = calculateScore(oldq) - scores[oldq];
      }
      scores[q] = calculateScore(q);
      if (maxdiff < scores[q] - oldscore + oldQScoreDiff) {
        maxdiff = scores[q] - oldscore + oldQScoreDiff;
        maxArg = q;
      }
      scores[q] = oldscore;
      selected[t] = oldq;
    }
    // cerr<<"maxArg = " << maxArg <<"maxDiff=" << maxdiff << "\\n";
    selected[t] = maxArg;
    scores[oldq] += oldQScoreDiff;
    scores[maxArg] += (maxdiff - oldQScoreDiff);
    // cerr <<"oldScore=" << oldscore <<"newScore=" << newScore << "\\n";
  }
  for (int i = 0; i < D; i++) {
    cout << selected[i] + 1 << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int D;
int c[26];
int s[365][26];
int selected[365];
int scores[26];

int calculateScore(int t) {
  int retVal = 0;
  int last = -1;
  for (int i = 0; i < D; i++) {
    if (selected[i] == t) {
      last = i;
      retVal += s[i][t];
    } else {
      retVal -= c[t] * (i - last);
    }
  }
  return retVal;
}

int main() {
  clock_t start = clock();
  cin >> D;
  for (int i = 0; i < 26; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < D; i++)
    for (int j = 0; j < 26; j++) {
      cin >> s[i][j];
    }

  int last[26];
  memset(last, -1, sizeof(last));
  for (int i = 0; i < D; i++) {
    int maxNum = -(1e7);
    int maxArg = 0;
    for (int j = 0; j < 26; j++) {
      if (s[i][j] - c[j] * (i - last[j]) > maxNum) {
        maxNum = s[i][j] - c[j] * (i - last[j]);
        maxArg = j;
      }
    }
    cerr << "i=" << i << "maxArg=" << maxArg << "\\n";
    selected[i] = maxArg;
    last[maxArg] = i;
  }
  for (int i = 0; i < 26; i++) {
    scores[i] = calculateScore(i);
  }
  int itr = 1;
  while (clock() - start < (CLOCKS_PER_SEC * 2 - 10000)) {
    // cerr <<"itr =" << itr <<"\\n";
    itr++;
    int t = rand() % D;
    int oldq = selected[t];
    int maxdiff = 0;
    int maxArg = oldq;
    int oldQScoreDiff = 0;
    for (int q = 0; q < 26; q++) {
      if (q == oldq) {
        continue;
      }
      int oldscore = scores[q];
      selected[t] = q;
      if (oldQScoreDiff == 0) {
        oldQScoreDiff = calculateScore(oldq) - scores[oldq];
      }
      scores[q] = calculateScore(q);
      if (maxdiff < scores[q] - oldscore + oldQScoreDiff) {
        maxdiff = scores[q] - oldscore + oldQScoreDiff;
        maxArg = q;
      }
      scores[q] = oldscore;
      selected[t] = oldq;
    }
    // cerr<<"maxArg = " << maxArg <<"maxDiff=" << maxdiff << "\\n";
    selected[t] = maxArg;
    scores[oldq] += oldQScoreDiff;
    scores[maxArg] += (maxdiff - oldQScoreDiff);
    // cerr <<"oldScore=" << oldscore <<"newScore=" << newScore << "\\n";
  }
  for (int i = 0; i < D; i++) {
    cout << selected[i] + 1 << "\\n";
  }
  return 0;
}