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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
using dd = long double;
const ll MOD = 1e9 + 7;
const dd PI = 3.1415926435;

class UnionFind {
public:
  UnionFind(int N) : par(N) {
    for (ll i = 0; i < N; i++) {
      par[i] = i;
    }
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    par[rx] = ry;
  }

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }

private:
  vector<int> par;
};

ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return b;
  }
  return gcd(b, a % b);
}

ll fact(ll n) {
  if (n <= 0) {
    return 1;
  }
  if (n == 1) {
    return 1;
  }
  return n * fact(n - 1) % MOD;
}

ll pow_fast(ll n, ll k) {
  if (k == 0) {
    return 1;
  }
  if (k == 1) {
    return n;
  }

  if (k % 2 == 0) {
    ll tmp = pow_fast(n, k / 2LL);
    return tmp * tmp % MOD;
  } else {
    ll tmp = pow_fast(n, k / 2LL);
    tmp *= tmp;
    tmp %= MOD;
    return tmp * n % MOD;
  }
}

map<ll, ll> sosuu(ll n) {
  map<ll, ll> res;
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      res[i] += 1;
      n /= i;
    }
  }
  if (n != 1) {
    res[n] += 1;
  }
  return res;
}

struct Dish {
  ll time;
  ll taste;
};

static ll FACTORIAL[10000010] = {};
ll nCk(ll n, ll k) {
  if (FACTORIAL[1] == 0) {
    FACTORIAL[0] = 1;
    FACTORIAL[1] = 1;
    for (ll i = 2; i <= 10000000; i++) {
      FACTORIAL[i] = FACTORIAL[i - 1] * i;
      FACTORIAL[i] %= MOD;
    }
  }
  ll N_fact = FACTORIAL[n];
  ll K_fact = pow_fast(FACTORIAL[k], MOD - 2);
  ll N_K_fact = pow_fast(FACTORIAL[n - k], MOD - 2);
  ll ret = N_fact;
  ret %= MOD;
  ret *= K_fact;
  ret %= MOD;
  ret *= N_K_fact;
  ret %= MOD;
  return ret;
}

void solve(string s, ll K) {
  ll sum = 0;
  for (ll i = s.size(); i <= s.size() + K; i++) {
    // 25
    ll cnt = pow_fast(25, i - s.size()) % MOD;
    cnt *= nCk(i - 1, s.size() - 1);
    cnt %= MOD;

    // 26
    cnt *= pow_fast(26, s.size() + K - i);
    cnt %= MOD;

    sum += cnt;
    sum %= MOD;
  }
  cout << sum << endl;
  return;
}

vector<string> explode(string s, char delim) {
  vector<string> ret;
  string cur;
  for (ll i = 0; i < s.size(); i++) {
    if (s[i] == delim) {
      ret.push_back(cur);
      cur.clear();
      continue;
    }
    cur += s[i];
  }

  if (cur.size() != 0) {
    ret.push_back(cur);
  }

  return ret;
}

template <typename T> void DEBUG(T var) {
  static size_t cnt = 1;
  cout << "[DEBUG] " << cnt << " :";
  cout << "(" << typeid(var).name() << ")";
  cout << var << endl;
  cnt += 1;
}

struct Element {
  char c;
  bool visited;
  ll cost;
};

const ll D = 365;
vector<ll> C(26);
vector<vector<ll>> S(D + 1, vector<ll>(26));
vector<vector<ll>> LAST(D + 1, vector<ll>(26, 0));
vector<ll> CONTEST(D + 1);

int main() {
  ll d;
  cin >> d;

  for (ll i = 0; i < 26; i++) {
    cin >> C[i];
  }
  for (ll i = 0; i < D; i++) {
    for (ll j = 0; j < 26; j++) {
      cin >> S[i][j];
    }
  }

  vector<ll> SATISFY(D, 0);

  // ベンチマーク
  for (ll i = 0; i < D; i++) {
    ll contest = 0;
    ll mx = INT_MIN;
    for (ll t = 0; t < 26; t++) {
      ll satis = 0;
      satis += S[i][t];
      for (ll j = 0; j < 26; j++) {
        if (j == t) {
          continue;
        }
        if (i == 0) {
          satis -= C[j] * (i + 1 - 0);
        } else {
          satis -= C[j] * (i + 1 - LAST[i - 1][j]);
        }
      }
      if (satis > mx) {
        mx = satis;
        contest = t;
      }
    }

    CONTEST[i] = contest;
    if (i != 0) {
      LAST[i] = LAST[i - 1];
    }
    LAST[i][contest] = i + 1;

    if (i == 0) {
      SATISFY[i] = mx;
    } else {
      SATISFY[i] = SATISFY[i - 1] + mx;
    }
  }

  clock_t start = clock();
  clock_t end = clock();
  dd time = (end - start) / CLOCKS_PER_SEC;

  while (time <= 1.5) {
    vector<ll> NEW_SATISFY = SATISFY;
    vector<vector<ll>> NEW_LAST = LAST;
    vector<ll> NEW_CONTEST = CONTEST;

    ll random = rand() % D;
    ll t = rand() % 26;
    NEW_CONTEST[random] = t;
    NEW_LAST[random][t] = random + 1;

    if (random != 0) {
      NEW_LAST[random][CONTEST[random]] = LAST[random - 1][CONTEST[random]];
    }
    for (size_t i = random + 1; i < D; i++) {
      ll contest = 0;
      ll mx = INT_MIN;
      for (ll t = 0; t < 26; t++) {
        ll satis = 0;
        satis += S[i][t];
        for (ll j = 0; j < 26; j++) {
          if (j == t) {
            continue;
          }
          if (i == 0) {
            satis -= C[j] * (i + 1 - 0);
          } else {
            satis -= C[j] * (i + 1 - NEW_LAST[i - 1][j]);
          }
        }
        if (satis > mx) {
          mx = satis;
          contest = t;
        }
      }

      NEW_CONTEST[i] = contest;
      if (i != 0) {
        NEW_LAST[i] = NEW_LAST[i - 1];
      }
      NEW_LAST[i][contest] = i + 1;

      if (i == 0) {
        NEW_SATISFY[i] = mx;
      } else {
        NEW_SATISFY[i] = NEW_SATISFY[i - 1] + mx;
      }
    }

    if (NEW_SATISFY[D - 1] > SATISFY[D - 1]) {
      SATISFY = NEW_SATISFY;
      LAST = NEW_LAST;
      CONTEST = NEW_CONTEST;
    }
    end = clock();
    time = (dd)(end - start) / CLOCKS_PER_SEC;
  }

  for (ll i = 0; i < D; i++) {
    cout << (CONTEST[i] + 1) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
using dd = long double;
const ll MOD = 1e9 + 7;
const dd PI = 3.1415926435;

class UnionFind {
public:
  UnionFind(int N) : par(N) {
    for (ll i = 0; i < N; i++) {
      par[i] = i;
    }
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    par[rx] = ry;
  }

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }

private:
  vector<int> par;
};

ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return b;
  }
  return gcd(b, a % b);
}

ll fact(ll n) {
  if (n <= 0) {
    return 1;
  }
  if (n == 1) {
    return 1;
  }
  return n * fact(n - 1) % MOD;
}

ll pow_fast(ll n, ll k) {
  if (k == 0) {
    return 1;
  }
  if (k == 1) {
    return n;
  }

  if (k % 2 == 0) {
    ll tmp = pow_fast(n, k / 2LL);
    return tmp * tmp % MOD;
  } else {
    ll tmp = pow_fast(n, k / 2LL);
    tmp *= tmp;
    tmp %= MOD;
    return tmp * n % MOD;
  }
}

map<ll, ll> sosuu(ll n) {
  map<ll, ll> res;
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      res[i] += 1;
      n /= i;
    }
  }
  if (n != 1) {
    res[n] += 1;
  }
  return res;
}

struct Dish {
  ll time;
  ll taste;
};

static ll FACTORIAL[10000010] = {};
ll nCk(ll n, ll k) {
  if (FACTORIAL[1] == 0) {
    FACTORIAL[0] = 1;
    FACTORIAL[1] = 1;
    for (ll i = 2; i <= 10000000; i++) {
      FACTORIAL[i] = FACTORIAL[i - 1] * i;
      FACTORIAL[i] %= MOD;
    }
  }
  ll N_fact = FACTORIAL[n];
  ll K_fact = pow_fast(FACTORIAL[k], MOD - 2);
  ll N_K_fact = pow_fast(FACTORIAL[n - k], MOD - 2);
  ll ret = N_fact;
  ret %= MOD;
  ret *= K_fact;
  ret %= MOD;
  ret *= N_K_fact;
  ret %= MOD;
  return ret;
}

void solve(string s, ll K) {
  ll sum = 0;
  for (ll i = s.size(); i <= s.size() + K; i++) {
    // 25
    ll cnt = pow_fast(25, i - s.size()) % MOD;
    cnt *= nCk(i - 1, s.size() - 1);
    cnt %= MOD;

    // 26
    cnt *= pow_fast(26, s.size() + K - i);
    cnt %= MOD;

    sum += cnt;
    sum %= MOD;
  }
  cout << sum << endl;
  return;
}

vector<string> explode(string s, char delim) {
  vector<string> ret;
  string cur;
  for (ll i = 0; i < s.size(); i++) {
    if (s[i] == delim) {
      ret.push_back(cur);
      cur.clear();
      continue;
    }
    cur += s[i];
  }

  if (cur.size() != 0) {
    ret.push_back(cur);
  }

  return ret;
}

template <typename T> void DEBUG(T var) {
  static size_t cnt = 1;
  cout << "[DEBUG] " << cnt << " :";
  cout << "(" << typeid(var).name() << ")";
  cout << var << endl;
  cnt += 1;
}

struct Element {
  char c;
  bool visited;
  ll cost;
};

const ll D = 365;
vector<ll> C(26);
vector<vector<ll>> S(D + 1, vector<ll>(26));
vector<vector<ll>> LAST(D + 1, vector<ll>(26, 0));
vector<ll> CONTEST(D + 1);

int main() {
  ll d;
  cin >> d;

  for (ll i = 0; i < 26; i++) {
    cin >> C[i];
  }
  for (ll i = 0; i < D; i++) {
    for (ll j = 0; j < 26; j++) {
      cin >> S[i][j];
    }
  }

  vector<ll> SATISFY(D, 0);

  // ベンチマーク
  for (ll i = 0; i < D; i++) {
    ll contest = 0;
    ll mx = INT_MIN;
    for (ll t = 0; t < 26; t++) {
      ll satis = 0;
      satis += S[i][t];
      for (ll j = 0; j < 26; j++) {
        if (j == t) {
          continue;
        }
        if (i == 0) {
          satis -= C[j] * (i + 1 - 0);
        } else {
          satis -= C[j] * (i + 1 - LAST[i - 1][j]);
        }
      }
      if (satis > mx) {
        mx = satis;
        contest = t;
      }
    }

    CONTEST[i] = contest;
    if (i != 0) {
      LAST[i] = LAST[i - 1];
    }
    LAST[i][contest] = i + 1;

    if (i == 0) {
      SATISFY[i] = mx;
    } else {
      SATISFY[i] = SATISFY[i - 1] + mx;
    }
  }

  clock_t start = clock();
  clock_t end = clock();
  dd time = (end - start) / CLOCKS_PER_SEC;

  while (time <= 1.0) {
    vector<ll> NEW_SATISFY = SATISFY;
    vector<vector<ll>> NEW_LAST = LAST;
    vector<ll> NEW_CONTEST = CONTEST;

    ll random = rand() % D;
    ll t = rand() % 26;
    NEW_CONTEST[random] = t;
    NEW_LAST[random][t] = random + 1;

    if (random != 0) {
      NEW_LAST[random][CONTEST[random]] = LAST[random - 1][CONTEST[random]];
    }
    for (size_t i = random + 1; i < D; i++) {
      ll contest = 0;
      ll mx = INT_MIN;
      for (ll t = 0; t < 26; t++) {
        ll satis = 0;
        satis += S[i][t];
        for (ll j = 0; j < 26; j++) {
          if (j == t) {
            continue;
          }
          if (i == 0) {
            satis -= C[j] * (i + 1 - 0);
          } else {
            satis -= C[j] * (i + 1 - NEW_LAST[i - 1][j]);
          }
        }
        if (satis > mx) {
          mx = satis;
          contest = t;
        }
      }

      NEW_CONTEST[i] = contest;
      if (i != 0) {
        NEW_LAST[i] = NEW_LAST[i - 1];
      }
      NEW_LAST[i][contest] = i + 1;

      if (i == 0) {
        NEW_SATISFY[i] = mx;
      } else {
        NEW_SATISFY[i] = NEW_SATISFY[i - 1] + mx;
      }
    }

    if (NEW_SATISFY[D - 1] > SATISFY[D - 1]) {
      SATISFY = NEW_SATISFY;
      LAST = NEW_LAST;
      CONTEST = NEW_CONTEST;
    }
    end = clock();
    time = (end - start) / CLOCKS_PER_SEC;
  }

  for (ll i = 0; i < D; i++) {
    cout << (CONTEST[i] + 1) << endl;
  }
}