Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
// typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
// typedef tuple<int, int, int> T;
#define FOR(i, s, x) for (int i = s; i < (int)(x); i++)
#define REP(i, x) FOR(i, 0, x)
#define ALL(c) c.begin(), c.end()
#define DUMP(x) cerr << #x << " = " << (x) << endl

const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};

const ll mod = 1e9 + 7;

using std::chrono::system_clock;

int D;
int C[26];
std::vector<std::vector<int>> S;
const int INF = 1e9;

std::vector<int> T;

system_clock::time_point start;

void init() {
  std::cin >> D;
  REP(i, 26) std::cin >> C[i];
  S.resize(D);
  REP(i, D) {
    S[i].resize(26);
    REP(j, 26) std::cin >> S[i][j];
  }

  T.resize(D, -1);
}

std::vector<int> prev_days;

int calc_score(std::vector<int> &contests, bool verbose = false) {
  int score = 0;
  prev_days.assign(26, -1);
  REP(i, contests.size()) {
    prev_days[contests[i]] = i;
    if (verbose) {
      for (int d : prev_days)
        std::cerr << d << ' ';
      std::cerr << endl;
    }
    REP(j, 26) { score -= C[j] * (i - prev_days[j]); }
    score += S[i][contests[i]];
    if (verbose) {
      std::cerr << score << std::endl;
    }
  }
  return score;
}

void output() {
  for (int t : T)
    std::cout << t + 1 << '\n';
}

void greedy() {
  std::vector<int> contests;
  REP(d, D) {
    int max_score = -INF, max_con = -1;
    REP(i, 26) {
      contests.emplace_back(i);
      int new_score = calc_score(contests);
      if (new_score > max_score) {
        max_score = new_score;
        max_con = i;
      }
      contests.pop_back();
    }
    contests.emplace_back(max_con);
  }
  T = contests;
  std::cerr << calc_score(T) << endl;
}

bool update_greedy() {
  int score = calc_score(T);
  bool is_updated = false;
  int day = -1, cont = -1;
  REP(d, D) {
    REP(i, 26) {
      int tmp = T[d];
      T[d] = i;
      int new_score = calc_score(T);
      if (new_score > score) {
        new_score = score;
        is_updated = true;
        day = d, cont = i;
      }
      T[d] = tmp;
    }
  }
  if (is_updated)
    T[day] = cont;
  return is_updated;
}

void greedy2() {
  while (true) {
    double diff = std::chrono::duration_cast<std::chrono::milliseconds>(
                      system_clock::now() - start)
                      .count() /
                  1000.;
    if (diff > 1.99)
      break;
    if (not update_greedy()) {
      break;
    }
  }
  std::cerr << calc_score(T) << endl;
}

int main() {
  start = system_clock::now();

  // use scanf in CodeForces!
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  init();

  greedy();
  greedy2();

  output();

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
// typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
// typedef tuple<int, int, int> T;
#define FOR(i, s, x) for (int i = s; i < (int)(x); i++)
#define REP(i, x) FOR(i, 0, x)
#define ALL(c) c.begin(), c.end()
#define DUMP(x) cerr << #x << " = " << (x) << endl

const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};

const ll mod = 1e9 + 7;

using std::chrono::system_clock;

int D;
int C[26];
std::vector<std::vector<int>> S;
const int INF = 1e9;

std::vector<int> T;

system_clock::time_point start;

void init() {
  std::cin >> D;
  REP(i, 26) std::cin >> C[i];
  S.resize(D);
  REP(i, D) {
    S[i].resize(26);
    REP(j, 26) std::cin >> S[i][j];
  }

  T.resize(D, -1);
}

std::vector<int> prev_days;

int calc_score(std::vector<int> &contests, bool verbose = false) {
  int score = 0;
  prev_days.assign(26, -1);
  REP(i, contests.size()) {
    prev_days[contests[i]] = i;
    if (verbose) {
      for (int d : prev_days)
        std::cerr << d << ' ';
      std::cerr << endl;
    }
    REP(j, 26) { score -= C[j] * (i - prev_days[j]); }
    score += S[i][contests[i]];
    if (verbose) {
      std::cerr << score << std::endl;
    }
  }
  return score;
}

void output() {
  for (int t : T)
    std::cout << t + 1 << '\n';
}

void greedy() {
  std::vector<int> contests;
  REP(d, D) {
    int max_score = -INF, max_con = -1;
    REP(i, 26) {
      contests.emplace_back(i);
      int new_score = calc_score(contests);
      if (new_score > max_score) {
        max_score = new_score;
        max_con = i;
      }
      contests.pop_back();
    }
    contests.emplace_back(max_con);
  }
  T = contests;
  std::cerr << calc_score(T) << endl;
}

bool update_greedy() {
  int score = calc_score(T);
  bool is_updated = false;
  int day = -1, cont = -1;
  REP(d, D) {
    REP(i, 26) {
      int tmp = T[d];
      T[d] = i;
      int new_score = calc_score(T);
      if (new_score > score) {
        new_score = score;
        is_updated = true;
        day = d, cont = i;
      }
      T[d] = tmp;
    }
  }
  if (is_updated)
    T[day] = cont;
  return is_updated;
}

void greedy2() {
  while (true) {
    double diff = std::chrono::duration_cast<std::chrono::milliseconds>(
                      system_clock::now() - start)
                      .count() /
                  1000.;
    if (diff > 1.95)
      break;
    if (not update_greedy()) {
      break;
    }
  }
  std::cerr << calc_score(T) << endl;
}

int main() {
  start = system_clock::now();

  // use scanf in CodeForces!
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  init();

  greedy();
  greedy2();

  output();

  return 0;
}