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

#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(v) ((int)v.size())
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <int MOD> struct Fp {
  ll val;
  constexpr Fp(ll v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp &operator/=(const Fp &r) noexcept {
    ll a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      ll t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, ll n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};
constexpr ll MOD = 1e9 + 7;
using mint = Fp<MOD>;

#define pb push_back
#define mp make_pair
#define mt make_tuple

int D;
array<ll, 26> Cs;
vector<vector<ll>> Ss;

// http://shindannin.hatenadiary.com/entry/20121224/1356364040
unsigned int randxor() {
  static unsigned int x = 123456789, y = 362436069, z = 521288629, w = 88675123;
  unsigned int t;
  t = (x ^ (x << 11));
  x = y;
  y = z;
  z = w;
  return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

struct Answer {
  Answer() {
    m_lastdays.resize(26, 0);
    m_contest_set.resize(26);
    REP(d, 26) { m_contest_set[d].insert(0); }
  }

  vector<int> m_contests;
  vector<int> m_lastdays;
  vector<set<int>> m_contest_set;
  ll m_tempscore = 0;
  ll m_day = 0;

  void add_contest(int contest) {
    m_tempscore += Ss[m_day][contest];
    m_lastdays[contest] = m_day;
    REP(c, 26) { m_tempscore -= Cs[c] * ((ll)(m_day - m_lastdays[c])); }
    m_contests.push_back(contest);
    m_contest_set[contest].insert(m_day + 1);
    m_day += 1;
    // cout << "m_tempscore = " << m_tempscore << endl;
  }

  void remove_last(ll m_prevtempscore) {
    int last_contest = m_contests.back();

    auto it1 = m_contest_set[last_contest].rbegin();
    it1++;

    m_contests.pop_back();
    m_lastdays[last_contest] = *it1;
    m_day--;

    it1--;
    m_contest_set[last_contest].erase(*it1);

    // TODO!
    m_tempscore = m_prevtempscore;
  }

  void change_contest(int day, int contest) {
    auto old_contest = m_contests[day];
    if (contest == old_contest)
      return;

    m_contests[day] = contest;
    m_tempscore -= Ss[day][old_contest];
    m_tempscore += Ss[day][old_contest];
  }

  ll calc_score_init() {
    // assert(SIZE(m_contests) == D);
    ll ans = 0;

    vector<int> lastdays(26);

    REP(d, SIZE(m_contests)) {
      auto contest = m_contests[d];
      ans += Ss[d][contest];
      lastdays[contest] = d + 1;
      REP(c, 26) { ans -= Cs[c] * ((ll)(d + 1) - lastdays[c]); }
      // cerr << "temp score: " << ans << endl;
    }
    return max(0LL, 1000000 + ans);
  }
  void print_contests() {
    REP(d, D) { printf("%d\\n", m_contests[d] + 1); }
  }
};

int main(void) {
  // ios::sync_with_stdio(false);
  cin.tie(0);

  auto start = std::chrono::system_clock::now();

  cin >> D;
  REP(i, 26) cin >> Cs[i];

  Ss.resize(D);
  REP(i, D) {
    Ss[i].resize(26);
    REP(j, 26) { cin >> Ss[i][j]; }
  }

  Answer greedy_ans;

  for (int d = 0; d < D; d += 2) {
    ll temp_best = -1e18;
    if (d + 1 >= D) {
      int next_c = -1;
      ll prevtempscore = greedy_ans.m_tempscore;
      REP(c, 26) {
        greedy_ans.add_contest(c);
        // cerr << "before add c(init)::" << greedy_ans.calc_score_init() <<
        // endl;
        if (greedy_ans.m_tempscore > temp_best) {
          next_c = c;
          temp_best = greedy_ans.m_tempscore;
        }
        greedy_ans.remove_last(prevtempscore);
        // cerr << "after remove c(init)::" << greedy_ans.calc_score_init() <<
        // endl;
      }
      greedy_ans.add_contest(next_c);
    } else {
      int next_c1 = -1;
      int next_c2 = -1;
      ll prevtempscore = greedy_ans.m_tempscore;
      REP(c1, 26) {
        REP(c2, 26) {
          // cerr << "before add c1(init)::" << greedy_ans.calc_score_init() <<
          // endl;
          greedy_ans.add_contest(c1);
          // cerr << "before add c2(init)::" << greedy_ans.calc_score_init() <<
          // endl;
          greedy_ans.add_contest(c2);
          // cerr << "after add c2(init)::" << greedy_ans.calc_score_init() <<
          // endl;
          if (greedy_ans.m_tempscore > temp_best) {
            next_c1 = c1;
            next_c2 = c2;
            temp_best = greedy_ans.m_tempscore;
          }
          greedy_ans.remove_last(prevtempscore);
          // cerr << "after remove c2(init)::" << greedy_ans.calc_score_init()
          // << endl;
          greedy_ans.remove_last(prevtempscore);
          // cerr << "after remove c1(init)::" << greedy_ans.calc_score_init()
          // << endl;
        }
      }
      greedy_ans.add_contest(next_c1);
      greedy_ans.add_contest(next_c2);
    }
  }

  cerr << "greedy_score(incremental)" << greedy_ans.m_tempscore << endl;
  auto best_score = greedy_ans.calc_score_init();
  cerr << "greedy_score(from init):" << best_score << endl;

  Answer best_ans = greedy_ans;

  // ll cnt = 0;
  // while (true) {
  //     auto end = std::chrono::system_clock::now();
  //     auto diff = end - start;
  //     auto milli =
  //     std::chrono::duration_cast<std::chrono::milliseconds>(diff).count(); if
  //     (milli > 1950) break;

  //    ++cnt;
  //    Answer ans;
  //    REP(d, D) {
  //        ll temp_best = ll(-1e18);
  //        ll next_c = -1;
  //        vector<pair<ll, int>> score_and_contests;
  //        REP(c, 26) {
  //            Answer ans2 = ans;
  //            ans2.add_contest(c);
  //            score_and_contests.push_back({ ans2.m_tempscore, c });
  //        }
  //        sort(ALL(score_and_contests));

  //        if (rand() % 100 < 90) {
  //            ans.add_contest(score_and_contests[25].second);
  //        }
  //        else {
  //            ans.add_contest(score_and_contests[24].second);
  //        }
  //    }
  //    //cerr << "random_score:" <<  << endl;
  //    auto random_score = ans.calc_score_init();
  //    if (random_score > best_score) {
  //        cerr << "updated! " << endl;
  //        best_score = random_score;
  //        best_ans = ans;
  //    }

  //}

  // cerr << "try num: " << cnt << endl;

  cerr << "best_score:" << best_score << endl;
  best_ans.print_contests();

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(v) ((int)v.size())
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <int MOD> struct Fp {
  ll val;
  constexpr Fp(ll v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp &operator/=(const Fp &r) noexcept {
    ll a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      ll t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, ll n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};
constexpr ll MOD = 1e9 + 7;
using mint = Fp<MOD>;

#define pb push_back
#define mp make_pair
#define mt make_tuple

int D;
array<ll, 26> Cs;
vector<vector<ll>> Ss;

// http://shindannin.hatenadiary.com/entry/20121224/1356364040
unsigned int randxor() {
  static unsigned int x = 123456789, y = 362436069, z = 521288629, w = 88675123;
  unsigned int t;
  t = (x ^ (x << 11));
  x = y;
  y = z;
  z = w;
  return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

struct Answer {
  Answer() {
    m_lastdays.resize(26, 0);
    m_contest_set.resize(26);
    REP(d, 26) { m_contest_set[d].insert(0); }
  }

  vector<int> m_contests;
  vector<int> m_lastdays;
  vector<set<int>> m_contest_set;
  ll m_tempscore = 0;
  ll m_day = 0;

  void add_contest(int contest) {
    m_tempscore += Ss[m_day][contest];
    m_lastdays[contest] = m_day;
    REP(c, 26) { m_tempscore -= Cs[c] * ((ll)(m_day - m_lastdays[c])); }
    m_contests.push_back(contest);
    m_contest_set[contest].insert(m_day + 1);
    m_day += 1;
    // cout << "m_tempscore = " << m_tempscore << endl;
  }

  void remove_last(ll m_prevtempscore) {
    int last_contest = m_contests.back();

    auto it1 = m_contest_set[last_contest].rbegin();
    it1++;

    m_contests.pop_back();
    m_lastdays[last_contest] = *it1;
    m_day--;

    it1--;
    m_contest_set[last_contest].erase(*it1);

    // TODO!
    m_tempscore = m_prevtempscore;
  }

  void change_contest(int day, int contest) {
    auto old_contest = m_contests[day];
    if (contest == old_contest)
      return;

    m_contests[day] = contest;
    m_tempscore -= Ss[day][old_contest];
    m_tempscore += Ss[day][old_contest];
  }

  ll calc_score_init() {
    assert(SIZE(m_contests) == D);
    ll ans = 0;

    vector<int> lastdays(26);

    REP(d, D) {
      auto contest = m_contests[d];
      ans += Ss[d][contest];
      lastdays[contest] = d + 1;
      REP(c, 26) { ans -= Cs[c] * ((ll)(d + 1) - lastdays[c]); }
      // cerr << "temp score: " << ans << endl;
    }
    return max(0LL, 1000000 + ans);
  }
  void print_contests() {
    REP(d, D) { cout << m_contests[d] + 1 << endl; }
  }
};

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  auto start = std::chrono::system_clock::now();

  cin >> D;
  REP(i, 26) cin >> Cs[i];

  Ss.resize(D);
  REP(i, D) {
    Ss[i].resize(26);
    REP(j, 26) { cin >> Ss[i][j]; }
  }

  Answer greedy_ans;

  for (int d = 0; d < D; d += 2) {
    ll temp_best = -1e18;
    if (d + 1 >= D) {
      int next_c = -1;
      ll prevtempscore = greedy_ans.m_tempscore;
      REP(c, 26) {
        greedy_ans.add_contest(c);
        if (greedy_ans.m_tempscore > temp_best) {
          next_c = c;
          temp_best = greedy_ans.m_tempscore;
        }
        greedy_ans.remove_last(prevtempscore);
      }
      greedy_ans.add_contest(next_c);
    } else {
      int next_c1 = -1;
      int next_c2 = -1;
      ll prevtempscore = greedy_ans.m_tempscore;
      REP(c1, 26) {
        REP(c2, 26) {
          greedy_ans.add_contest(c1);
          greedy_ans.add_contest(c2);
          if (greedy_ans.m_tempscore > temp_best) {
            next_c1 = c1;
            next_c2 = c2;
            temp_best = greedy_ans.m_tempscore;
          }
          greedy_ans.remove_last(prevtempscore);
          greedy_ans.remove_last(prevtempscore);
        }
      }
      greedy_ans.add_contest(next_c1);
      greedy_ans.add_contest(next_c2);
    }
  }

  cerr << "greedy_score(incremental)" << greedy_ans.m_tempscore << endl;
  auto best_score = greedy_ans.calc_score_init();
  cerr << "greedy_score(from init):" << best_score << endl;

  Answer best_ans = greedy_ans;

  ll cnt = 0;
  while (true) {
    auto end = std::chrono::system_clock::now();
    auto diff = end - start;
    auto milli =
        std::chrono::duration_cast<std::chrono::milliseconds>(diff).count();
    if (milli > 1950)
      break;

    ++cnt;
    Answer ans;
    REP(d, D) {
      ll temp_best = ll(-1e18);
      ll next_c = -1;
      vector<pair<ll, int>> score_and_contests;
      REP(c, 26) {
        Answer ans2 = ans;
        ans2.add_contest(c);
        score_and_contests.push_back({ans2.m_tempscore, c});
      }
      sort(ALL(score_and_contests));

      if (rand() % 100 < 90) {
        ans.add_contest(score_and_contests[25].second);
      } else {
        ans.add_contest(score_and_contests[24].second);
      }
    }
    // cerr << "random_score:" <<  << endl;
    auto random_score = ans.calc_score_init();
    if (random_score > best_score) {
      cerr << "updated! " << endl;
      best_score = random_score;
      best_ans = ans;
    }
  }

  cerr << "try num: " << cnt << endl;

  cerr << "best_score:" << best_score << endl;
  best_ans.print_contests();

  return 0;
}