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

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

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using vint = vector<int>;
using vvint = vector<vint>;
using vvvint = vector<vvint>;
using vdouble = vector<double>;
using vvdouble = vector<vdouble>;
using vvvdouble = vector<vvdouble>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using uint = unsigned int;
using ull = unsigned long long;

template <typename T> using uset = unordered_set<T>;
template <typename T1, typename T2> using umap = unordered_map<T1, T2>;

constexpr int INF = (1 << 30) - 1;
constexpr ll LLINF = 1LL << 60;
constexpr int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr char el = '\\n';
constexpr int mod = 1000000007;
constexpr int mod2 = 998244353;
template <typename T> T gcd(T a, T b) { return (b ? gcd(b, a % b) : a); }
template <typename T> T lcm(T a, T b) { return (a / gcd(a, b) * b); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return (a > b && (a = b, true));
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return (a < b && (a = b, true));
}

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

template <typename T> bool isin(T y, T x, T h, T w) {
  return (0 <= y && 0 <= x && y < h && x < w);
}
template <typename T> bool isin1(T y, T x, T h, T w) {
  return (0 < y && 0 < x && y <= h && x <= w);
}

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

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

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

int D;
vll C, T;
vvll S;

inline ll calcScore() {
  vll last(26, -1);
  ll score = 0;
  for (int i = 0; i < D; i++) {
    score += S[i][T[i]];
    last[T[i]] = i;
    for (int j = 0; j < 26; j++) {
      score -= (i - last[j]) * C[j];
    }
  }
  return (score);
}

void Main() {
  cin >> D;
  C = makeVector(26, 0ll);
  cin >> C;
  S = makeVector(D, 26, 0ll);
  cin >> S;
  T = makeVector(D, 0ll);

  vll last(26, -1);
  for (int i = 0; i < D; i++) {
    ll maxp = -1, maxd = -LLINF;
    ll decr = 0;
    for (int k = 0; k < 26; k++) {
      decr += (i - last[k]) * C[k];
    }
    for (int j = 0; j < 26; j++) {
      ll sc = S[i][j] - decr + (i - last[j]) * C[j];
      if (maxd < sc) {
        maxp = j;
        maxd = sc;
      }
      // cout << sc << " ";
    }
    // cout << endl;
    T[i] = maxp;
    last[T[i]] = i;
  }
  // cout << T << endl;
  // cout << calcScore() << endl;

  ll maxv = -LLINF;
  int M = 120000;
  for (int i = 0; i < M; i++) {
    int d = rand() % D;
    int q = rand() % 26;
    ll tmp = T[d];
    T[d] = q;
    ll score = calcScore();
    if (score <= maxv)
      T[d] = tmp;
    else
      maxv = score;
  }

  for (int i = 0; i < D; i++) {
    cout << T[i] + 1 << el;
  }
  // cout << maxv << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
  Main();
  return (0);
}


Error: TLE

Fixed submission:

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

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using vint = vector<int>;
using vvint = vector<vint>;
using vvvint = vector<vvint>;
using vdouble = vector<double>;
using vvdouble = vector<vdouble>;
using vvvdouble = vector<vvdouble>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using uint = unsigned int;
using ull = unsigned long long;

template <typename T> using uset = unordered_set<T>;
template <typename T1, typename T2> using umap = unordered_map<T1, T2>;

constexpr int INF = (1 << 30) - 1;
constexpr ll LLINF = 1LL << 60;
constexpr int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr char el = '\\n';
constexpr int mod = 1000000007;
constexpr int mod2 = 998244353;
template <typename T> T gcd(T a, T b) { return (b ? gcd(b, a % b) : a); }
template <typename T> T lcm(T a, T b) { return (a / gcd(a, b) * b); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return (a > b && (a = b, true));
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return (a < b && (a = b, true));
}

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

template <typename T> bool isin(T y, T x, T h, T w) {
  return (0 <= y && 0 <= x && y < h && x < w);
}
template <typename T> bool isin1(T y, T x, T h, T w) {
  return (0 < y && 0 < x && y <= h && x <= w);
}

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

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

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

int D;
vll C, T;
vvll S;

inline ll calcScore() {
  vll last(26, -1);
  ll score = 0;
  for (int i = 0; i < D; i++) {
    score += S[i][T[i]];
    last[T[i]] = i;
    for (int j = 0; j < 26; j++) {
      score -= (i - last[j]) * C[j];
    }
  }
  return (score);
}

void Main() {
  cin >> D;
  C = makeVector(26, 0ll);
  cin >> C;
  S = makeVector(D, 26, 0ll);
  cin >> S;
  T = makeVector(D, 0ll);

  vll last(26, -1);
  for (int i = 0; i < D; i++) {
    ll maxp = -1, maxd = -LLINF;
    for (int j = 0; j < 26; j++) {
      ll sc = S[i][j];
      for (int k = 0; k < 26; k++) {
        if (j != k)
          sc -= (i - last[k]) * C[k];
      }
      if (maxd < sc) {
        maxp = j;
        maxd = sc;
      }
      // cout << sc << " ";
    }
    // cout << endl;
    T[i] = maxp;
    last[T[i]] = i;
  }
  // cout << T << endl;
  // cout << calcScore() << endl;

  ll maxv = -LLINF;
  int M = 240000;
  for (int i = 0; i < M; i++) {
    int d = rand() % D;
    int q = rand() % 26;
    ll tmp = T[d];
    T[d] = q;
    ll score = calcScore();
    if (score <= maxv)
      T[d] = tmp;
    else
      maxv = score;
  }

  for (int i = 0; i < D; i++) {
    cout << T[i] + 1 << el;
  }
  // cout << maxv << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
  Main();
  return (0);
}