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

// #include "pch.h"
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>

typedef long long ll;
typedef std::pair<int, int> Pii;
typedef std::pair<long long, long long> Pll;
typedef std::pair<double, double> Pdd;

#define rip(i, n, s) for (int i = (s); i < (int)(n); i++)
#define mapa make_pair
#define all(a) a.begin(), a.end()
#define MM << " " <<

template <typename T> using MaxHeap = std::priority_queue<T>;
template <typename T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> std::vector<T> vec(int len, T elem) {
  return std::vector<T>(len, elem);
}
template <typename T> void vdeb(std::vector<T> &da) {
  for (int i = 0; i < da.size(); i++) {
    if (i == da.size() - 1)
      std::cout << da[i];
    else
      std::cout << da[i] << ' ';
  }
  std::cout << std::endl;
}
template <typename T> void vdeb(std::vector<std::vector<T>> &da) {
  for (int i = 0; i < da.size(); i++)
    vdeb(da[i]);
  std::cout << std::endl;
}

using namespace std;

const int D = 365;
const int si = 26;
vector<int> c(si);
vector<vector<int>> s(D, vector<int>(si));

struct cand {
  int score;
  int hist[D];
  int cc;
  vector<int> last;
  cand(int _cc) {
    score = 1000000;
    cc = _cc;
    last = vector<int>(si);
  }
};

bool operator<(const cand &left, const cand &right) {
  return left.score < right.score;
}

random_device r;
MaxHeap<cand> ans_heap;
int cc = 0;
const int width = 10, branch = 30, reading = 1;
int times = 1;
cand example(0);

void input() {
  int n;
  cin >> n;
  rip(i, si, 0) {
    cin >> c[i];
    cc += c[i];
  }
  rip(i, D, 0) rip(j, si, 0) cin >> s[i][j];
}

cand gen_next(cand now, int day) {
  rip(i, reading, 0) {
    int a = r() % si;
    now.hist[day + i] = a;
    now.score += s[day + i][a];
    now.cc += cc - c[a] * (day + i - now.last[a]);
    now.last[a] = day + i;
    now.score -= now.cc;
  }
  return now;
}

void beem(int day) {
  MaxHeap<cand> cand_heap;
  rip(i, width, 0) {
    cand now = ans_heap.top();
    ans_heap.pop();
    rip(j, branch, 0) {
      cand next = gen_next(now, day);
      cand_heap.push(next);
    }
  }
  swap(ans_heap, cand_heap);
}

void output(cand ans) { rip(i, D, 0) printf("%d\\n", ans.hist[i] + 1); }

int main() {
  input();
  rip(i, width, 0) { ans_heap.push(example); }
  rip(i, D / reading, 0) {
    beem(i * reading);
    times++;
  }
  output(ans_heap.top());
  // cout << ans_heap.top().score << endl;
}

Error: TLE

Fixed submission:

// #include "pch.h"
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>

typedef long long ll;
typedef std::pair<int, int> Pii;
typedef std::pair<long long, long long> Pll;
typedef std::pair<double, double> Pdd;

#define rip(i, n, s) for (int i = (s); i < (int)(n); i++)
#define mapa make_pair
#define all(a) a.begin(), a.end()
#define MM << " " <<

template <typename T> using MaxHeap = std::priority_queue<T>;
template <typename T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> std::vector<T> vec(int len, T elem) {
  return std::vector<T>(len, elem);
}
template <typename T> void vdeb(std::vector<T> &da) {
  for (int i = 0; i < da.size(); i++) {
    if (i == da.size() - 1)
      std::cout << da[i];
    else
      std::cout << da[i] << ' ';
  }
  std::cout << std::endl;
}
template <typename T> void vdeb(std::vector<std::vector<T>> &da) {
  for (int i = 0; i < da.size(); i++)
    vdeb(da[i]);
  std::cout << std::endl;
}

using namespace std;

const int D = 365;
const int si = 26;
vector<int> c(si);
vector<vector<int>> s(D, vector<int>(si));

struct cand {
  int score;
  int hist[D];
  cand() { score = 0; }
};

bool operator<(const cand &left, const cand &right) {
  return left.score < right.score;
}

random_device r;
MaxHeap<cand> ans_heap;
const int width = 50, branch = 300, reading = 5;
int times = 1;
cand example = cand();

void input() {
  // ifstream
  // fin("/6f4c9d0cfda6a311fea2d9cf23e4f032/tester/example/sample2.in");
  int n;
  cin >> n;
  rip(i, si, 0) { cin >> c[i]; }
  rip(i, D, 0) rip(j, si, 0) cin >> s[i][j];
}

cand gen_next(cand now, int day) {
  rip(i, reading, 0) {
    int a = r() % si;
    now.hist[day + i] = a;
    now.score += s[day + i][a] + c[a];
  }
  return now;
}

void beem(int day) {
  MaxHeap<cand> cand_heap;
  rip(i, width, 0) {
    cand now = ans_heap.top();
    ans_heap.pop();
    rip(j, branch, 0) {
      cand next = gen_next(now, day);
      cand_heap.push(next);
    }
  }
  swap(ans_heap, cand_heap);
}

void output(cand ans) {
  rip(i, D, 0) { cout << ans.hist[i] + 1 << endl; }
}

int main() {
  input();
  rip(i, width, 0) { ans_heap.push(example); }
  rip(i, D / reading, 0) {
    beem(i * reading);
    times++;
  }
  output(ans_heap.top());
  // cout << ans_heap.top().score << endl;
}