Problem p02618 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;

#define N 26
#define MAX_T 1.9
#define TRIAL 5

const int INF = 1 << 30;

int c[N];
int s[365][N];
vector<int> sorted[365];

std::chrono::time_point<std::chrono::steady_clock> start, endt;

random_device rd;
mt19937 generator(rd());
uniform_real_distribution<> dist_real(0.0, 1.0);
uniform_int_distribution<> dist_int(0, RAND_MAX);

struct ContestInfo {
  int D;
  long score;
  long best_score;
  vector<int> t;
  vector<int> best_t;
  set<int> held[N];
  ContestInfo(int dd) : D(dd), score(0) {
    t.resize(D, -1);
    for (int i = 0; i < N; i++) {
      held[i].insert(-1);
      held[i].insert(D);
      // score -= c[i] * D * (D + 1) / 2;
    }
    // cout << score << endl;
  }

  void add(int dd, int qq) {
    if (t[dd] != -1) {
      cout << "Add faild!" << endl;
      exit(1);
    }

    t[dd] = qq;
    score += s[dd][qq];
    held[qq].insert(dd);
    auto itr = held[qq].find(dd);
    auto ritr = next(itr);
    auto litr = prev(itr);
    auto range = [](int l, int r) {
      int k = r - l - 1;
      return -k * (k + 1) / 2;
    };
    score -= c[qq] * range(*litr, *ritr);
    score += c[qq] * (range(*litr, *itr) + range(*itr, *ritr));
  }

  void erase(int dd) {
    if (t[dd] == -1) {
      cout << "Erase faild!" << endl;
      exit(1);
    }
    int qq = t[dd];
    t[dd] = -1;
    score -= s[dd][qq];
    auto itr = held[qq].find(dd);
    auto ritr = next(itr);
    auto litr = prev(itr);
    auto range = [](int l, int r) {
      int k = r - l - 1;
      return -k * (k + 1) / 2;
    };
    score += c[qq] * range(*litr, *ritr);
    score -= c[qq] * (range(*litr, *itr) + range(*itr, *ritr));
    held[qq].erase(itr);
  }

  void change(int dd, int qq) {
    erase(dd);
    add(dd, qq);
  }

  void save() {
    best_t = t;
    best_score = score;
  }

  void output(bool show = false) {
    for (int i = 0; i < D; i++)
      cout << best_t[i] + 1 << endl;
    if (show)
      cout << best_score << endl;
  }
};

double dice() { return dist_real(generator); }

double prob(double rem) {
  return 1.0 / exp(rem);
  // return 1.0 / pow(5, rem);
}

void tic() { start = std::chrono::steady_clock::now(); }

double toc() {
  endt = std::chrono::steady_clock::now();
  std::chrono::duration<double> elapsed_seconds = endt - start;
  return elapsed_seconds.count();
}

vector<int> sort_index(const vector<int> &v) {
  vector<int> idx(v.size());
  iota(idx.begin(), idx.end(), 0);
  sort(idx.begin(), idx.end(), [&v](int i1, int i2) { return v[i1] > v[i2]; });
  return idx;
}

int main() {
  int d;
  cin >> d;
  for (int i = 0; i < N; i++)
    cin >> c[i];
  for (int i = 0; i < d; i++) {
    // vector<int> temp(N);
    for (int j = 0; j < N; j++) {
      cin >> s[i][j];
      // temp[i] = s[i][j];
    }
    // sorted[i] = sort_index(temp);
  }

  tic();
  int t = 0;
  ContestInfo ci(d);
  for (int i = 0; i < d; i++) {
    t = max_element(s[i], s[i] + N) - s[i];
    ci.add(i, t);
  }

  long current_score = ci.score;
  int best_score = current_score;
  ci.save();

  int dd, qq;
  while (toc() < MAX_T) {
    dd = dist_int(generator) % d;
    qq = dist_int(generator) % N;
    int old_q = ci.t[dd];
    ci.change(dd, qq);
    long new_score = ci.score;
    if (new_score < current_score && prob(MAX_T - toc()) < dice()) {
      ci.change(dd, old_q);
    } else {
      current_score = new_score;
      if (best_score < current_score) {
        best_score = current_score;
        ci.save();
      }
    }
  }
  ci.output();
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;

#define N 26
#define MAX_T 1.9
#define TRIAL 3

const int INF = 1 << 30;

int c[N];
int s[365][N];
vector<int> sorted[365];

std::chrono::time_point<std::chrono::steady_clock> start, endt;

random_device rd;
mt19937 generator(rd());
uniform_real_distribution<> dist_real(0.0, 1.0);
uniform_int_distribution<> dist_int(0, RAND_MAX);

struct ContestInfo {
  int D;
  int score;
  int best_score;
  vector<int> t;
  vector<int> best_t;
  set<int> held[N];
  ContestInfo(int dd) : D(dd), score(0) {
    t.resize(D, -1);
    for (int i = 0; i < N; i++) {
      held[i].insert(-1);
      held[i].insert(D);
      score -= c[i] * D * (D + 1) / 2;
    }
  }

  void add(int dd, int qq) {
    if (t[dd] != -1) {
      cout << "Add faild!" << endl;
      exit(1);
    }

    t[dd] = qq;
    score += s[dd][qq];
    held[qq].insert(dd);
    auto itr = held[qq].find(dd);
    auto ritr = next(itr);
    auto litr = prev(itr);
    auto range = [](int l, int r) {
      int k = r - l - 1;
      return -k * (k + 1) / 2;
    };
    score -= c[qq] * range(*litr, *ritr);
    score += c[qq] * (range(*litr, *itr) + range(*itr, *ritr));
  }

  void erase(int dd) {
    if (t[dd] == -1) {
      cout << "Erase faild!" << endl;
      exit(1);
    }
    int qq = t[dd];
    t[dd] = -1;
    score -= s[dd][qq];
    auto itr = held[qq].find(dd);
    auto ritr = next(itr);
    auto litr = prev(itr);
    auto range = [](int l, int r) {
      int k = r - l - 1;
      return -k * (k + 1) / 2;
    };
    score += c[qq] * range(*litr, *ritr);
    score -= c[qq] * (range(*litr, *itr) + range(*itr, *ritr));
    held[qq].erase(itr);
  }

  void change(int dd, int qq) {
    erase(dd);
    add(dd, qq);
  }

  void save() {
    best_t = t;
    best_score = score;
  }

  void output(bool show = false) {
    for (int i = 0; i < D; i++)
      cout << best_t[i] + 1 << endl;
    if (show)
      cout << best_score << endl;
  }
};

double dice() { return dist_real(generator); }

double prob(double rem) {
  return 1.0 / exp(rem);
  // return 1.0 / pow(5, rem);
}

void tic() { start = std::chrono::steady_clock::now(); }

double toc() {
  endt = std::chrono::steady_clock::now();
  std::chrono::duration<double> elapsed_seconds = endt - start;
  return elapsed_seconds.count();
}

vector<int> sort_index(const vector<int> &v) {
  vector<int> idx(v.size());
  iota(idx.begin(), idx.end(), 0);
  sort(idx.begin(), idx.end(), [&v](int i1, int i2) { return v[i1] > v[i2]; });
  return idx;
}

int main() {
  int d;
  cin >> d;
  for (int i = 0; i < N; i++)
    cin >> c[i];
  for (int i = 0; i < d; i++) {
    vector<int> temp(N);
    for (int j = 0; j < N; j++) {
      cin >> s[i][j];
      temp[i] = s[i][j];
    }
    sorted[i] = sort_index(temp);
  }

  tic();
  int t = 0;
  ContestInfo ci(d);
  for (int i = 0; i < d; i++) {
    t = max_element(s[i], s[i] + N) - s[i];
    ci.add(i, t);
  }

  int current_score = ci.score;
  ci.save();

  int dd, qq;
  while (toc() < MAX_T) {
    dd = dist_int(generator) % d;
    int old_q = ci.t[dd];
    int new_score = -INF;
    int new_q = -1;
    for (int i = 0; i < TRIAL; i++) {
      // qq = sorted[dd][i];
      qq = dist_int(generator) % N;
      ci.change(dd, qq);
      if (new_score < ci.score) {
        new_score = ci.score;
        new_q = qq;
      }
    }
    if (new_score < current_score && prob(MAX_T - toc()) < dice()) {
      ci.change(dd, old_q);
    } else {
      current_score = new_score;
      ci.change(dd, new_q);
      if (ci.best_score < ci.score) {
        ci.save();
      }
    }
  }
  ci.output();
}