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

#include <bits/stdc++.h>

const long Day_num = 365;
const long type_num = 26;

class Scoring {
public:
  Scoring(const long arg_days, const std::vector<long> &arg_costs,
          const std::vector<std::vector<long>> &arg_incentives);
  std::vector<long> score(const std::vector<long> &arg_types);

private:
  long days;
  std::vector<long> costs;
  std::vector<std::vector<long>> incentives;
};

std::vector<long> Scoring::score(const std::vector<long> &arg_types) {
  std::vector<long> result(days);
  long satisfy = 0;
  std::vector<long> last_days(type_num, 0);

  for (long day = 1; day <= days; ++day) {
    const auto exe_type = arg_types[day - 1];
    satisfy += incentives[day - 1][exe_type - 1];
    last_days[exe_type - 1] = day;
    for (long type = 1; type <= type_num; ++type) {
      satisfy -= costs[type - 1] * (day - last_days[type - 1]);
    }
    result[day - 1] = satisfy;
  }
  return result;
}

Scoring::Scoring(const long arg_days, const std::vector<long> &arg_costs,
                 const std::vector<std::vector<long>> &arg_incentives) {
  days = arg_days;
  costs = arg_costs;
  incentives = arg_incentives;
}

int main() {
  clock_t start_clock = clock();
  std::mt19937 engine(12345678);
  std::uniform_int_distribution<> type_random(1, type_num);
  std::uniform_int_distribution<> day_random(1, Day_num);
  long D;
  std::cin >> D;
  std::vector<long> costs(type_num);
  for (long type = 1; type <= type_num; ++type) {
    std::cin >> costs[type - 1];
  }
  std::vector<std::vector<long>> incentives(D, std::vector<long>(type_num));
  for (long day = 1; day <= D; ++day) {
    for (long type = 1; type <= type_num; ++type) {
      std::cin >> incentives[day - 1][type - 1];
    }
  }

  Scoring scoring(D, costs, incentives);

  std::vector<long> types(D);
  for (long day = 1; day <= D; ++day) {
    types[day - 1] = type_random(engine);
  }

  while (static_cast<double>(clock() - start_clock) / CLOCKS_PER_SEC * 1000.0 <
         2000.0 - 100.0) {
    const auto old_types = types;
    types[day_random(engine) - 1] = type_random(engine);
    if (scoring.score(old_types).back() > scoring.score(types).back()) {
      types = old_types;
    }
  }

  for (long day = 1; day <= D; ++day) {
    std::cout << types[day - 1] << std::endl;
  }
  // std::vector<long> types(D);
  // for (long day = 1; day <= D; ++day) {
  //     std::cin >> types[day-1];
  // }

  // long M;
  // std::cin >> M;

  // std::vector<std::array<long,2>> query(M);
  // for (long index = 0; index < M; ++index) {
  //     std::cin >> query[index][0];
  //     std::cin >> query[index][1];
  // }

  // Scoring scoring(D, costs, incentives);
  // for (long index = 0; index < M; ++index) {
  //     types[query[index][0]-1] = query[index][1];
  //     std::cout << scoring.score(types).back() << std::endl;
  // }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

const long Day_num = 365;
const long type_num = 26;

class Scoring {
public:
  Scoring(const long arg_days, const std::vector<long> &arg_costs,
          const std::vector<std::vector<long>> &arg_incentives);
  std::vector<long> score(const std::vector<long> &arg_types);

private:
  long days;
  std::vector<long> costs;
  std::vector<std::vector<long>> incentives;
};

std::vector<long> Scoring::score(const std::vector<long> &arg_types) {
  std::vector<long> result(days);
  long satisfy = 0;
  std::vector<long> last_days(type_num, 0);

  for (long day = 1; day <= days; ++day) {
    const auto exe_type = arg_types[day - 1];
    satisfy += incentives[day - 1][exe_type - 1];
    last_days[exe_type - 1] = day;
    for (long type = 1; type <= type_num; ++type) {
      satisfy -= costs[type - 1] * (day - last_days[type - 1]);
    }
    result[day - 1] = satisfy;
  }
  return result;
}

Scoring::Scoring(const long arg_days, const std::vector<long> &arg_costs,
                 const std::vector<std::vector<long>> &arg_incentives) {
  days = arg_days;
  costs = arg_costs;
  incentives = arg_incentives;
}

int main() {
  clock_t start_clock = clock();
  std::mt19937 engine(12345678);
  std::uniform_int_distribution<> type_random(1, type_num);
  std::uniform_int_distribution<> day_random(1, Day_num);
  long D;
  std::cin >> D;
  std::vector<long> costs(type_num);
  for (long type = 1; type <= type_num; ++type) {
    std::cin >> costs[type - 1];
  }
  std::vector<std::vector<long>> incentives(D, std::vector<long>(type_num));
  for (long day = 1; day <= D; ++day) {
    for (long type = 1; type <= type_num; ++type) {
      std::cin >> incentives[day - 1][type - 1];
    }
  }

  Scoring scoring(D, costs, incentives);

  std::vector<long> types(D);
  for (long day = 1; day <= D; ++day) {
    types[day - 1] = type_random(engine);
  }

  while (static_cast<double>(clock() - start_clock) / CLOCKS_PER_SEC <
         2.0 - 0.01) {
    const auto old_types = types;
    types[day_random(engine) - 1] = type_random(engine);
    if (scoring.score(old_types).back() > scoring.score(types).back()) {
      types = old_types;
    }
  }

  for (long day = 1; day <= D; ++day) {
    std::cout << types[day - 1] << std::endl;
  }
  // std::vector<long> types(D);
  // for (long day = 1; day <= D; ++day) {
  //     std::cin >> types[day-1];
  // }

  // long M;
  // std::cin >> M;

  // std::vector<std::array<long,2>> query(M);
  // for (long index = 0; index < M; ++index) {
  //     std::cin >> query[index][0];
  //     std::cin >> query[index][1];
  // }

  // Scoring scoring(D, costs, incentives);
  // for (long index = 0; index < M; ++index) {
  //     types[query[index][0]-1] = query[index][1];
  //     std::cout << scoring.score(types).back() << std::endl;
  // }
}