Problem p02618 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cassert>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <time.h>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define repf(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define repr(i, a, b) for (int i = (int)a; i > (int)b; i--)
#define all(v) (v).begin(), (v).end()
#define mp(a, b) make_pair(a, b)
#define eb(x) emplace_back(x)
#define F first
#define S second
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int> vii;
typedef vector<vii> vvii;
const ll mod = 1e9 + 7;
const int infi = 1147483600;
const ll infl = 4e18 + 5;
const char ENDL = '\\n';
// cout << fixed << setprecision(17) << res << endl;
const ll MOD = 998244353;

bool debug = false;

random_device rnd;
mt19937_64 mt(rnd());

ll c[26], s[370][26], D, last[26];

void input() {
  cin >> D;
  rep(i, 26) cin >> c[i];
  rep(i, D) rep(j, 26) cin >> s[i][j];
}

void make_input() {
  rep(i, 365) c[i] = mt() % 101;
  rep(i, 365) rep(j, 26) s[i][j] = mt() % 20001;
}

ll calc_v(vll t) {
  vll v(D, 0);
  rep(i, 26) last[i] = -1;
  rep(i, D) {
    if (i > 0)
      v[i] = v[i - 1];
    v[i] += s[i][t[i]];
    last[t[i]] = i;
    rep(j, 26) v[i] -= c[j] * (i - last[j]);
  }
  return v[D - 1];
}

// note:ランダム

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  clock_t start = clock();
  D = 365;
  if (debug)
    make_input();
  else
    input();
  rep(i, 26) last[i] = -1;

  ll mx = -infl;
  vll ans(D, 0);

  // ans:答えをＤ日分入力
  // last[i]:i番目のコンテストを最後に行ったのはlast[i]日
  rep(i, D) ans[i] = (mt() % 26);
  ll v;
  v = calc_v(ans);
  if (debug)
    cout << "start value:" << v << ENDL;
  ll q = 50000;
  rep(i, q) {
    ll d, x1, x2;
    d = (mt() % D);
    x1 = ans[d];
    x2 = (mt() % 26);
    ans[d] = x2;
    ll v2 = calc_v(ans);
    if (v < v2)
      v = v2;
    else
      ans[d] = x1;
  }

  clock_t end = clock();
  if (debug) {
    cout << "end value :" << v << ENDL;
    cout << "check :"
         << ((*min_element(all(ans)) >= 0) && (*max_element(all(ans)) <= 25))
         << ENDL;
    ld time = static_cast<ld>(end - start) / CLOCKS_PER_SEC * 1000.0;

    cout << "time :" << fixed << setprecision(3) << time << "ms" << ENDL;
  } else
    rep(i, D) cout << ans[i] + 1 << ENDL;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cassert>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <time.h>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define repf(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define repr(i, a, b) for (int i = (int)a; i > (int)b; i--)
#define all(v) (v).begin(), (v).end()
#define mp(a, b) make_pair(a, b)
#define eb(x) emplace_back(x)
#define F first
#define S second
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int> vii;
typedef vector<vii> vvii;
const ll mod = 1e9 + 7;
const int infi = 1147483600;
const ll infl = 4e18 + 5;
const char ENDL = '\\n';
// cout << fixed << setprecision(17) << res << endl;
const ll MOD = 998244353;

bool debug = true;

random_device rnd;
mt19937_64 mt(rnd());

ll c[26], s[370][26], D, last[26];

void input() {
  cin >> D;
  rep(i, 26) cin >> c[i];
  rep(i, D) rep(j, 26) cin >> s[i][j];
}

void make_input() {
  rep(i, 365) c[i] = mt() % 101;
  rep(i, 365) rep(j, 26) s[i][j] = mt() % 20001;
}

ll calc_v(vll t) {
  vll v(D, 0);
  rep(i, 26) last[i] = -1;
  rep(i, D) {
    if (i > 0)
      v[i] = v[i - 1];
    v[i] += s[i][t[i]];
    last[t[i]] = i;
    rep(j, 26) v[i] -= c[j] * (i - last[j]);
  }
  return v[D - 1];
}

// note:ランダム

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  clock_t start = clock();
  D = 365;
  if (debug)
    make_input();
  else
    input();
  rep(i, 26) last[i] = -1;

  ll mx = -infl;
  vll ans(D, 0);

  // ans:答えをＤ日分入力
  // last[i]:i番目のコンテストを最後に行ったのはlast[i]日
  rep(i, D) ans[i] = (mt() % 26);
  ll v;
  v = calc_v(ans);
  if (debug)
    cout << "start value:" << v << ENDL;
  ll q = 50000;
  rep(i, q) {
    ll d, x1, x2;
    d = (mt() % D);
    x1 = ans[d];
    x2 = (mt() % 26);
    ans[d] = x2;
    ll v2 = calc_v(ans);
    if (v < v2)
      v = v2;
    else
      ans[d] = x1;
  }

  clock_t end = clock();
  if (debug) {
    cout << "end value :" << v << ENDL;
    cout << "check :"
         << ((*min_element(all(ans)) >= 0) && (*max_element(all(ans)) <= 25))
         << ENDL;
    ld time = static_cast<ld>(end - start) / CLOCKS_PER_SEC * 1000.0;

    cout << "time :" << fixed << setprecision(3) << time << "ms" << ENDL;
  }
  // else
  // rep(i, D) cout << ans[i] + 1 << ENDL;
}