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

// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

/*
#include <boost/multiprecision/cpp_dec_float.hpp>
#include<boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
using Real = mp::number<mp::cpp_dec_float<1024>>;
using Bint = mp::cpp_int;
*/

using namespace std;

#define int long long
#define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++)
#define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--)
#define FOR(i, n) for ((i) = (0); (i) < (n); (i)++)
#define RFOR(i, n) for ((i) = ((n)-1); (i) >= (0); (i)--)
#define MOD 1000000007

int D;
int c[32];
int s[512][32];
int ans[512];

void inp() {
  cin >> D;
  for (int i = 1; i <= 26; i++) {
    cin >> c[i];
  }
  for (int i = 1; i <= D; i++) {
    for (int j = 1; j <= 26; j++) {
      cin >> s[i][j];
    }
  }
}

void ans_inp() {
  for (int i = 1; i <= D; i++) {
    cin >> ans[i];
  }
}

void ans_ran() {
  for (int i = 1; i <= D; i++) {
    ans[i] = rand() % 26 + 1;
  }
}

int man_calc(bool disp) {
  int l[32] = {0};
  int man = 0;
  for (int i = 1; i <= D; i++) {
    int n = ans[i];
    l[n] = i;
    man += s[i][n];
    for (int j = 1; j <= 26; j++) {
      man -= c[j] * (i - l[j]);
    }
    if (disp)
      cout << man << endl;
  }
  return man;
}

void ans_disp() {
  for (int i = 1; i <= D; i++) {
    cout << ans[i] << endl;
  }
}

void change_ans(int i, int n) { ans[i] = n; }

void solve_C() {
  int M;
  cin >> M;
  for (int i = 0; i < M; i++) {
    int from, to;
    cin >> from >> to;
    change_ans(from, to);
    cout << man_calc(false) << endl;
  }
}

void random_solve() {
  srand(1234);
  ans_ran();
  for (int i = 0; i <= 100000; i++) {
    int bef = man_calc(false);
    int ci = rand() % D + 1;
    int tn = rand() % 26 + 1;
    int fn = ans[ci];
    change_ans(ci, tn);
    int aft = man_calc(false);
    if (bef > aft) {
      change_ans(ci, fn);
    }
  }
  ans_disp();
  // cout<<man_calc(false)<<endl;
}

signed main() {
  inp();
  random_solve();
  return 0;
}


Error: TLE

Fixed submission:

// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

/*
#include <boost/multiprecision/cpp_dec_float.hpp>
#include<boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
using Real = mp::number<mp::cpp_dec_float<1024>>;
using Bint = mp::cpp_int;
*/

using namespace std;

#define int long long
#define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++)
#define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--)
#define FOR(i, n) for ((i) = (0); (i) < (n); (i)++)
#define RFOR(i, n) for ((i) = ((n)-1); (i) >= (0); (i)--)
#define MOD 1000000007

int D;
int c[32];
int s[512][32];
int ans[512];

void inp() {
  cin >> D;
  for (int i = 1; i <= 26; i++) {
    cin >> c[i];
  }
  for (int i = 1; i <= D; i++) {
    for (int j = 1; j <= 26; j++) {
      cin >> s[i][j];
    }
  }
}

void ans_inp() {
  for (int i = 1; i <= D; i++) {
    cin >> ans[i];
  }
}

void ans_ran() {
  for (int i = 1; i <= D; i++) {
    ans[i] = rand() % 26 + 1;
  }
}

int man_calc(bool disp) {
  int l[32] = {0};
  int man = 0;
  for (int i = 1; i <= D; i++) {
    int n = ans[i];
    l[n] = i;
    man += s[i][n];
    for (int j = 1; j <= 26; j++) {
      man -= c[j] * (i - l[j]);
    }
    if (disp)
      cout << man << endl;
  }
  return man;
}

void ans_disp() {
  for (int i = 1; i <= D; i++) {
    cout << ans[i] << endl;
  }
}

void change_ans(int i, int n) { ans[i] = n; }

void solve_C() {
  int M;
  cin >> M;
  for (int i = 0; i < M; i++) {
    int from, to;
    cin >> from >> to;
    change_ans(from, to);
    cout << man_calc(false) << endl;
  }
}

void random_solve() {
  srand(1234);
  ans_ran();
  for (int i = 0; i <= 1000000; i++) {
    int bef = man_calc(false);
    int ci = rand() % D + 1;
    int tn = rand() % 26 + 1;
    int fn = ans[ci];
    change_ans(ci, tn);
    int aft = man_calc(false);
    if (bef > aft) {
      change_ans(ci, fn);
    }
  }
  ans_disp();
  // cout<<man_calc(false)<<endl;
}

signed main() {
  inp();
  random_solve();
  return 0;
}