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

// #include <bits/stdc++.h>
#include <algorithm>
#include <chrono>
#include <iostream>
#include <vector>
using namespace std;

const int typeNum = 26;

void solve(long long D, std::vector<long long> c,
           std::vector<std::vector<long long>> s) {
  std::chrono::system_clock::time_point start, end; // 型は auto で可
  start = std::chrono::system_clock::now();         // 計測開始時間
  end = std::chrono::system_clock::now();
  double elapsed =
      std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
          .count();

  // initial t
  vector<long long> t = vector<long long>(D);
  int i = 0;
  for (auto &tt : t) {
    tt = i + 1;
    i = (i + 1) % typeNum;
  }

  long long pastSut = 0;
  long long iterator = 0;

  while (true) {
    // change id and value
    long long changedIndex = iterator % (t.size() - 1);
    if (changedIndex < 0)
      changedIndex += t.size() - 1;
    long long randomValue = (iterator * 18973) % typeNum + 1;
    if (randomValue < 1)
      randomValue += typeNum;
    long long pastValue = t[changedIndex];
    t[changedIndex] = randomValue;

    vector<long long> newT = vector<long long>();
    int skipCount = 0;
    for (int i = 0; i < t.size(); ++i) {
      if (i == changedIndex) {
        skipCount++;
        continue;
      }
      newT.push_back(t[i]);
    }
    for (int i = 0; i < skipCount; ++i) {
      newT.push_back(randomValue);
    }

    // current sutisfuction
    long long sut = 0;

    // make table
    vector<vector<long long>> table;
    for (int i = 0; i < typeNum; ++i) {
      table.push_back(vector<long long>(D));

      for (int j = 0; j < D; ++j) {
        table[i][j] = -1;
      }
    }

    for (int i = 0; i < D; ++i) {
      vector<long long> &target = table[newT[i] - 1];
      for (int j = i; j < target.size(); ++j) {
        target[j] = i;
      }
    }

    for (int d = 0; d < D; ++d) {
      sut += s[d][newT[d] - 1];

      // minus
      for (int i = 0; i < typeNum; ++i) {
        sut -= c[i] * (d - table[i][d]);
      }

      // todays sutisfuction
      // cout << sut << endl;
    }

    if (sut > pastSut) {
      // change
      pastSut = sut;
      for (int i = 0; i < t.size(); ++i) {
        t[i] = newT[i];
      }

    } else {
      // cancel
      // t[changedIndex] = pastValue;
    }

    iterator++;

    end = std::chrono::system_clock::now();
    elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
                  .count();
    if (elapsed > 1970)
      break;

    // break;
  }

  for (long long &tt : t) {
    cout << tt << endl;
  }

  // result sutisfuction
  // cout << pastSut << endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  long long D;
  scanf("%lld", &D);
  std::vector<long long> c(26);
  for (int i = 0; i < 26; i++) {
    scanf("%lld", &c[i]);
  }
  std::vector<std::vector<long long>> s(D, std::vector<long long>(26));
  for (int i = 0; i < D; i++) {
    for (int j = 0; j < 26; j++) {
      scanf("%lld", &s[i][j]);
    }
  }
  solve(D, std::move(c), std::move(s));
  return 0;
}


Error: TLE

Fixed submission:

// #include <bits/stdc++.h>
#include <algorithm>
#include <chrono>
#include <iostream>
#include <vector>
using namespace std;

const int typeNum = 26;

void solve(long long D, std::vector<long long> c,
           std::vector<std::vector<long long>> s) {
  std::chrono::system_clock::time_point start, end; // 型は auto で可
  start = std::chrono::system_clock::now();         // 計測開始時間
  end = std::chrono::system_clock::now();
  double elapsed =
      std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
          .count();

  // initial t
  vector<long long> t = vector<long long>(D);
  int i = 0;
  for (auto &tt : t) {
    tt = i + 1;
    i = (i + 1) % typeNum;
  }

  long long pastSut = 0;
  long long iterator = 0;

  while (true) {
    // change id and value
    long long changedIndex = iterator % (t.size() - 1);
    if (changedIndex < 0)
      changedIndex += t.size() - 1;
    long long randomValue = (iterator * 18973) % typeNum + 1;
    if (randomValue < 1)
      randomValue += typeNum;
    long long pastValue = t[changedIndex];
    t[changedIndex] = randomValue;

    vector<long long> newT = vector<long long>();
    int skipCount = 0;
    for (int i = 0; i < t.size(); ++i) {
      if (i = changedIndex) {
        skipCount++;
        continue;
      }
      newT.push_back(t[i]);
    }
    for (int i = 0; i < skipCount; ++i) {
      newT.push_back(randomValue);
    }

    // current sutisfuction
    long long sut = 0;

    // make table
    vector<vector<long long>> table;
    for (int i = 0; i < typeNum; ++i) {
      table.push_back(vector<long long>(D));

      for (int j = 0; j < D; ++j) {
        table[i][j] = -1;
      }
    }

    for (int i = 0; i < D; ++i) {
      vector<long long> &target = table[newT[i] - 1];
      for (int j = i; j < target.size(); ++j) {
        target[j] = i;
      }
    }

    for (int d = 0; d < D; ++d) {
      sut += s[d][newT[d] - 1];

      // minus
      for (int i = 0; i < typeNum; ++i) {
        sut -= c[i] * (d - table[i][d]);
      }

      // todays sutisfuction
      // cout << sut << endl;
    }

    if (sut > pastSut) {
      // change
      pastSut = sut;
      for (int i = 0; i < t.size(); ++i) {
        t[i] = newT[i];
      }

    } else {
      // cancel
      // t[changedIndex] = pastValue;
    }

    iterator++;

    end = std::chrono::system_clock::now();
    elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
                  .count();
    if (elapsed > 1970)
      break;

    // break;
  }

  for (long long &tt : t) {
    cout << tt << endl;
  }

  // result sutisfuction
  // cout << pastSut << endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  long long D;
  scanf("%lld", &D);
  std::vector<long long> c(26);
  for (int i = 0; i < 26; i++) {
    scanf("%lld", &c[i]);
  }
  std::vector<std::vector<long long>> s(D, std::vector<long long>(26));
  for (int i = 0; i < D; i++) {
    for (int j = 0; j < 26; j++) {
      scanf("%lld", &s[i][j]);
    }
  }
  solve(D, std::move(c), std::move(s));
  return 0;
}