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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <sys/time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define endl '\\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef long double ld;
typedef pair<int, string> P;
typedef tuple<int, string, string> T;
typedef complex<double> comp;
typedef vector<vector<ld>> matrix;
struct pairhash {
public:
  template <typename T, typename U>
  size_t operator()(const pair<T, U> &x) const {
    size_t seed = hash<T>()(x.first);
    return hash<U>()(x.second) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
  }
};
const int inf = 1e9 + 9;
const ll mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);

int D;
int c[26];
int s[365][26];
struct timeval t1, t2;

double get_elapsed_time(struct timeval *begin, struct timeval *end) {
  return (end->tv_sec - begin->tv_sec) * 1000 +
         (end->tv_usec - begin->tv_usec) / 1000.0;
}

const string contest[26] = {"00", "01", "02", "03", "04", "05", "06",
                            "07", "08", "09", "10", "11", "12", "13",
                            "14", "15", "16", "17", "18", "19", "20",
                            "21", "22", "23", "24", "25"};

int last[26];

int calc_score(const string &p) {
  const int d = (int)p.size() / 2;
  int score = 0;
  memset(last, 0, sizeof(last));
  for (int i = 0; i < d; i++) {
    int t = (p[2 * i] - '0') * 10 + (p[2 * i + 1] - '0');
    score += s[i][t];
    last[t] = i + 1;
    for (int j = 0; j < 26; j++) {
      score -= c[j] * (i + 1 - last[j]);
    }
  }
  return score;
}

int calc_score_diff(const string &h, int v, int d) {
  int ret = s[d - 1][v];
  for (int i = 0; i < 26; i++) {
    ret -= c[i] * (d - ((h[3 * i] - '0') * 100 + (h[3 * i + 1] - '0') * 10 +
                        (h[3 * i + 2] - '0')));
  }
  return ret;
}

void solve() {
  priority_queue<T> que[D + 1];
  int sum_c = 0;
  for (int i = 0; i < 26; i++) {
    sum_c += c[i];
  }

  string z = "";
  for (int i = 0; i < 26; i++) {
    z += "000";
  }
  string cz = "";
  for (int i = 0; i < D; i++) {
    cz += "00";
  }

  for (int i = 0; i < 26; i++) {
    for (int j = 0; j < 26; j++) {
      string t = cz;
      t[0] = contest[i][0];
      t[1] = contest[i][1];
      t[2] = contest[j][0];
      t[3] = contest[j][1];
      int s = calc_score(t);
      string r = z;
      r[3 * i + 2] = '1';
      r[3 * j + 2] = '2';
      que[0].push(make_tuple(s, t, r));
    }
  }

  for (int i = 2; i < D; i++) {
    que[(i + 1) % 2] = priority_queue<T>();
    for (int x = 0; x < 110; x++) {
      T t = que[i % 2].top();
      que[i % 2].pop();
      for (int j = 0; j < 26; j++) {
        string r = get<2>(t);
        int k = i + 1;
        r[3 * j] = (char)('0' + (k / 100));
        k -= 100 * (k / 100);
        r[3 * j + 1] = (char)('0' + (k / 10));
        k -= 10 * (k / 10);
        r[3 * j + 2] = (char)('0' + k);
        int diff = calc_score_diff(r, j, i + 1);
        int nscore = get<0>(t) + diff;
        string p = get<1>(t);
        p[2 * i] = contest[j][0];
        p[2 * i + 1] = contest[j][1];
        que[(i + 1) % 2].push(make_tuple(nscore, p, r));
      }
    }
  }

  T t = que[D % 2].top();
  string p = get<1>(t);
  for (int i = 0; i < D; i++) {
    int v = (p[2 * i] - '0') * 10 + (p[2 * i + 1] - '0') + 1;
    cout << v << endl;
  }
}

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

int main() {
  gettimeofday(&t1, NULL);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);

  input();
  solve();
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <sys/time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define endl '\\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef long double ld;
typedef pair<int, string> P;
typedef tuple<int, string, string> T;
typedef complex<double> comp;
typedef vector<vector<ld>> matrix;
struct pairhash {
public:
  template <typename T, typename U>
  size_t operator()(const pair<T, U> &x) const {
    size_t seed = hash<T>()(x.first);
    return hash<U>()(x.second) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
  }
};
const int inf = 1e9 + 9;
const ll mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);

int D;
int c[26];
int s[365][26];
struct timeval t1, t2;

double get_elapsed_time(struct timeval *begin, struct timeval *end) {
  return (end->tv_sec - begin->tv_sec) * 1000 +
         (end->tv_usec - begin->tv_usec) / 1000.0;
}

const string contest[26] = {"00", "01", "02", "03", "04", "05", "06",
                            "07", "08", "09", "10", "11", "12", "13",
                            "14", "15", "16", "17", "18", "19", "20",
                            "21", "22", "23", "24", "25"};

int last[26];

int calc_score(const string &p) {
  const int d = (int)p.size() / 2;
  int score = 0;
  memset(last, 0, sizeof(last));
  for (int i = 0; i < d; i++) {
    int t = (p[2 * i] - '0') * 10 + (p[2 * i + 1] - '0');
    score += s[i][t];
    last[t] = i + 1;
    for (int j = 0; j < 26; j++) {
      score -= c[j] * (i + 1 - last[j]);
    }
  }
  return score;
}

int calc_score_diff(const string &h, int v, int d) {
  int ret = s[d - 1][v];
  for (int i = 0; i < 26; i++) {
    ret -= c[i] * (d - ((h[3 * i] - '0') * 100 + (h[3 * i + 1] - '0') * 10 +
                        (h[3 * i + 2] - '0')));
  }
  return ret;
}

void solve() {
  priority_queue<T> que[D + 1];
  int sum_c = 0;
  for (int i = 0; i < 26; i++) {
    sum_c += c[i];
  }

  string z = "";
  for (int i = 0; i < 26; i++) {
    z += "000";
  }

  for (int i = 0; i < 26; i++) {
    for (int j = 0; j < 26; j++) {
      string t = contest[i] + contest[j];
      int s = calc_score(t);
      string r = z;
      r[3 * i + 2] = '1';
      r[3 * j + 2] = '2';
      que[2].push(make_tuple(s, t, r));
    }
  }

  int avg_score[D + 1];
  for (int i = 1; i <= D; i++) {
    avg_score[i] = 0;
  }

  while (true) {
    for (int i = 2; i < D; i++) {
      T t = que[i].top();
      que[i].pop();
      for (int j = 0; j < 26; j++) {
        string r = get<2>(t);
        int k = i + 1;
        r[3 * j] = (char)('0' + (k / 100));
        k -= 100 * (k / 100);
        r[3 * j + 1] = (char)('0' + (k / 10));
        k -= 10 * (k / 10);
        r[3 * j + 2] = (char)('0' + k);
        int diff = calc_score_diff(r, j, i + 1);
        string p = get<1>(t) + contest[j];
        int nscore = get<0>(t) + diff;
        int num = que[i + 1].size();
        if ((int)que[i + 1].size() >= 200 && nscore < avg_score[i + 1])
          continue;
        avg_score[i] = (avg_score[i] * num + nscore) / (num + 1);
        que[i + 1].push(make_tuple(nscore, p, r));
      }
    }

    gettimeofday(&t2, NULL);
    if (get_elapsed_time(&t1, &t2) > 1500) {
      break;
    }
  }

  T t = que[D].top();
  string p = get<1>(t);
  for (int i = 0; i < D; i++) {
    int v = (p[2 * i] - '0') * 10 + (p[2 * i + 1] - '0') + 1;
    cout << v << endl;
  }
}

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

int main() {
  gettimeofday(&t1, NULL);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);

  input();
  solve();
}