Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

constexpr int CONTEST = 26;
constexpr int D = 365;

int C[CONTEST];
int S[D + 1][CONTEST];

/////////////////////////////////////////////////////////////////////////
// TIME MANAGEMENT
/////////////////////////////////////////////////////////////////////////
// const double TIME_LIMIT = 100;
// const double TIME_LIMIT = 1900;
const double TIME_LIMIT = 19000;
struct TimeManager {
  double lim;
  double elapsed;
  chrono::system_clock::time_point t_start;

  TimeManager() {
    t_start = chrono::system_clock::now();
    lim = TIME_LIMIT;
  }

  template <typename T> TimeManager(const T &t) {
    t_start = chrono::system_clock::now();
    lim = static_cast<double>(t);
  }

  void set_time() { t_start = chrono::system_clock::now(); }

  void update() {
    auto t_now = chrono::system_clock::now();
    elapsed =
        chrono::duration_cast<chrono::milliseconds>(t_now - t_start).count();
  }

  bool is_timeover() { return elapsed > lim; }

  double passed_ratio() {
    // update();
    return elapsed / lim;
  }

  template <typename T> bool is_passed(const T &r) {
    double tmp = static_cast<double>(r);
    return tmp > passed_ratio();
  }
};

TimeManager t_manager;
/////////////////////////////////////////////////////////////////////////
// TIME MANAGEMENT END
/////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////
// XORSHIFT
/////////////////////////////////////////////////////////////////////////
int xorShift() {
  static unsigned long long x = 88172645463325252ULL;
  x ^= x << 13;
  x ^= x >> 7;
  x ^= x << 17;
  return int(x & 0x7fffffff);
}
/////////////////////////////////////////////////////////////////////////
// XORSHIFT END
/////////////////////////////////////////////////////////////////////////

int cur_score = 0, best_score = 0;
int cur_schedule[D + 1], best_schedule[D + 1];
int last[CONTEST];
int score_col[CONTEST];
int score_col_old[CONTEST];
int idx2pos[CONTEST][D + 1];
int idx_num[CONTEST];
int pos2idx[CONTEST][D + 1];
bool changed_col[CONTEST];
bool used[CONTEST];
int siz_ca;
int changed_at[CONTEST];
int changed_to_col[CONTEST];
int changed_to_col_rev[CONTEST];

int calcScore() {
  int res = 0;
  for (int col = 0; col < CONTEST; col++) {
    score_col[col] = 0;
    int &score = score_col[col];
    for (int i = 1; i < idx_num[col]; i++) {
      score += S[idx2pos[col][i]][col];
      int d = idx2pos[col][i] - idx2pos[col][i - 1];
      score -= C[col] * d * (d - 1) / 2;
    }
    int d = (D + 1) - idx2pos[col][idx_num[col] - 1];
    score -= C[col] * d * (d - 1) / 2;
    res += score;
  }
  return res;
}

int calcScore(const int &base_score) {
  int res = base_score;
  for (int i = 0; i < CONTEST; i++)
    if (changed_col[i]) {
      int &col = i;
      res -= score_col[col];
      int &score = score_col[col];
      score = 0;
      for (int j = 1; j < idx_num[col]; j++) {
        score += S[idx2pos[col][j]][col];
        int d = idx2pos[col][j] - idx2pos[col][j - 1];
        score -= C[col] * d * (d - 1) / 2;
      }
      int d = (D + 1) - idx2pos[col][idx_num[col] - 1];
      score -= C[col] * d * (d - 1) / 2;
      res += score;
    }
  // cout << "calcScore = " << res << endl;
  return res;
}

bool change(const int &at, const int &col) {
  int rem = cur_schedule[at];
  assert(col != -1);
  if (rem == col)
    return false;

  if (rem >= 0) {
    int rem_idx = pos2idx[rem][at];
    pos2idx[rem][at] = -1;
    for (int i = rem_idx + 1; i < idx_num[rem]; i++) {
      idx2pos[rem][i - 1] = idx2pos[rem][i];
      pos2idx[rem][idx2pos[rem][i - 1]] = i - 1;
    }
    idx_num[rem]--;
  }

  if (idx2pos[col][idx_num[col] - 1] < at) {
    idx2pos[col][idx_num[col]] = at;
    pos2idx[col][idx2pos[col][idx_num[col]]] = idx_num[col];
  } else {
    bool done = false;
    for (int i = 0; i < idx_num[col]; i++) {
      if (idx2pos[col][i] > at) {
        for (int j = idx_num[col]; j > i; j--) {
          idx2pos[col][j] = idx2pos[col][j - 1];
          pos2idx[col][idx2pos[col][j]] = j;
        }
        idx2pos[col][i] = at;
        pos2idx[col][at] = i;
        done = true;
        break;
      }
    }
    if (!done) {
      cerr << at << " " << col << endl;
      cerr << idx_num[col] << " " << idx2pos[col][idx_num[col] - 1] << endl;
    }
    assert(done);
  }
  idx_num[col]++;
  cur_schedule[at] = col;
  return true;
}

void initializer() {
  for (int i = 1; i <= D; i++) {
    int val = -1;
    int idx = -1;
    for (int j = 0; j < CONTEST; j++) {
      if (val < S[i][j]) {
        val = S[i][j];
        idx = j;
      }
    }
    assert(idx != -1);
    change(i, idx);
  }
}

int update_ = 0;
bool updateBest() {
  if (best_score >= cur_score)
    return false;
  best_score = cur_score;
  for (int i = 1; i <= D; i++)
    best_schedule[i] = cur_schedule[i];
  update_++;
  return true;
}

bool updateBest(const int &siz, int at[], int col[]) {
  if (best_score >= cur_score)
    return false;
  best_score = cur_score;
  for (int i = 0; i < siz; i++)
    best_schedule[at[i]] = col[i];
  update_++;
  return false;
}

int tryChange(const int &siz, int at[], int col[]) {
  for (int i = 0; i < CONTEST; i++) {
    score_col_old[i] = score_col[i];
    changed_col[i] = false;
  }
  for (int i = 0; i < siz; i++) {
    changed_to_col_rev[i] = cur_schedule[at[i]];
    changed_col[cur_schedule[at[i]]] = true;
    changed_col[col[i]] = true;
    change(at[i], col[i]);
  }
  // return calcScore();
  return calcScore(cur_score);
}

bool rollBack(const int &siz, int at[], int col[], const int &old_score) {
  cur_score = old_score;
  for (int i = 0; i < CONTEST; i++)
    score_col[i] = score_col_old[i];
  for (int i = 0; i < siz; i++) {
    change(at[i], col[i]);
  }
  return true;
}

double start_temp;
double end_temp;
int changed_ = 0;
bool SA(const int &nxt_score, const int &old_score) {
  if (old_score <= nxt_score) {
    changed_++;
    return true;
  }

  double temp = start_temp + (end_temp - start_temp) * t_manager.passed_ratio();
  double prob = exp((double)(nxt_score - old_score) / temp);

  if (prob > (double)(xorShift() % 100) / 100) {
    changed_++;
    return true;
  }
  return false;
}

int step = 0;

int replace_ = 0;
bool replaceAccept(const int &at, const int &col) {
  return cur_schedule[at] != col;
}
bool replace() {
  step++;
  int at = xorShift() % D + 1;
  int col = xorShift() % CONTEST;
  for (int i = 0; i < 5 && !replaceAccept(at, col); i++) {
    at = xorShift() % D + 1;
    col = xorShift() % CONTEST;
  }
  if (!replaceAccept(at, col))
    return false;

  int old_score = cur_score;
  siz_ca = 1;
  changed_at[0] = at;
  changed_to_col[0] = col;
  int from = cur_schedule[at];
  int new_score = tryChange(siz_ca, changed_at, changed_to_col);
  if (SA(new_score, old_score)) {
    cur_score = new_score;
    updateBest(siz_ca, changed_at, changed_to_col);
    // cout << "replace : " << "at = " << changed_at[0] << " from =  " << from
    // << " to = " << changed_to_col[0] << endl;
    replace_++;
    return true;
  } else {
    rollBack(siz_ca, changed_at, changed_to_col_rev, old_score);
    return false;
  }
}

int shift_ = 0;
bool shift() {
  step++;
  int at = xorShift() % D + 1;
  int col = cur_schedule[at];
  int pre_at = idx2pos[col][pos2idx[col][at] - 1];
  for (int i = pre_at + 1; i < at; i++)
    if (cur_schedule[i] != col) {
      siz_ca = 0;
      changed_at[siz_ca] = at;
      changed_to_col[siz_ca] = cur_schedule[i];
      siz_ca++;
      changed_at[siz_ca] = i;
      changed_to_col[siz_ca] = col;
      siz_ca++;
      int old_score = cur_score;
      int new_score = tryChange(siz_ca, changed_at, changed_to_col);
      if (SA(new_score, old_score)) {
        cur_score = new_score;
        updateBest(siz_ca, changed_at, changed_to_col);
        shift_++;
        return true;
      } else {
        rollBack(siz_ca, changed_at, changed_to_col_rev, old_score);
      }
    }
  int cnt = 0;
  for (int i = pre_at + 1; i < at && cnt < 30; i++)
    if (cur_schedule[i] != col) {
      int col2 = cur_schedule[i];
      int pre_at2 = idx2pos[col2][pos2idx[col2][i] - 1];
      for (int j = pre_at2 + 1; j < pre_at && cnt < 30; j++)
        if (cur_schedule[i] != cur_schedule[j] && cur_schedule[j] != col) {
          siz_ca = 0;
          changed_at[siz_ca] = at;
          changed_to_col[siz_ca] = cur_schedule[j];
          siz_ca++;
          changed_at[siz_ca] = i;
          changed_to_col[siz_ca] = col;
          siz_ca++;
          changed_at[siz_ca] = j;
          changed_to_col[siz_ca] = cur_schedule[i];
          siz_ca++;
          int old_score = cur_score;
          int new_score = tryChange(siz_ca, changed_at, changed_to_col);
          if (SA(new_score, old_score)) {
            cur_score = new_score;
            updateBest(siz_ca, changed_at, changed_to_col);
            shift_++;
            return true;
          } else {
            rollBack(siz_ca, changed_at, changed_to_col_rev, old_score);
          }
          cnt++;
        }
    }
  return false;
}

void init() {
  int t;
  cin >> t;

  for (int i = 0; i <= D; i++)
    cur_schedule[i] = best_schedule[i] = -1;
  for (int i = 0; i < CONTEST; i++) {
    idx2pos[i][idx_num[i]++] = 0;
  }
  for (int i = 0; i < CONTEST; i++)
    cin >> C[i];
  for (int i = 1; i <= D; i++) {
    for (int j = 0; j < CONTEST; j++) {
      cin >> S[i][j];
    }
  }
  initializer();
  cur_score = calcScore();
  best_score = -(1e9);
  updateBest();
  start_temp = (double)abs(cur_score) / 5e6;
  end_temp = 0;
  t_manager.update();
}

void output() {
  for (int i = 1; i <= D; i++) {
    assert(best_schedule[i] >= 0 && best_schedule[i] <= 25);
    cout << best_schedule[i] + 1 << endl;
  }
}

void displeyInfo() {
  cerr << endl;
  cerr << "cur_score = " << cur_score << endl;
  cerr << "best_score = " << best_score << endl;
  cerr << "step = " << step << endl;
  cerr << "changed = " << changed_ << endl;
  cerr << "replace = " << replace_ << endl;
  cerr << "shift = " << shift_ << endl;
  cerr << "time passed = " << setprecision(3) << fixed
       << t_manager.passed_ratio() << endl;
  cerr << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  init();

  // displeyInfo();

  // int res = 0;
  // for (int i = 1; i <= D; i++) {
  //   for (int j = 0; j < CONTEST; j++) {
  //     if (j == best_schedule[i]) {
  //       res += S[i][j];
  //     }
  //     int k = 0;
  //     for (int l = 1; l <= i; l++) if (best_schedule[l] == j) k = l;
  //     res -= C[j] * (i - k);
  //   }
  //   // cout << res << endl;
  // }
  // int res2 = 0;
  // for (int i = 0; i < CONTEST; i++) res2 += score_col[i];
  // cerr << cur_score << endl;
  // cerr << best_score << endl;
  // cerr << res << endl;
  // cerr << res2 << endl;

  for (int i = 0; !t_manager.is_timeover(); i++) {
    if (i % 100 == 0) {
      t_manager.update();
    }

    bool upd = false;
    upd |= replace();
    if (!upd)
      upd |= shift();

    updateBest();

    // if (i % 30000 == 0) {
    //   displeyInfo();
    // }
  }

  output();
  // displeyInfo();

  // for (int i = 1; i <= D; i++) cout << " " << i << " " << cur_schedule[i] <<
  // endl; for (int i = 0; i < CONTEST; i++) {
  //   cout << "i = " << i << " idx_num[i] = " << idx_num[i] << endl;
  //   for (int j = 0; j < idx_num[i]; j++) {
  //     cout << idx2pos[i][j] << " ";
  //   }
  //   cout << endl;
  // }
  // cout << endl;
  // res = 0;
  // for (int i = 1; i <= D; i++) {
  //   for (int j = 0; j < CONTEST; j++) {
  //     if (j == best_schedule[i]) {
  //       res += S[i][j];
  //     }
  //     int k = 0;
  //     for (int l = 1; l <= i; l++) if (best_schedule[l] == j) k = l;
  //     res -= C[j] * (i - k);
  //   }
  //   // cout << res << endl;
  // }
  // res2 = 0;
  // for (int i = 0; i < CONTEST; i++) res2 += score_col[i];
  // cerr << cur_score << endl;
  // cerr << best_score << endl;
  // cerr << res << endl;
  // cerr << res2 << endl;
  return 0;
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

constexpr int CONTEST = 26;
constexpr int D = 365;

int C[CONTEST];
int S[D + 1][CONTEST];

/////////////////////////////////////////////////////////////////////////
// TIME MANAGEMENT
/////////////////////////////////////////////////////////////////////////
// const double TIME_LIMIT = 100;
const double TIME_LIMIT = 1900;
// const double TIME_LIMIT = 19000;
struct TimeManager {
  double lim;
  double elapsed;
  chrono::system_clock::time_point t_start;

  TimeManager() {
    t_start = chrono::system_clock::now();
    lim = TIME_LIMIT;
  }

  template <typename T> TimeManager(const T &t) {
    t_start = chrono::system_clock::now();
    lim = static_cast<double>(t);
  }

  void set_time() { t_start = chrono::system_clock::now(); }

  void update() {
    auto t_now = chrono::system_clock::now();
    elapsed =
        chrono::duration_cast<chrono::milliseconds>(t_now - t_start).count();
  }

  bool is_timeover() { return elapsed > lim; }

  double passed_ratio() {
    // update();
    return elapsed / lim;
  }

  template <typename T> bool is_passed(const T &r) {
    double tmp = static_cast<double>(r);
    return tmp > passed_ratio();
  }
};

TimeManager t_manager;
/////////////////////////////////////////////////////////////////////////
// TIME MANAGEMENT END
/////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////
// XORSHIFT
/////////////////////////////////////////////////////////////////////////
int xorShift() {
  static unsigned long long x = 88172645463325252ULL;
  x ^= x << 13;
  x ^= x >> 7;
  x ^= x << 17;
  return int(x & 0x7fffffff);
}
/////////////////////////////////////////////////////////////////////////
// XORSHIFT END
/////////////////////////////////////////////////////////////////////////

int cur_score = 0, best_score = 0;
int cur_schedule[D + 1], best_schedule[D + 1];
int last[CONTEST];
int score_col[CONTEST];
int score_col_old[CONTEST];
int idx2pos[CONTEST][D + 1];
int idx_num[CONTEST];
int pos2idx[CONTEST][D + 1];
bool changed_col[CONTEST];
bool used[CONTEST];
int siz_ca;
int changed_at[CONTEST];
int changed_to_col[CONTEST];
int changed_to_col_rev[CONTEST];

int calcScore() {
  int res = 0;
  for (int col = 0; col < CONTEST; col++) {
    score_col[col] = 0;
    int &score = score_col[col];
    for (int i = 1; i < idx_num[col]; i++) {
      score += S[idx2pos[col][i]][col];
      int d = idx2pos[col][i] - idx2pos[col][i - 1];
      score -= C[col] * d * (d - 1) / 2;
    }
    int d = (D + 1) - idx2pos[col][idx_num[col] - 1];
    score -= C[col] * d * (d - 1) / 2;
    res += score;
  }
  return res;
}

int calcScore(const int &base_score) {
  int res = base_score;
  for (int i = 0; i < CONTEST; i++)
    if (changed_col[i]) {
      int &col = i;
      res -= score_col[col];
      int &score = score_col[col];
      score = 0;
      for (int j = 1; j < idx_num[col]; j++) {
        score += S[idx2pos[col][j]][col];
        int d = idx2pos[col][j] - idx2pos[col][j - 1];
        score -= C[col] * d * (d - 1) / 2;
      }
      int d = (D + 1) - idx2pos[col][idx_num[col] - 1];
      score -= C[col] * d * (d - 1) / 2;
      res += score;
    }
  // cout << "calcScore = " << res << endl;
  return res;
}

bool change(const int &at, const int &col) {
  int rem = cur_schedule[at];
  assert(col != -1);
  if (rem == col)
    return false;

  if (rem >= 0) {
    int rem_idx = pos2idx[rem][at];
    pos2idx[rem][at] = -1;
    for (int i = rem_idx + 1; i < idx_num[rem]; i++) {
      idx2pos[rem][i - 1] = idx2pos[rem][i];
      pos2idx[rem][idx2pos[rem][i - 1]] = i - 1;
    }
    idx_num[rem]--;
  }

  if (idx2pos[col][idx_num[col] - 1] < at) {
    idx2pos[col][idx_num[col]] = at;
    pos2idx[col][idx2pos[col][idx_num[col]]] = idx_num[col];
  } else {
    bool done = false;
    for (int i = 0; i < idx_num[col]; i++) {
      if (idx2pos[col][i] > at) {
        for (int j = idx_num[col]; j > i; j--) {
          idx2pos[col][j] = idx2pos[col][j - 1];
          pos2idx[col][idx2pos[col][j]] = j;
        }
        idx2pos[col][i] = at;
        pos2idx[col][at] = i;
        done = true;
        break;
      }
    }
    if (!done) {
      cerr << at << " " << col << endl;
      cerr << idx_num[col] << " " << idx2pos[col][idx_num[col] - 1] << endl;
    }
    assert(done);
  }
  idx_num[col]++;
  cur_schedule[at] = col;
  return true;
}

void initializer() {
  for (int i = 1; i <= D; i++) {
    int val = -1;
    int idx = -1;
    for (int j = 0; j < CONTEST; j++) {
      if (val < S[i][j]) {
        val = S[i][j];
        idx = j;
      }
    }
    assert(idx != -1);
    change(i, idx);
  }
}

int update_ = 0;
bool updateBest() {
  if (best_score >= cur_score)
    return false;
  best_score = cur_score;
  for (int i = 1; i <= D; i++)
    best_schedule[i] = cur_schedule[i];
  update_++;
  return true;
}

bool updateBest(const int &siz, int at[], int col[]) {
  if (best_score >= cur_score)
    return false;
  best_score = cur_score;
  for (int i = 0; i < siz; i++)
    best_schedule[at[i]] = col[i];
  update_++;
  return false;
}

int tryChange(const int &siz, int at[], int col[]) {
  for (int i = 0; i < CONTEST; i++) {
    score_col_old[i] = score_col[i];
    changed_col[i] = false;
  }
  for (int i = 0; i < siz; i++) {
    changed_to_col_rev[i] = cur_schedule[at[i]];
    changed_col[cur_schedule[at[i]]] = true;
    changed_col[col[i]] = true;
    change(at[i], col[i]);
  }
  // return calcScore();
  return calcScore(cur_score);
}

bool rollBack(const int &siz, int at[], int col[], const int &old_score) {
  cur_score = old_score;
  for (int i = 0; i < CONTEST; i++)
    score_col[i] = score_col_old[i];
  for (int i = 0; i < siz; i++) {
    change(at[i], col[i]);
  }
  return true;
}

double start_temp;
double end_temp;
int changed_ = 0;
bool SA(const int &nxt_score, const int &old_score) {
  if (old_score <= nxt_score) {
    changed_++;
    return true;
  }

  double temp = start_temp + (end_temp - start_temp) * t_manager.passed_ratio();
  double prob = exp((double)(nxt_score - old_score) / temp);

  if (prob > (double)(xorShift() % 100) / 100) {
    changed_++;
    return true;
  }
  return false;
}

int step = 0;

int replace_ = 0;
bool replaceAccept(const int &at, const int &col) {
  return cur_schedule[at] != col;
}
bool replace() {
  step++;
  int at = xorShift() % D + 1;
  int col = xorShift() % CONTEST;
  for (int i = 0; i < 5 && !replaceAccept(at, col); i++) {
    at = xorShift() % D + 1;
    col = xorShift() % CONTEST;
  }
  if (!replaceAccept(at, col))
    return false;

  int old_score = cur_score;
  siz_ca = 1;
  changed_at[0] = at;
  changed_to_col[0] = col;
  int from = cur_schedule[at];
  int new_score = tryChange(siz_ca, changed_at, changed_to_col);
  if (SA(new_score, old_score)) {
    cur_score = new_score;
    updateBest(siz_ca, changed_at, changed_to_col);
    // cout << "replace : " << "at = " << changed_at[0] << " from =  " << from
    // << " to = " << changed_to_col[0] << endl;
    replace_++;
    return true;
  } else {
    rollBack(siz_ca, changed_at, changed_to_col_rev, old_score);
    return false;
  }
}

int shift_ = 0;
bool shift() {
  step++;
  int at = xorShift() % D + 1;
  int col = cur_schedule[at];
  int pre_at = idx2pos[col][pos2idx[col][at] - 1];
  for (int i = pre_at + 1; i < at; i++)
    if (cur_schedule[i] != col) {
      siz_ca = 0;
      changed_at[siz_ca] = at;
      changed_to_col[siz_ca] = cur_schedule[i];
      siz_ca++;
      changed_at[siz_ca] = i;
      changed_to_col[siz_ca] = col;
      siz_ca++;
      int old_score = cur_score;
      int new_score = tryChange(siz_ca, changed_at, changed_to_col);
      if (SA(new_score, old_score)) {
        cur_score = new_score;
        updateBest(siz_ca, changed_at, changed_to_col);
        shift_++;
        return true;
      } else {
        rollBack(siz_ca, changed_at, changed_to_col_rev, old_score);
      }
    }
  int cnt = 0;
  for (int i = pre_at + 1; i < at && cnt < 30; i++)
    if (cur_schedule[i] != col) {
      int col2 = cur_schedule[i];
      int pre_at2 = idx2pos[col2][pos2idx[col2][i] - 1];
      for (int j = pre_at2 + 1; j < pre_at && cnt < 30; j++)
        if (cur_schedule[i] != cur_schedule[j] && cur_schedule[j] != col) {
          siz_ca = 0;
          changed_at[siz_ca] = at;
          changed_to_col[siz_ca] = cur_schedule[j];
          siz_ca++;
          changed_at[siz_ca] = i;
          changed_to_col[siz_ca] = col;
          siz_ca++;
          changed_at[siz_ca] = j;
          changed_to_col[siz_ca] = cur_schedule[i];
          siz_ca++;
          int old_score = cur_score;
          int new_score = tryChange(siz_ca, changed_at, changed_to_col);
          if (SA(new_score, old_score)) {
            cur_score = new_score;
            updateBest(siz_ca, changed_at, changed_to_col);
            shift_++;
            return true;
          } else {
            rollBack(siz_ca, changed_at, changed_to_col_rev, old_score);
          }
          cnt++;
        }
    }
  return false;
}

void init() {
  int t;
  cin >> t;

  for (int i = 0; i <= D; i++)
    cur_schedule[i] = best_schedule[i] = -1;
  for (int i = 0; i < CONTEST; i++) {
    idx2pos[i][idx_num[i]++] = 0;
  }
  for (int i = 0; i < CONTEST; i++)
    cin >> C[i];
  for (int i = 1; i <= D; i++) {
    for (int j = 0; j < CONTEST; j++) {
      cin >> S[i][j];
    }
  }
  initializer();
  cur_score = calcScore();
  best_score = -(1e9);
  updateBest();
  start_temp = (double)abs(cur_score) / 5e6;
  end_temp = 0;
  t_manager.update();
}

void output() {
  for (int i = 1; i <= D; i++) {
    assert(best_schedule[i] >= 0 && best_schedule[i] <= 25);
    cout << best_schedule[i] + 1 << endl;
  }
}

void displeyInfo() {
  cerr << endl;
  cerr << "cur_score = " << cur_score << endl;
  cerr << "best_score = " << best_score << endl;
  cerr << "step = " << step << endl;
  cerr << "changed = " << changed_ << endl;
  cerr << "replace = " << replace_ << endl;
  cerr << "shift = " << shift_ << endl;
  cerr << "time passed = " << setprecision(3) << fixed
       << t_manager.passed_ratio() << endl;
  cerr << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  init();

  // displeyInfo();

  // int res = 0;
  // for (int i = 1; i <= D; i++) {
  //   for (int j = 0; j < CONTEST; j++) {
  //     if (j == best_schedule[i]) {
  //       res += S[i][j];
  //     }
  //     int k = 0;
  //     for (int l = 1; l <= i; l++) if (best_schedule[l] == j) k = l;
  //     res -= C[j] * (i - k);
  //   }
  //   // cout << res << endl;
  // }
  // int res2 = 0;
  // for (int i = 0; i < CONTEST; i++) res2 += score_col[i];
  // cerr << cur_score << endl;
  // cerr << best_score << endl;
  // cerr << res << endl;
  // cerr << res2 << endl;

  for (int i = 0; !t_manager.is_timeover(); i++) {
    if (i % 100 == 0) {
      t_manager.update();
    }

    bool upd = false;
    upd |= replace();
    if (!upd)
      upd |= shift();

    updateBest();

    // if (i % 30000 == 0) {
    //   displeyInfo();
    // }
  }

  output();
  // displeyInfo();

  // for (int i = 1; i <= D; i++) cout << " " << i << " " << cur_schedule[i] <<
  // endl; for (int i = 0; i < CONTEST; i++) {
  //   cout << "i = " << i << " idx_num[i] = " << idx_num[i] << endl;
  //   for (int j = 0; j < idx_num[i]; j++) {
  //     cout << idx2pos[i][j] << " ";
  //   }
  //   cout << endl;
  // }
  // cout << endl;
  // res = 0;
  // for (int i = 1; i <= D; i++) {
  //   for (int j = 0; j < CONTEST; j++) {
  //     if (j == best_schedule[i]) {
  //       res += S[i][j];
  //     }
  //     int k = 0;
  //     for (int l = 1; l <= i; l++) if (best_schedule[l] == j) k = l;
  //     res -= C[j] * (i - k);
  //   }
  //   // cout << res << endl;
  // }
  // res2 = 0;
  // for (int i = 0; i < CONTEST; i++) res2 += score_col[i];
  // cerr << cur_score << endl;
  // cerr << best_score << endl;
  // cerr << res << endl;
  // cerr << res2 << endl;
  return 0;
}