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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef pair<int, int> ii;
typedef pair<ll, ll> pll;

class Sched {
public:
  Sched(int D) : D(D) {
    c = vector<int>(26);
    for (int i = 0; i < 26; i++) {
      cin >> c[i];
    }
    s = vector<vector<int>>(D, vector<int>(26));
    for (int i = 0; i < D; i++) {
      for (int j = 0; j < 26; j++) {
        cin >> s[i][j];
      }
    }
  }

  vector<int> choose() {
    vector<int> choice(D);
    vector<int> l(26);
    for (int d = 0; d < D; d++) {
      int best = 0;
      for (int i = 0; i < 26; i++) {
        int dm = c[i] * (d + 1 - l[i]) - s[d][i];
        if (dm < best) {
          best = dm;
          choice[d] = i;
        }
      }
      l[choice[d]] = d + 1;
    }
    return choice;
  }

  ll score(vector<int> &contest) {
    ll score = 0;
    vector<int> l(26);
    for (int d = 0; d < D; d++) {
      l[contest[d]] = d + 1;
      score += s[d][contest[d]];
      for (int i = 0; i < 26; i++) {
        score -= c[i] * (d + 1 - l[i]);
      }
    }
    return score;
  }

private:
  vector<int> c;
  vector<vector<int>> s;
  int D;
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int D;
  cin >> D;
  Sched scheduler(D);
  auto c = scheduler.choose();
  ll score = scheduler.score(c);
  for (int i = 0; i < 10000; i++) {
    int b = c[i % D];
    for (int j = 0; j < 26; j++) {
      if (j != b) {
        c[i % D] = j;
        ll score2 = scheduler.score(c);
        if (score2 > score) {
          b = j;
          score = score2;
        }
      }
    }
    c[i % D] = b;
  }
  for (auto r : c) {
    cout << r + 1 << "\\n";
  }
  cerr << score << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef pair<int, int> ii;
typedef pair<ll, ll> pll;

class Sched {
public:
  Sched(int D) : D(D) {
    c = vector<int>(26);
    for (int i = 0; i < 26; i++) {
      cin >> c[i];
    }
    s = vector<vector<int>>(D, vector<int>(26));
    for (int i = 0; i < D; i++) {
      for (int j = 0; j < 26; j++) {
        cin >> s[i][j];
      }
    }
  }

  vector<int> choose() {
    vector<int> choice(D);
    vector<int> l(26);
    for (int d = 0; d < D; d++) {
      int best = 0;
      for (int i = 0; i < 26; i++) {
        int dm = c[i] * (d + 1 - l[i]) - s[d][i];
        if (dm < best) {
          best = dm;
          choice[d] = i;
        }
      }
      l[choice[d]] = d + 1;
    }
    return choice;
  }

  ll score(vector<int> &contest) {
    ll score = 0;
    vector<int> l(26);
    for (int d = 0; d < D; d++) {
      l[contest[d]] = d + 1;
      score += s[d][contest[d]];
      for (int i = 0; i < 26; i++) {
        score -= c[i] * (d + 1 - l[i]);
      }
    }
    return score;
  }

private:
  vector<int> c;
  vector<vector<int>> s;
  int D;
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int D;
  cin >> D;
  Sched scheduler(D);
  auto c = scheduler.choose();
  ll score = scheduler.score(c);
  for (int i = 0; i < 500000; i++) {
    int old = c[i % D];
    c[i % D] = rand() % 26;
    ll score2 = scheduler.score(c);
    if (score2 < score) {
      c[i % D] = old;
    }
  }
  for (auto r : c) {
    cout << r + 1 << "\\n";
  }
  cerr << score << endl;
  return 0;
}