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

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_set>
#include <vector>
using namespace std;
using namespace std::chrono;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
#define REP(i, n) for (int i = 0; i < (n); i++)
/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/
const int mod = 998244353;
const llint inf = 2.19e15 + 1;
const long double pai = 3.141592653589793238462643383279502884197;
const long double eps = 1e-10;
template <class T, class U> bool chmin(T &a, U b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> bool chmax(T &a, U b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
llint gcd(llint a, llint b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
llint lcm(llint a, llint b) {
  if (a == 0) {
    return b;
  }
  return a / gcd(a, b) * b;
}
template <class T> void SO(T &ve) { sort(ve.begin(), ve.end()); }
template <class T> void REV(T &ve) { reverse(ve.begin(), ve.end()); }
template <class U, class T> llint LBI(const U &ar, T in) {
  return lower_bound(ar.begin(), ar.end(), in) - ar.begin();
}
template <class U, class T> llint UBI(const U &ar, T in) {
  return upper_bound(ar.begin(), ar.end(), in) - ar.begin();
}
inline double getTime() {
  using namespace std::chrono;
  return duration_cast<milliseconds>(system_clock::now().time_since_epoch())
      .count();
}
double starttime;
inline double myTime() { return getTime() - starttime; } // ミリ秒単位

llint getscore(int D, vector<llint> C, vector<vector<llint>> S, vector<int> syu,
               vector<vector<int>> &R, vector<vector<int>> &L) {
  llint i, j, k, score = 0;
  vector<int> mae(26);
  for (i = 1; i <= D; i++) {
    for (j = 0; j < 26; j++) {
      L[j][i] = mae[j];
    }
    int a = syu[i];
    score += S[a][i];
    score -= (i - mae[a]) * (i - mae[a] - 1) / 2 * C[a];
    // cerr<<score<<endl;
    mae[a] = i;
  }
  for (j = 0; j < 26; j++) {
    L[j][D + 1] = mae[j];
    score -= (D + 1 - mae[j]) * (D + 1 - mae[j] - 1) / 2 * C[j];
    mae[j] = D + 1;
  }
  for (i = D; i >= 0; i--) {
    for (j = 0; j < 26; j++) {
      R[j][i] = mae[j];
    }
    mae[syu[i]] = i;
  }
  return score;
}
int main(void) {
  // 焼きなましすればよさそう
  // 近傍 1日のコンテスト変更
  //  2日のコンテストswap

  mt19937 engine(1333);

  std::uniform_real_distribution<> dist(0.0, 1.0);

  int D, i, j, k;
  cin >> D;
  vector<llint> C(26);
  for (i = 0; i < 26; i++) {
    cin >> C[i];
  }
  vector<vector<llint>> S(26, vector<llint>(D + 2));
  for (j = 1; j <= D; j++) {
    for (i = 0; i < 26; i++) {
      cin >> S[i][j];
    }
  }
  starttime = getTime();
  vector<int> syu(D + 2); // なんのコンテスト
  for (i = 1; i <= D; i++) {
    syu[i] = i % 26;
  }
  // L,Rの更新を行う
  vector<vector<int>> R(26, vector<int>(D + 2, D + 1)); // 次のコンテストはいつ
  vector<vector<int>> L(26, vector<int>(D + 2, 0)); // 次のコンテストはいつ
  double ondo = 3000;
  cerr << "de" << endl;
  llint score = getscore(D, C, S, syu, R, L);
  cerr << score << endl;
  int mita = 0;
  int aaa = 1, bbb = 0;
  for (llint kai = 0; kai < 1e12; kai++) {
    if ((kai & 2047) == 0) {
      double zikan = myTime();
      if (zikan > 1900) {
        break;
      }
      ondo = (1 - zikan / 1900.0) * 7000;
      if (zikan / 70 >= mita + 1) {
        mita = zikan / 70;
        cerr << "score=" << score << endl;
        cerr << "per=" << bbb * 1.0 / aaa << endl;
        aaa = 1;
        bbb = 0;
      }
    }

    aaa++;
    if (engine() % 8 == 0) {
      // 1点更新
      int x = 1 + engine() % D;
      int a = syu[x];
      int b = engine() % 26;
      if (a == b) {
        continue;
      }
      int sco = S[b][x] - S[a][x] + (x - L[b][x]) * (R[b][x] - x) * C[b] -
                (x - L[a][x]) * (R[a][x] - x) * C[a];
      if (sco > log(dist(engine)) * ondo) {
        // 更新される

        int z = x;

        for (z = x; z > 0 && syu[z] != b; z--) {
          R[b][z - 1] = x;
        }
        for (z = x; z <= D && syu[z] != b; z++) {
          L[b][z + 1] = x;
        }
        syu[x] = b;
        for (z = x; z > 0 && syu[z] != a; z--) {
          R[a][z - 1] = R[a][x];
        }
        for (z = x; z <= D && syu[z] != a; z++) {
          L[a][z + 1] = L[a][x];
        }

        score += sco;
        bbb++;
      }
    } else {
      // 2点更新
      int x = 1 + engine() % D;
      int y = x + engine() % 11 - 5;
      if (y <= 0 || y > D) {
        continue;
      }
      int a = syu[x];
      int b = syu[y];
      if (a == b) {
        continue;
      }
      if (x > y) {
        swap(x, y);
        swap(a, b);
      }
      if (R[a][x] < y || L[a][x] > y) {
        continue;
      }
      if (R[b][y] < x || L[b][y] > x) {
        continue;
      }
      // ax,by  -> ay,bx
      int sco = S[b][x] + S[a][y] - S[a][x] - S[b][y] +
                (x - L[b][y]) * (R[b][y] - x) * C[b] -
                (x - L[a][x]) * (R[a][x] - x) * C[a] -
                (y - L[b][y]) * (R[b][y] - y) * C[b] +
                (y - L[a][x]) * (R[a][x] - y) * C[a];
      if (sco > log(dist(engine)) * ondo) {
        syu[x] = b;
        syu[y] = a;
        int La = L[a][x], Lb = L[b][y], Ra = R[a][x], Rb = R[b][y];
        int z;
        for (z = La + 1; z < y; z++) {
          L[a][z] = La;
          R[a][z] = y;
        }
        for (z = y + 1; z < Ra; z++) {
          L[a][z] = y;
          R[a][z] = Ra;
        }
        R[a][La] = y;
        L[a][y] = La;
        R[a][y] = Ra;
        L[a][Ra] = y;
        for (z = Lb + 1; z < x; z++) {
          L[b][z] = Lb;
          R[b][z] = x;
        }
        for (z = x + 1; z < Rb; z++) {
          L[b][z] = x;
          R[b][z] = Rb;
        }
        R[b][Lb] = x;
        L[b][x] = Lb;
        R[b][x] = Rb;
        L[b][Rb] = x;
        syu[x] = b;
        syu[y] = a;
        score += sco;
        bbb++;
      }
    }
  }
  llint backsco = getscore(D, C, S, syu, R, L);
  cerr << "score" << score << "backsco" << backsco << endl;
  for (i = 1; i <= D; i++) {
    cout << syu[i] + 1 << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_set>
#include <vector>
using namespace std;
using namespace std::chrono;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
#define REP(i, n) for (int i = 0; i < (n); i++)
/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/
const int mod = 998244353;
const llint inf = 2.19e15 + 1;
const long double pai = 3.141592653589793238462643383279502884197;
const long double eps = 1e-10;
template <class T, class U> bool chmin(T &a, U b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> bool chmax(T &a, U b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
llint gcd(llint a, llint b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
llint lcm(llint a, llint b) {
  if (a == 0) {
    return b;
  }
  return a / gcd(a, b) * b;
}
template <class T> void SO(T &ve) { sort(ve.begin(), ve.end()); }
template <class T> void REV(T &ve) { reverse(ve.begin(), ve.end()); }
template <class U, class T> llint LBI(const U &ar, T in) {
  return lower_bound(ar.begin(), ar.end(), in) - ar.begin();
}
template <class U, class T> llint UBI(const U &ar, T in) {
  return upper_bound(ar.begin(), ar.end(), in) - ar.begin();
}
inline double getTime() {
  using namespace std::chrono;
  return duration_cast<milliseconds>(system_clock::now().time_since_epoch())
      .count();
}
double starttime;
inline double myTime() { return getTime() - starttime; } // ミリ秒単位

llint getscore(int D, vector<llint> C, vector<vector<llint>> S, vector<int> syu,
               vector<vector<int>> &R, vector<vector<int>> &L) {
  llint i, j, k, score = 0;
  vector<int> mae(26);
  for (i = 1; i <= D; i++) {
    for (j = 0; j < 26; j++) {
      L[j][i] = mae[j];
    }
    int a = syu[i];
    score += S[a][i];
    score -= (i - mae[a]) * (i - mae[a] - 1) / 2 * C[a];
    // cerr<<score<<endl;
    mae[a] = i;
  }
  for (j = 0; j < 26; j++) {
    L[j][D + 1] = mae[j];
    score -= (D + 1 - mae[j]) * (D + 1 - mae[j] - 1) / 2 * C[j];
    mae[j] = D + 1;
  }
  for (i = D; i >= 0; i--) {
    for (j = 0; j < 26; j++) {
      R[j][i] = mae[j];
    }
    mae[syu[i]] = i;
  }
  return score;
}
int main(void) {
  // 焼きなましすればよさそう
  // 近傍 1日のコンテスト変更
  //  2日のコンテストswap

  mt19937 engine(1333);

  std::uniform_real_distribution<> dist(0.0, 1.0);

  int D, i, j, k;
  cin >> D;
  vector<llint> C(26);
  for (i = 0; i < 26; i++) {
    cin >> C[i];
  }
  vector<vector<llint>> S(26, vector<llint>(D + 2));
  for (j = 1; j <= D; j++) {
    for (i = 0; i < 26; i++) {
      cin >> S[i][j];
    }
  }
  starttime = getTime();
  vector<int> syu(D + 2); // なんのコンテスト
  for (i = 1; i <= D; i++) {
    syu[i] = i % 26;
  }
  // L,Rの更新を行う
  vector<vector<int>> R(26, vector<int>(D + 2, D + 1)); // 次のコンテストはいつ
  vector<vector<int>> L(26, vector<int>(D + 2, 0)); // 次のコンテストはいつ
  double ondo = 3000;
  cerr << "de" << endl;
  llint score = getscore(D, C, S, syu, R, L);
  cerr << score << endl;
  int mita = 0;
  for (llint kai = 0; kai < 1e12; kai++) {
    if ((kai & 2047) == 0) {
      double zikan = myTime() / 2;
      if (zikan > 1900) {
        break;
      }
      ondo = (1 - zikan / 1900.0) * (1 - zikan / 1900.0) * 5000;
      if (zikan / 70 >= mita + 1) {
        mita = zikan / 70;
        cerr << "score=" << score << endl;
      }
    }

    if (engine() % 8 == 0) {
      // 1点更新
      int x = 1 + engine() % D;
      int a = syu[x];
      int b = engine() % 26;
      if (a == b) {
        continue;
      }
      int sco = S[b][x] - S[a][x] + (x - L[b][x]) * (R[b][x] - x) * C[b] -
                (x - L[a][x]) * (R[a][x] - x) * C[a];
      if (sco > log(dist(engine)) * ondo) {
        // 更新される

        int z = x;

        for (z = x; z > 0 && syu[z] != b; z--) {
          R[b][z - 1] = x;
        }
        for (z = x; z <= D && syu[z] != b; z++) {
          L[b][z + 1] = x;
        }
        syu[x] = b;
        for (z = x; z > 0 && syu[z] != a; z--) {
          R[a][z - 1] = R[a][x];
        }
        for (z = x; z <= D && syu[z] != a; z++) {
          L[a][z + 1] = L[a][x];
        }

        score += sco;
      }
    } else {
      // 2点更新
      int x = 1 + engine() % D;
      int y = x + engine() % 11 - 5;
      if (y <= 0 || y > D) {
        continue;
      }
      int a = syu[x];
      int b = syu[y];
      if (a == b) {
        continue;
      }
      if (x > y) {
        swap(x, y);
        swap(a, b);
      }
      if (R[a][x] < y || L[a][x] > y) {
        continue;
      }
      if (R[b][y] < x || L[b][y] > x) {
        continue;
      }
      // ax,by  -> ay,bx
      int sco = S[b][x] + S[a][y] - S[a][x] - S[b][y] +
                (x - L[b][y]) * (R[b][y] - x) * C[b] -
                (x - L[a][x]) * (R[a][x] - x) * C[a] -
                (y - L[b][y]) * (R[b][y] - y) * C[b] +
                (y - L[a][x]) * (R[a][x] - y) * C[a];
      if (sco > log(dist(engine)) * ondo) {
        syu[x] = b;
        syu[y] = a;
        int La = L[a][x], Lb = L[b][y], Ra = R[a][x], Rb = R[b][y];
        int z;
        for (z = La + 1; z < y; z++) {
          L[a][z] = La;
          R[a][z] = y;
        }
        for (z = y + 1; z < Ra; z++) {
          L[a][z] = y;
          R[a][z] = Ra;
        }
        R[a][La] = y;
        L[a][y] = La;
        R[a][y] = Ra;
        L[a][Ra] = y;
        for (z = Lb + 1; z < x; z++) {
          L[b][z] = Lb;
          R[b][z] = x;
        }
        for (z = x + 1; z < Rb; z++) {
          L[b][z] = x;
          R[b][z] = Rb;
        }
        R[b][Lb] = x;
        L[b][x] = Lb;
        R[b][x] = Rb;
        L[b][Rb] = x;
        syu[x] = b;
        syu[y] = a;
        score += sco;
      }
    }
  }
  llint backsco = getscore(D, C, S, syu, R, L);
  cerr << "score" << score << "backsco" << backsco << endl;
  for (i = 1; i <= D; i++) {
    cout << syu[i] + 1 << endl;
  }
  return 0;
}