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

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

ll d;
vector<ll> c(26 + 1);
vector<vector<ll>> s(365 + 1, vector<ll>(26 + 1));

ll scoring(vector<ll> t) {
  ll ans = 0;
  vector<ll> last(d + 1, 0);

  for (int i = 1; i <= d; i++) {
    ans += s[i][t[i]];
    last[t[i]] = i;

    for (int j = 1; j <= 26; j++) {
      ans -= c[j] * (i - last[j]);
    }
  }

  return ans;
}

int main(void) {
  auto start = chrono::system_clock::now();
  srand(time(NULL));
  cin >> d;

  for (int i = 1; i <= 26; i++) {
    cin >> c[i];
  }

  for (int i = 1; i <= d; i++) {
    for (int j = 1; j <= 26; j++) {
      cin >> s[i][j];
    }
  }

  vector<ll> BestT(d + 1);

  for (int i = 1; i <= d; i++) {
    BestT[i] = rand() % 26 + 1;
  }

  ll score = scoring(BestT);

  while (1) {
    vector<ll> t = BestT;

    int a, b;

    do {
      a = rand() % 26 + 1;
      b = rand() % 26 + 1;
    } while (a == b);

    swap(t[a], t[b]);

    if (scoring(t) > score) {
      BestT = t;
    }

    auto end = std::chrono::system_clock::now(); // 計測終了時刻を保存
    auto dur = end - start;                      // 要した時間を計算
    auto msec =
        std::chrono::duration_cast<std::chrono::milliseconds>(dur).count();

    if (msec >= 1900) {
      break;
    }
  }

  for (int i = 1; i <= d; i++) {
    printf("%lld\\n", BestT[i]);
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

ll d;
vector<ll> c(26 + 1);
vector<vector<ll>> s(365 + 1, vector<ll>(26 + 1));

ll scoring(vector<ll> t) {
  ll ans = 0;
  vector<ll> last(d + 1, 0);

  for (int i = 1; i <= d; i++) {
    ans += s[i][t[i]];
    last[t[i]] = i;

    for (int j = 1; j <= 26; j++) {
      ans -= c[j] * (i - last[j]);
    }
  }

  return ans;
}

int main(void) {
  clock_t start = clock();
  srand(time(NULL));
  cin >> d;

  for (int i = 1; i <= 26; i++) {
    cin >> c[i];
  }

  for (int i = 1; i <= d; i++) {
    for (int j = 1; j <= 26; j++) {
      cin >> s[i][j];
    }
  }

  vector<ll> BestT(d + 1);

  for (int i = 1; i <= d; i++) {
    BestT[i] = rand() % 26 + 1;
  }

  ll score = scoring(BestT);

  while ((clock() - start) / CLOCKS_PER_SEC < 2) {
    vector<ll> t = BestT;

    int a, b;

    do {
      a = rand() % 26 + 1;
      b = rand() % 26 + 1;
    } while (a == b);

    swap(t[a], t[b]);

    if (scoring(t) > score) {
      BestT = t;
    }
  }

  for (int i = 1; i <= d; i++) {
    printf("%lld\\n", BestT[i]);
  }
}