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

#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
#define int long long
#define all(a) (a).begin(), (a).end()
#define fs first
#define sc second
#define xx first
#define yy second.first
#define zz second.second
#define H pair<int, int>
#define P pair<int, pair<int, int>>
#define Q(i, j, k) mkp(i, mkp(j, k))
#define rng(i, s, n) for (int i = (s); i < (n); i++)
#define rep(i, n) rng(i, 0, (n))
#define mkp make_pair
#define vec vector
#define vi vec<int>
#define pb emplace_back
#define siz(a) (int)(a).size()
#define crdcomp(b)                                                             \\
  sort(all((b)));                                                              \\
  (b).erase(unique(all((b))), (b).end())
#define getidx(b, i) lower_bound(all(b), (i)) - (b).begin()
#define ssp(i, n) (i == (int)(n)-1 ? "\\n" : " ")
#define ctoi(c) (int)(c - '0')
#define itoc(c) (char)(c + '0')
#define cyes printf("Yes\\n")
#define cno printf("No\\n")
#define cdf(n)                                                                 \\
  int quetimes_ = (n);                                                         \\
  rep(qq123_, quetimes_)
#define gcj printf("Case #%lld: ", qq123_ + 1)
#define readv(a, n)                                                            \\
  a.resize(n, 0);                                                              \\
  rep(i, (n)) a[i] = read()
#define found(a, x) (a.find(x) != a.end())
// #define endl "\\n"
constexpr int mod = 1e9 + 7;
constexpr int Mod = 998244353;
constexpr ld EPS = 1e-10;
constexpr ll inf = 3 * 1e18;
constexpr int Inf = 15 * 1e8;
constexpr int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
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;
}
ll read() {
  ll u, k = scanf("%lld", &u);
  return u;
}
string reads() {
  string s;
  cin >> s;
  return s;
}
H readh(bool g = 0) {
  H u;
  int k = scanf("%lld %lld", &u.fs, &u.sc);
  if (g)
    u.fs--, u.sc--;
  return u;
}
bool ina(H t, int h, int w) {
  return 0 <= t.fs && t.fs < h && 0 <= t.sc && t.sc < w;
}
bool ina(int t, int l, int r) { return l <= t && t < r; }
ll gcd(ll i, ll j) { return j ? gcd(j, i % j) : i; }
ll popcount(ll x) {
  int sum = 0;
  for (int i = 0; i < 60; i++)
    if ((1ll << i) & x)
      sum++;
  return sum;
}
class mint {
public:
  ll v;
  mint(ll v = 0) { s(v % mod + mod); }
  constexpr static int mod = 1e9 + 7;
  constexpr static int fn_ = 6e5 + 5;
  static mint fact[fn_], comp[fn_];
  mint pow(int x) const {
    mint b(v), c(1);
    while (x) {
      if (x & 1)
        c *= b;
      b *= b;
      x >>= 1;
    }
    return c;
  }
  inline mint &s(int vv) {
    v = vv < mod ? vv : vv - mod;
    return *this;
  }
  inline mint inv() const { return pow(mod - 2); }
  inline mint operator-() const { return mint() - *this; }
  inline mint &operator+=(const mint b) { return s(v + b.v); }
  inline mint &operator-=(const mint b) { return s(v + mod - b.v); }
  inline mint &operator*=(const mint b) {
    v = v * b.v % mod;
    return *this;
  }
  inline mint &operator/=(const mint b) {
    v = v * b.inv().v % mod;
    return *this;
  }
  inline mint operator+(const mint b) const { return mint(v) += b; }
  inline mint operator-(const mint b) const { return mint(v) -= b; }
  inline mint operator*(const mint b) const { return mint(v) *= b; }
  inline mint operator/(const mint b) const { return mint(v) /= b; }
  friend ostream &operator<<(ostream &os, const mint &m) { return os << m.v; }
  friend istream &operator>>(istream &is, mint &m) {
    int x;
    is >> x;
    m = mint(x);
    return is;
  }
  bool operator<(const mint &r) const { return v < r.v; }
  bool operator>(const mint &r) const { return v > r.v; }
  bool operator<=(const mint &r) const { return v <= r.v; }
  bool operator>=(const mint &r) const { return v >= r.v; }
  bool operator==(const mint &r) const { return v == r.v; }
  bool operator!=(const mint &r) const { return v != r.v; }
  explicit operator bool() const { return v; }
  explicit operator int() const { return v; }
  mint comb(mint k) {
    if (k > *this)
      return mint();
    if (!fact[0])
      combinit();
    if (v >= fn_) {
      if (k > *this - k)
        k = *this - k;
      mint tmp(1);
      for (int i = v; i >= v - k.v + 1; i--)
        tmp *= mint(i);
      return tmp * comp[k.v];
    }
    return fact[v] * comp[k.v] * comp[v - k.v];
  } // nCk
  mint perm(mint k) {
    if (k > *this)
      return mint();
    if (!fact[0])
      combinit();
    if (v >= fn_) {
      mint tmp(1);
      for (int i = v; i >= v - k.v + 1; i--)
        tmp *= mint(i);
      return tmp;
    }
    return fact[v] * comp[v - k.v];
  }
  static void combinit() {
    fact[0] = 1;
    for (int i = 1; i < fn_; i++)
      fact[i] = fact[i - 1] * mint(i);
    comp[fn_ - 1] = fact[fn_ - 1].inv();
    for (int i = fn_ - 2; i >= 0; i--)
      comp[i] = comp[i + 1] * mint(i + 1);
  }
};
mint mint::fact[fn_], mint::comp[fn_];
//--------------------------------------------------------------

//---------------------------------------------------------------------

int d;
vi c;
int a[400][30];
int ans[400];

set<int> se[26];

int res;

struct Ans {
  int score;
  vi select;
  vi e;
  bool operator<(Ans a) const { return score > a.score; }
};

int score(int d, vi ans) {
  int ret = 0;
  vi e(26, 0);

  rep(i, 26) se[i].insert(0);

  rng(i, 1, d + 1) {

    ret += a[i][ans[i - 1]];
    e[ans[i - 1]] = i;
    se[ans[i - 1]].insert(i);

    rep(j, 26) { ret -= c[j] * (i - e[j]); }
  }

  rep(i, 26) se[i].insert(d + 1);

  return ret;
}

int score2(int d, Ans &ans) {
  int ret = 0;

  int i = d;
  ret += a[i][ans.select[i - 1]];
  ans.e[ans.select[i - 1]] = i;

  rep(j, 26) { ret -= c[j] * (i - ans.e[j]); }

  return ret;
}

int testing() {
  vec<Ans> anses = {Ans{0, vi(0), vi(26, 0)}};

  rng(i, 1, d + 1) {
    vec<Ans> test;

    for (auto g : anses) {

      rep(j, 26) {
        g.select.pb(j);
        test.pb(Ans{score2(i, g), g.select, g.e});
        g.select.pop_back();
      }
    }

    sort(all(test));
    while (siz(test) > 5) {
      test.pop_back();
    }
    swap(anses, test);
  }

  int ret = score(d, anses[0].select);
  rep(i, d) ans[i] = anses[0].select[i];

  return ret;
}

void update(int x, int q) {
  // その時点での値を帰るときは、何も変わらないのであります。

  int k = ans[x];

  res -= a[x][k];
  res += a[x][q];

  // 後は引き算を変えるだけ。
  // 変わるのは、kとq

  int pre = x - *prev(se[k].lower_bound(x));
  // 前とここの差分はこれ
  res -= pre * (*se[k].upper_bound(x) - x) * c[k];
  se[k].erase(x);

  pre = x - *prev(se[q].lower_bound(x));
  res += pre * (*se[q].upper_bound(x) - x) * c[q];
  se[q].insert(x);

  ans[x] = q;
}

mt19937 rnd;

signed main() {
  cin >> d;
  // readv(c, 26);
  // rng(i, 1, d + 1)rep(j, 26) cin >> a[i][j];

  rep(i, 26) c.pb(rnd() % 200);
  rng(i, 1, d + 1) rep(j, 26) a[i][j] = rnd() % 500;

  testing();
  rep(i, d) cout << ans[i] << endl;
}

Error: TLE

Fixed submission:

#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
#define int long long
#define all(a) (a).begin(), (a).end()
#define fs first
#define sc second
#define xx first
#define yy second.first
#define zz second.second
#define H pair<int, int>
#define P pair<int, pair<int, int>>
#define Q(i, j, k) mkp(i, mkp(j, k))
#define rng(i, s, n) for (int i = (s); i < (n); i++)
#define rep(i, n) rng(i, 0, (n))
#define mkp make_pair
#define vec vector
#define vi vec<int>
#define pb emplace_back
#define siz(a) (int)(a).size()
#define crdcomp(b)                                                             \\
  sort(all((b)));                                                              \\
  (b).erase(unique(all((b))), (b).end())
#define getidx(b, i) lower_bound(all(b), (i)) - (b).begin()
#define ssp(i, n) (i == (int)(n)-1 ? "\\n" : " ")
#define ctoi(c) (int)(c - '0')
#define itoc(c) (char)(c + '0')
#define cyes printf("Yes\\n")
#define cno printf("No\\n")
#define cdf(n)                                                                 \\
  int quetimes_ = (n);                                                         \\
  rep(qq123_, quetimes_)
#define gcj printf("Case #%lld: ", qq123_ + 1)
#define readv(a, n)                                                            \\
  a.resize(n, 0);                                                              \\
  rep(i, (n)) a[i] = read()
#define found(a, x) (a.find(x) != a.end())
// #define endl "\\n"
constexpr int mod = 1e9 + 7;
constexpr int Mod = 998244353;
constexpr ld EPS = 1e-10;
constexpr ll inf = 3 * 1e18;
constexpr int Inf = 15 * 1e8;
constexpr int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
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;
}
ll read() {
  ll u, k = scanf("%lld", &u);
  return u;
}
string reads() {
  string s;
  cin >> s;
  return s;
}
H readh(bool g = 0) {
  H u;
  int k = scanf("%lld %lld", &u.fs, &u.sc);
  if (g)
    u.fs--, u.sc--;
  return u;
}
bool ina(H t, int h, int w) {
  return 0 <= t.fs && t.fs < h && 0 <= t.sc && t.sc < w;
}
bool ina(int t, int l, int r) { return l <= t && t < r; }
ll gcd(ll i, ll j) { return j ? gcd(j, i % j) : i; }
ll popcount(ll x) {
  int sum = 0;
  for (int i = 0; i < 60; i++)
    if ((1ll << i) & x)
      sum++;
  return sum;
}
class mint {
public:
  ll v;
  mint(ll v = 0) { s(v % mod + mod); }
  constexpr static int mod = 1e9 + 7;
  constexpr static int fn_ = 6e5 + 5;
  static mint fact[fn_], comp[fn_];
  mint pow(int x) const {
    mint b(v), c(1);
    while (x) {
      if (x & 1)
        c *= b;
      b *= b;
      x >>= 1;
    }
    return c;
  }
  inline mint &s(int vv) {
    v = vv < mod ? vv : vv - mod;
    return *this;
  }
  inline mint inv() const { return pow(mod - 2); }
  inline mint operator-() const { return mint() - *this; }
  inline mint &operator+=(const mint b) { return s(v + b.v); }
  inline mint &operator-=(const mint b) { return s(v + mod - b.v); }
  inline mint &operator*=(const mint b) {
    v = v * b.v % mod;
    return *this;
  }
  inline mint &operator/=(const mint b) {
    v = v * b.inv().v % mod;
    return *this;
  }
  inline mint operator+(const mint b) const { return mint(v) += b; }
  inline mint operator-(const mint b) const { return mint(v) -= b; }
  inline mint operator*(const mint b) const { return mint(v) *= b; }
  inline mint operator/(const mint b) const { return mint(v) /= b; }
  friend ostream &operator<<(ostream &os, const mint &m) { return os << m.v; }
  friend istream &operator>>(istream &is, mint &m) {
    int x;
    is >> x;
    m = mint(x);
    return is;
  }
  bool operator<(const mint &r) const { return v < r.v; }
  bool operator>(const mint &r) const { return v > r.v; }
  bool operator<=(const mint &r) const { return v <= r.v; }
  bool operator>=(const mint &r) const { return v >= r.v; }
  bool operator==(const mint &r) const { return v == r.v; }
  bool operator!=(const mint &r) const { return v != r.v; }
  explicit operator bool() const { return v; }
  explicit operator int() const { return v; }
  mint comb(mint k) {
    if (k > *this)
      return mint();
    if (!fact[0])
      combinit();
    if (v >= fn_) {
      if (k > *this - k)
        k = *this - k;
      mint tmp(1);
      for (int i = v; i >= v - k.v + 1; i--)
        tmp *= mint(i);
      return tmp * comp[k.v];
    }
    return fact[v] * comp[k.v] * comp[v - k.v];
  } // nCk
  mint perm(mint k) {
    if (k > *this)
      return mint();
    if (!fact[0])
      combinit();
    if (v >= fn_) {
      mint tmp(1);
      for (int i = v; i >= v - k.v + 1; i--)
        tmp *= mint(i);
      return tmp;
    }
    return fact[v] * comp[v - k.v];
  }
  static void combinit() {
    fact[0] = 1;
    for (int i = 1; i < fn_; i++)
      fact[i] = fact[i - 1] * mint(i);
    comp[fn_ - 1] = fact[fn_ - 1].inv();
    for (int i = fn_ - 2; i >= 0; i--)
      comp[i] = comp[i + 1] * mint(i + 1);
  }
};
mint mint::fact[fn_], mint::comp[fn_];
//--------------------------------------------------------------

//---------------------------------------------------------------------

int d;
vi c;
int a[400][30];
int ans[400];

set<int> se[26];

int res;

struct Ans {
  int score;
  vec<int> select;
  bool operator<(Ans a) const { return score > a.score; }
};

int score(int d, vi ans) {
  int ret = 0;
  vi e(26, 0);

  rep(i, 26) se[i].insert(0);

  rng(i, 1, d + 1) {

    ret += a[i][ans[i - 1]];
    e[ans[i - 1]] = i;
    se[ans[i - 1]].insert(i);

    rep(j, 26) { ret -= c[j] * (i - e[j]); }
  }

  rep(i, 26) se[i].insert(d + 1);

  return ret;
}

int testing() {
  vec<Ans> anses = {Ans{0, vi(0)}};

  rng(i, 1, d + 1) {
    vec<Ans> test;

    for (auto g : anses) {

      rep(j, 26) {
        g.select.pb(j);
        test.pb(Ans{score(i, g.select), g.select});
        g.select.pop_back();
      }
    }

    sort(all(test));
    while (siz(test) > 150) {
      test.pop_back();
    }
    swap(anses, test);
  }

  int ret = score(d, anses[0].select);
  rep(i, d) ans[i] = anses[0].select[i];

  return ret;
}

void update(int x, int q) {
  // その時点での値を帰るときは、何も変わらないのであります。

  int k = ans[x];

  res -= a[x][k];
  res += a[x][q];

  // 後は引き算を変えるだけ。
  // 変わるのは、kとq

  int pre = x - *prev(se[k].lower_bound(x));
  // 前とここの差分はこれ
  res -= pre * (*se[k].upper_bound(x) - x) * c[k];
  se[k].erase(x);

  pre = x - *prev(se[q].lower_bound(x));
  res += pre * (*se[q].upper_bound(x) - x) * c[q];
  se[q].insert(x);

  ans[x] = q;
}

signed main() {
  cin >> d;
  readv(c, 26);
  rng(i, 1, d + 1) rep(j, 26) cin >> a[i][j];

  testing();
  rep(i, d) cout << ans[i] << endl;
}