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;
  }
}

int change_ans(int i, int n, bool f) {
  int ret = 0;
  if (f) {
    int fr = ans[i];
    ret += s[i][n];
    ret -= s[i][fr];

    // cout<<ret<<endl;
    int fl = 0;
    for (int j = 1; j <= i - 1; j++) {
      if (ans[j] == fr)
        fl = 0;
      else
        fl++;
    }
    for (int j = i; j <= D; j++) {
      if (ans[j] == fr && j != i)
        break;
      ret -= c[fr] * (fl + 1);
    }
    // cout<<ret<<endl;
    int tl = 0;
    for (int j = 1; j <= i - 1; j++) {
      if (ans[j] == n)
        tl = 0;
      else
        tl++;
    }
    for (int j = i; j <= D; j++) {
      if (ans[j] == n)
        break;
      ret += c[n] * (tl + 1);
    }
    // cout<<ret<<endl;
  }
  ans[i] = n;
  // cout<<ret<<endl;
  return ret;
}

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

void random_solve() {
  int seed = 1234;
  int cnt = 1000000;
  srand(seed);
  ans_ran();
  int bc = D / 2, bt = 13;
  for (int i = 0; i <= cnt; i++) {
    int ci = bc + rand() % 3 - 1;
    if (ci <= 0)
      ci += D;
    ci %= D;
    bc = ci;
    int tn = bt + rand() % 3 - 1;
    if (tn <= 0)
      tn += 26;
    tn %= 26;
    bt = tn;
    int fn = ans[ci];
    if (tn == fn) {
      tn = (tn + 1) % 26;
    }
    // cout<<ci<<","<<bt<<endl;
    int cng = change_ans(ci, tn, true);
    if (cng < 0) {
      change_ans(ci, fn, false);
    }
  }
  ans_disp();
  // cout<<man_calc(false)<<endl;
}

signed main() {
  inp();
  // ans_inp();
  // solve_C();
  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;
  }
}

int change_ans(int i, int n, bool f) {
  int ret = 0;
  if (f) {
    int fr = ans[i];
    ret += s[i][n];
    ret -= s[i][fr];

    // cout<<ret<<endl;
    int fl = 0;
    for (int j = 1; j <= i - 1; j++) {
      if (ans[j] == fr)
        fl = 0;
      else
        fl++;
    }
    for (int j = i; j <= D; j++) {
      if (ans[j] == fr && j != i)
        break;
      ret -= c[fr] * (fl + 1);
    }
    // cout<<ret<<endl;
    int tl = 0;
    for (int j = 1; j <= i - 1; j++) {
      if (ans[j] == n)
        tl = 0;
      else
        tl++;
    }
    for (int j = i; j <= D; j++) {
      if (ans[j] == n)
        break;
      ret += c[n] * (tl + 1);
    }
    // cout<<ret<<endl;
  }
  ans[i] = n;
  // cout<<ret<<endl;
  return ret;
}

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

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

signed main() {
  inp();
  // ans_inp();
  // solve_C();
  random_solve();
  return 0;
}