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

#include <bits/stdc++.h>
using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
//------------------------------------------
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<char> VC;
typedef vector<VC> VVC;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef priority_queue<int> PQGI; // 大きい順
typedef priority_queue<int, VI, greater<int>> PQLI;
typedef priority_queue<PII> PQGP;
typedef priority_queue<PII, VPII, greater<PII>> PQLP;
// priority_queue<T,vector<T>,decltype(f)> pq{f};

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EB emplace_back
#define EF emplace_front
#define PB push_back
#define PF push_front
#define POB pop_back
#define POF pop_front
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define SORTR(c) sort((c).rbegin(), (c).rend())
#define LB lower_bound
#define UB upper_bound
#define NEXP next_permutation
#define FI first
#define SE second
#define Vmin(a) *min_element((a).begin(), (a).end())
#define Vmax(a) *max_element((a).begin(), (a).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORR(i, a, b) for (int i = (b - 1); i >= (a); i--)
#define REPR(i, n) FORR(i, 0, n)
#define CFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define CREP(i, n) CFOR(i, 0, n)
#define CFORR(i, a, b) for (int i = (b); i >= (a); i--)
#define CREPR(i, n) CFORR(i, 0, n)
#define BFOR(bit, a, b) for (int bit = (a); bit < (1 << (b)); ++bit)
#define BREP(bit, n) BFOR(bit, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 2;
const LL LINF = LLONG_MAX / 3;
const int RINF = INT_MIN / 2;
const LL RLINF = LLONG_MIN / 3;
const LL MOD = 1e9 + 7;
const LL MODD = 998244353;
const int MAX = 510000;

inline bool Eq(double a, double b) { return fabs(b - a) < EPS; }

// other
//-------------------------------------------
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

#define COUT(x) cout << (x) << endl
#define COUT2(x, y) cout << (x) << " " << (y) << endl
#define COUT3(x, y, z) cout << (x) << " " << (y) << " " << (z) << endl
#define PR(x) cout << (x)
#define ENDL cout << endl
#define SPACE cout << " "
#define BC(x) __builtin_popcountll(x)

VI dx = {1, 0, -1, 0, 1, 1, -1, -1};
VI dy = {0, 1, 0, -1, 1, -1, 1, -1};

LL Gcd(LL a, LL b) { return __gcd(a, b); }
LL Lcm(LL a, LL b) { return a / Gcd(a, b) * b; }

LL ModPow(LL A, LL N, LL M = MOD) {
  LL res = 1;
  while (N > 0) {
    if (N & 1)
      res = res * A % M;
    A = A * A % M;
    N >>= 1;
  }
  return res;
}

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << p.first << " " << p.second;
  return os;
}

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v)
    is >> in;
  return is;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < SZ(v); i++) {
    os << v[i] << (i + 1 != SZ(v) ? " " : "");
  }
  return os;
}

template <typename T> vector<T> make_v(size_t a, T b) {
  return vector<T>(a, b);
}
template <typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

struct Timer {
  chrono::high_resolution_clock::time_point st;

  Timer() { reset(); }

  void reset() { st = chrono::high_resolution_clock::now(); }

  chrono::milliseconds::rep elapsed() {
    auto ed = chrono::high_resolution_clock::now();
    return chrono::duration_cast<chrono::milliseconds>(ed - st).count();
  }
};

VI c;
VVI s;

struct Solve {
  int D, N;
  VI T;
  VVI shedule;
  VI score, penalty;

  Solve(int D, int N, VI T) : D(D), N(N), T(T) {
    shedule.assign(N, VI(1, 0));
    score.assign(N, 0);
    penalty.assign(N, 0);
    REP(i, D) { shedule.at(T.at(i)).PB(i + 1); }
    REP(i, N) shedule.at(i).PB(D + 1);

    REP(i, D) { score.at(T.at(i)) += s.at(i).at(T.at(i)); }
    REP(i, N) {
      int m = SZ(shedule.at(i));
      REP(j, m - 1) {
        int d = shedule.at(i).at(j + 1) - shedule.at(i).at(j);
        penalty.at(i) -= c.at(i) * d * (d - 1) / 2;
      }
    }
  }

  int getScore() {
    int res = 0;
    for (auto e : score)
      res += e;
    for (auto e : penalty)
      res += e;
    return res;
  }

  void change(int k, int nt) {
    int ot = T.at(k);
    T.at(k) = nt;

    score.at(ot) -= s.at(k).at(ot);
    score.at(nt) += s.at(k).at(nt);

    int m = SZ(shedule.at(ot));
    int d;
    REP(i, m) {
      if (shedule.at(ot).at(i) == k + 1) {
        d = shedule.at(ot).at(i) - shedule.at(ot).at(i - 1);
        penalty.at(ot) += c.at(ot) * d * (d - 1) / 2;
        d = shedule.at(ot).at(i + 1) - shedule.at(ot).at(i);
        penalty.at(ot) += c.at(ot) * d * (d - 1) / 2;
        d = shedule.at(ot).at(i + 1) - shedule.at(ot).at(i - 1);
        penalty.at(ot) -= c.at(ot) * d * (d - 1) / 2;

        shedule.at(ot).erase(shedule.at(ot).begin() + i);
        break;
      }
    }
    m = SZ(shedule.at(nt));
    REP(i, m) {
      if (shedule.at(nt).at(i) > k + 1) {

        d = shedule.at(nt).at(i) - shedule.at(nt).at(i - 1);
        penalty.at(nt) += c.at(nt) * d * (d - 1) / 2;
        d = shedule.at(nt).at(i) - (k + 1);
        penalty.at(nt) -= c.at(nt) * d * (d - 1) / 2;
        d = (k + 1) - shedule.at(nt).at(i - 1);
        penalty.at(nt) -= c.at(nt) * d * (d - 1) / 2;

        shedule.at(nt).insert(shedule.at(nt).begin() + i, k + 1);
        break;
      }
    }
  }

  void tswap(int k, int l) {
    int kt = T.at(k);
    int lt = T.at(l);
    change(k, lt);
    change(l, kt);
  }
};

int main() {

  // cin.tie(0);
  // ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);

  Timer timer;

  int D;
  cin >> D;
  int N = 26;
  c.assign(N, 0);
  cin >> c;
  s.assign(D, VI(N, 0));
  cin >> s;

  VI T(D); // cin >> T;
  // REP(i,D) T.at(i)--;

  srand(time(NULL));

  REP(i, D) T.at(i) = rand() % N;

  Solve solve(D, N, T);

  while (timer.elapsed() < 1900) {
    int t = rand() % 2;

    int sco = solve.getScore();

    if (t) {
      int k = rand() % D;
      int nt = rand() % N;
      int ot = T.at(k);
      solve.change(k, nt);
      int nsc = solve.getScore();
      if (sco > nsc) {
        solve.change(k, ot);
      } else {
        T.at(k) = nt;
      }
    } else {
      int l = rand() % D;
      int r = rand() % D;
      solve.tswap(l, r);
      int nsc = solve.getScore();
      if (sco > nsc) {
        solve.tswap(l, r);
      } else {
        swap(T.at(l), T.at(r));
      }
    }
  }

  REP(i, D) T.at(i)++;

  COUT(T);

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
//------------------------------------------
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<char> VC;
typedef vector<VC> VVC;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef priority_queue<int> PQGI; // 大きい順
typedef priority_queue<int, VI, greater<int>> PQLI;
typedef priority_queue<PII> PQGP;
typedef priority_queue<PII, VPII, greater<int>> PQLP;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EB emplace_back
#define PB push_back
#define PF push_front
#define POB pop_back
#define POF pop_front
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define SORTR(c) sort((c).rbegin(), (c).rend())
#define LB lower_bound
#define UB upper_bound
#define NEXP next_permutation
#define FI first
#define SE second
#define Vmin(a) *min_element((a).begin(), (a).end())
#define Vmax(a) *max_element((a).begin(), (a).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORR(i, a, b) for (int i = (b - 1); i >= (a); i--)
#define REPR(i, n) FORR(i, 0, n)
#define CFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define CREP(i, n) CFOR(i, 0, n)
#define CFORR(i, a, b) for (int i = (b); i >= (a); i--)
#define CREPR(i, n) CFORR(i, 0, n)
#define BFOR(bit, a, b) for (int bit = (a); bit < (1 << (b)); ++bit)
#define BREP(bit, n) BFOR(bit, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 2;
const LL LINF = LLONG_MAX / 3;
const int RINF = INT_MIN / 2;
const LL RLINF = LLONG_MIN / 3;
const LL MOD = 1e9 + 7;
const LL MODD = 998244353;
const int MAX = 510000;

inline bool Eq(double a, double b) { return fabs(b - a) < EPS; }

// other
//-------------------------------------------
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

#define COUT(x) cout << (x) << endl
#define COUT2(x, y) cout << (x) << " " << (y) << endl
#define COUT3(x, y, z) cout << (x) << " " << (y) << " " << (z) << endl
#define PR(x) cout << (x)
#define ENDL cout << endl
#define SPACE cout << " "
#define BC(x) __builtin_popcountll(x)

VI dx = {1, 0, -1, 0, 1, 1, -1, -1};
VI dy = {0, 1, 0, -1, 1, -1, 1, -1};
VC dc = {'R', 'D', 'L', 'U'};
VS ds = {"R", "D", "L", "U", "RD", "RU", "LD", "LU"};

LL Gcd(LL a, LL b) { return __gcd(a, b); }
LL Lcm(LL a, LL b) { return a / Gcd(a, b) * b; }

LL ModPow(LL A, LL N, LL M) {
  LL res = 1;
  while (N > 0) {
    if (N & 1)
      res = res * A % M;
    A = A * A % M;
    N >>= 1;
  }
  return res;
}

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v)
    is >> in;
  return is;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

int main() {

  // cin.tie(0);
  // ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);

  int D;
  cin >> D;
  int N = 26;
  VI C(N);
  cin >> C;
  VVI S(D, VI(N));
  REP(i, D) cin >> S.at(i);

  VI T(D); // cin >> T;
  srand(time(NULL));

  REP(i, D) T.at(i) = rand() % N + 1;

  int M; // cin >> M;
  M = 2e5;

  int ascore = 0;

  VI last(N, -1);

  REP(i, D) {
    int t = T.at(i) - 1;
    ascore += S.at(i).at(t);
    last.at(t) = i;
    REP(j, N) { ascore -= (i - last.at(j)) * C.at(j); }
  }

  // COUT(ascore);

  REP(m, M) {

    int d, q; // cin >> d >> q;

    d = rand() % D + 1;
    q = rand() % N + 1;
    int past = T.at(d - 1);
    T.at(d - 1) = q;

    int score = 0;

    VI last(N, -1);

    REP(i, D) {
      int t = T.at(i) - 1;
      score += S.at(i).at(t);
      last.at(t) = i;
      REP(j, N) { score -= (i - last.at(j)) * C.at(j); }
    }

    // COUT(score);
    if (ascore > score) {
      T.at(d - 1) = past;
    } else {
      ascore = score;
    }
  }

  REP(i, D) COUT(T.at(i));

  return 0;
}