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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int D;
  cin >> D;

  vector<int> c(27), tmp(27);
  vector<vector<int>> s(D + 1, vector<int>(27));
  for (int i = 1; i <= 26; i++)
    cin >> c.at(i);
  for (int i = 1; i <= D; i++)
    for (int j = 1; j <= 26; j++) {
      cin >> s.at(i).at(j);
    }

  vector<int> last(27); // 最終開催日
  for (int i = 1; i <= 26; i++)
    last.at(i) = 0;

  vector<int> t(D + 1); // 開催テストタイプ
  for (int i = 1; i <= D; i++)
    cin >> t.at(i);

  vector<int> dd(100 * D + 1), qq(100 * D + 1);
  for (int i = 1; i <= 100 * D; i++) {
    qq.at(i) = rand() % 26;
    dd.at(i) = rand() % D;
  }

  // 初期計算
  for (int i = 1; i <= D; i++) {
    int typ = 0;
    int smax = 0, stf;
    for (int j = 1; j <= 26; j++) {
      stf = s.at(i).at(j) + (i - last.at(j)) * c.at(j);
      if (stf > smax) {
        smax = stf;
        typ = j;
      }
    }

    last.at(typ) = i;

    t.at(i) = typ;
  }

  vector<int> stf(D + 1);
  int sf = 0;
  for (int i = 1; i <= D; i++) {
    sf += s.at(i).at(t.at(i));
    last.at(t.at(i)) = i;

    for (int j = 1; j <= 26; j++) {
      sf -= c.at(j) * (i - last.at(j));
    }
    stf.at(i) = sf;
  }

  vector<int> dmaxtyp(D + 1);
  for (int i = 1; i <= D; i++) {
    int mmax = 0, tmp = 0;
    for (int j = 1; j <= 26; j++) {
      if (mmax < s.at(i).at(j)) {
        tmp = j;
        mmax = s.at(i).at(j);
      }
    }
    dmaxtyp.at(i) = tmp;
  }

  // 局所探索?
  for (int k = 1; k <= 100 * D; k++) {
    int oldsf = sf;
    int oldtyp = t.at(dd.at(k));
    sf = 0;
    t.at(dd.at(k)) = qq.at(k);
    for (int i = 1; i <= 26; i++)
      last.at(i) = 0;

    for (int i = 1; i <= D; i++) {
      sf += s.at(i).at(t.at(i));
      last.at(t.at(i)) = i;

      for (int j = 1; j <= 26; j++) {
        sf -= c.at(j) * (i - last.at(j));
      }
    }
    if (oldsf > sf) {
      t.at(dd.at(k)) = oldtyp;
      sf = oldsf;
    }
  }
  // cout << sf << endl;
  for (int i = 1; i <= D; i++)
    cout << t.at(i) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int D;
  cin >> D;

  vector<int> c(27), tmp(27);
  vector<vector<int>> s(D + 1, vector<int>(27));
  for (int i = 1; i <= 26; i++)
    cin >> c.at(i);
  for (int i = 1; i <= D; i++)
    for (int j = 1; j <= 26; j++) {
      cin >> s.at(i).at(j);
    }

  vector<int> last(27); // 最終開催日
  for (int i = 1; i <= 26; i++)
    last.at(i) = 0;

  vector<int> t(D + 1); // 開催テストタイプ
  for (int i = 1; i <= D; i++)
    cin >> t.at(i);

  vector<int> dd(1000 * D + 1), qq(1000 * D + 1);
  for (int i = 1; i <= 1000 * D; i++) {
    qq.at(i) = rand() % 26;
    dd.at(i) = rand() % D;
  }

  // 初期計算
  for (int i = 1; i <= D; i++) {
    int typ = 0;
    int smax = 0, stf;
    for (int j = 1; j <= 26; j++) {
      stf = s.at(i).at(j) + (i - last.at(j)) * c.at(j);
      if (stf > smax) {
        smax = stf;
        typ = j;
      }
    }

    last.at(typ) = i;

    t.at(i) = typ;
  }

  vector<int> stf(D + 1);
  int sf = 0;
  for (int i = 1; i <= D; i++) {
    sf += s.at(i).at(t.at(i));
    last.at(t.at(i)) = i;

    for (int j = 1; j <= 26; j++) {
      sf -= c.at(j) * (i - last.at(j));
    }
    stf.at(i) = sf;
  }

  vector<int> dmaxtyp(D + 1);
  for (int i = 1; i <= D; i++) {
    int mmax = 0, tmp = 0;
    for (int j = 1; j <= 26; j++) {
      if (mmax < s.at(i).at(j)) {
        tmp = j;
        mmax = s.at(i).at(j);
      }
    }
    dmaxtyp.at(i) = tmp;
  }

  // 局所探索?
  for (int k = 1; k <= 1000 * D; k++) {
    int oldsf = sf;
    int oldtyp = t.at(dd.at(k));
    sf = 0;
    t.at(dd.at(k)) = qq.at(k);
    for (int i = 1; i <= 26; i++)
      last.at(i) = 0;

    for (int i = 1; i <= D; i++) {
      sf += s.at(i).at(t.at(i));
      last.at(t.at(i)) = i;

      for (int j = 1; j <= 26; j++) {
        sf -= c.at(j) * (i - last.at(j));
      }
    }
    if (oldsf > sf) {
      t.at(dd.at(k)) = oldtyp;
      sf = oldsf;
    }
  }
  cout << sf << endl;
  for (int i = 1; i <= D; i++)
    cout << t.at(i) << endl;
}