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

#include <bits/stdc++.h>
using namespace std;

int main(void) {
  long long D;
  cin >> D;
  vector<long long> c(26);
  vector<vector<long long>> used(26);
  for (long long i = 0; i < 26; i++) {
    cin >> c[i];
    used[i].push_back(-1);
  }
  vector<vector<long long>> s(D, vector<long long>(26));
  for (long long i = 0; i < D; i++) {
    for (long long j = 0; j < 26; j++) {
      cin >> s.at(i).at(j);
    }
  }

  long long lastnum = 0;
  vector<long long> ans(D);
  for (long long i = 0; i < D; i++) {
    long long pena = 0;
    for (long long j = 0; j < 26; j++) {
      pena += c[j] * (i - used.at(j).at(used[j].size() - 1));
    }
    ans[i] = -1;
    long long cnt = -100000000;
    for (long long j = 0; j < 26; j++) {
      pena -= c[j] * (i - used.at(j).at(used[j].size() - 1));
      if (s.at(i).at(j) - pena > cnt) {
        ans[i] = j;
        cnt = s.at(i).at(j) - pena;
      }
      pena += c[j] * (i - used.at(j).at(used[j].size() - 1));
    }
    used[ans[i]].push_back(i);
    lastnum += cnt;
  }
  double start_time = clock();
  double start_temp = lastnum / 2;
  double end_temp = 10;
  double T = start_temp;

  long long check = 0;
  while (clock() < 1.9 * CLOCKS_PER_SEC) {
    T = start_temp - (clock() - start_time) * (start_temp - end_temp) /
                         ((1.9 * CLOCKS_PER_SEC) - start_time);
    long long cnt = 100000000;
    long long ranD = rand() % D;
    long long ranq = rand() % 26;
    // for (long long i = 0; i < 26; i++)
    // {
    //     if (i != ans[ranD])
    //     {
    //         if (abs(s.at(ranD).at(ans[ranD]) - s.at(ranD).at(i)) < cnt)
    //         {
    //             ranq = i;
    //             cnt = abs(s.at(ranD).at(ans[ranD]) - s.at(ranD).at(i));
    //         }
    //     }
    // }

    used[ans[ranD]].erase(
        remove(used[ans[ranD]].begin(), used[ans[ranD]].end(), ranD),
        used[ans[ranD]].end());
    used[ranq].push_back(ranD);
    sort(used[ranq].begin(), used[ranq].end());
    long long oldans = ans[ranD];
    ans[ranD] = ranq;

    long long newnum = 0;
    for (long long i = 0; i < D; i++) {
      newnum += s.at(i).at(ans[i]);
    }
    for (long long j = 0; j < 26; j++) {
      long long pena = 0;
      for (long long i = 0; i < used[j].size(); i++) {
        if (i == used[j].size() - 1) {
          pena += c[j] * (D - used[j][i] - 1) * (D - used[j][i]) / 2;
        } else {
          pena += c[j] * (used[j][i + 1] - used[j][i]) *
                  (used[j][i + 1] - used[j][i] - 1) / 2;
        }
      }
      newnum -= pena;
    }

    double randT = ((rand() % RAND_MAX) / (double)RAND_MAX);
    double prob = exp((lastnum - newnum) / T);

    if (newnum > lastnum) {
      lastnum = newnum;
    } else if (randT <= pow(prob, -1)) {
      lastnum = newnum;
    } else {
      ans[ranD] = oldans;
      used[ranq].erase(remove(used[ranq].begin(), used[ranq].end(), ranD),
                       used[ranq].end());
      used[ans[ranD]].push_back(ranD);
      sort(used[ans[ranD]].begin(), used[ans[ranD]].end());
    }
    check++;
  }

  for (long long i = 0; i < D; i++) {
    cout << ans[i] + 1 << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main(void) {
  long long D;
  cin >> D;
  vector<long long> c(26);
  vector<vector<long long>> used(26);
  for (long long i = 0; i < 26; i++) {
    cin >> c[i];
    used[i].push_back(-1);
  }
  vector<vector<long long>> s(D, vector<long long>(26));
  for (long long i = 0; i < D; i++) {
    for (long long j = 0; j < 26; j++) {
      cin >> s.at(i).at(j);
    }
  }

  long long lastnum = 0;
  vector<long long> ans(D);
  for (long long i = 0; i < D; i++) {
    long long pena = 0;
    for (long long j = 0; j < 26; j++) {
      pena += c[j] * (i - used.at(j).at(used[j].size() - 1));
    }
    ans[i] = -1;
    long long cnt = -100000000;
    for (long long j = 0; j < 26; j++) {
      pena -= c[j] * (i - used.at(j).at(used[j].size() - 1));
      if (s.at(i).at(j) - pena > cnt) {
        ans[i] = j;
        cnt = s.at(i).at(j) - pena;
      }
      pena += c[j] * (i - used.at(j).at(used[j].size() - 1));
    }
    used[ans[i]].push_back(i);
    lastnum += cnt;
  }
  double start_time = clock();
  double start_temp = lastnum / 2;
  double end_temp = 10;
  double T = start_temp;

  long long check = 0;
  while (clock() < 1.99 * CLOCKS_PER_SEC) {
    T = start_temp - (clock() - start_time) * (start_temp - end_temp) /
                         ((1.99 * CLOCKS_PER_SEC) - start_time);
    long long cnt = 100000000;
    long long ranD = rand() % D;
    long long ranq = rand() % 26;
    // for (long long i = 0; i < 26; i++)
    // {
    //     if (i != ans[ranD])
    //     {
    //         if (abs(s.at(ranD).at(ans[ranD]) - s.at(ranD).at(i)) < cnt)
    //         {
    //             ranq = i;
    //             cnt = abs(s.at(ranD).at(ans[ranD]) - s.at(ranD).at(i));
    //         }
    //     }
    // }

    used[ans[ranD]].erase(
        remove(used[ans[ranD]].begin(), used[ans[ranD]].end(), ranD),
        used[ans[ranD]].end());
    used[ranq].push_back(ranD);
    sort(used[ranq].begin(), used[ranq].end());
    long long oldans = ans[ranD];
    ans[ranD] = ranq;

    long long newnum = 0;
    for (long long i = 0; i < D; i++) {
      newnum += s.at(i).at(ans[i]);
    }
    for (long long j = 0; j < 26; j++) {
      long long pena = 0;
      for (long long i = 0; i < used[j].size(); i++) {
        if (i == used[j].size() - 1) {
          pena += c[j] * (D - used[j][i] - 1) * (D - used[j][i]) / 2;
        } else {
          pena += c[j] * (used[j][i + 1] - used[j][i]) *
                  (used[j][i + 1] - used[j][i] - 1) / 2;
        }
      }
      newnum -= pena;
    }

    double randT = ((rand() % RAND_MAX) / (double)RAND_MAX);
    double prob = exp((lastnum - newnum) / T);

    if (newnum > lastnum) {
      lastnum = newnum;
    } else if (randT <= pow(prob, -1)) {
      lastnum = newnum;
    } else {
      ans[ranD] = oldans;
      used[ranq].erase(remove(used[ranq].begin(), used[ranq].end(), ranD),
                       used[ranq].end());
      used[ans[ranD]].push_back(ranD);
      sort(used[ans[ranD]].begin(), used[ans[ranD]].end());
    }
    check++;
  }

  for (long long i = 0; i < D; i++) {
    cout << ans[i] + 1 << endl;
  }
}