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

#include <bits/stdc++.h>
#define endl "\\n"
using namespace std;

#define ll long long
const long long llINF = 1LL << 60;
const int iINF = 0x20000000;
template <class T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

void execute();
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false); // cin と scanfの併用禁止
  cout << fixed << setprecision(15);
  execute();
  return 0;
}

void execute() {
  int N, K;
  cin >> N >> K;
  vector<pair<int, int>> A(N);
  vector<pair<int, int>> B(N);
  for (int i = 0; i < N; i++) {
    int v;
    cin >> v;
    A[i] = make_pair(i, v);
  }

  auto &read = A;
  auto &write = B;
  for (int i = 0; i < K; i++) {
    bool allN = true;
    sort(read.begin(), read.end(), [](pair<int, int> &x, pair<int, int> &y) {
      return x.first - x.second < y.first - y.second;
    });

    int idx = 0;
    multiset<int> st;
    for (int j = 0; j < N; j++) {
      // 新規要素の追加
      do {
        auto &r = read[idx];
        if (r.first - r.second <= j) {
          st.insert(r.first + r.second);
        } else {
          break;
        }
      } while (++idx < N);

      // 要らないものの消去
      for (auto itr = st.begin(); itr != st.end();) {
        if (j <= *itr) {
          break;
        }
        itr = st.erase(itr);
      }

      write[j] = make_pair(j, st.size());

      if (st.size() != N)
        allN = false;
    }

    if (allN) {
      for (int i = 0; i < N - 1; i++) {
        cout << N << " ";
      }
      cout << N << endl;
      return;
    }

    auto &t = read;
    read = write;
    write = t;
  }

  for (int i = 0; i < N - 1; i++) {
    cout << read[i].second << " ";
  }
  cout << read[N - 1].second << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define endl "\\n"
using namespace std;

#define ll long long
const long long llINF = 1LL << 60;
const int iINF = 0x20000000;
template <class T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

void execute();
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false); // cin と scanfの併用禁止
  cout << fixed << setprecision(15);
  execute();
  return 0;
}

void execute() {
  int N, K;
  cin >> N >> K;
  vector<pair<int, int>> A(N);
  vector<pair<int, int>> B(N);
  for (int i = 0; i < N; i++) {
    int v;
    cin >> v;
    A[i] = make_pair(i, v);
  }

  auto &read = A;
  auto &write = B;
  for (int i = 0; i < K; i++) {
    sort(read.begin(), read.end(), [](pair<int, int> &x, pair<int, int> &y) {
      return x.first - x.second < y.first - y.second;
    });

    int idx = 0;
    multiset<int> st;
    for (int j = 0; j < N; j++) {
      // 新規要素の追加
      do {
        auto &r = read[idx];
        if (r.first - r.second <= j) {
          st.insert(r.first + r.second);
        } else {
          break;
        }
      } while (++idx < N);

      // 要らないものの消去
      for (auto itr = st.begin(); itr != st.end();) {
        if (j <= *itr) {
          break;
        }
        itr = st.erase(itr);
      }

      write[j] = make_pair(j, st.size());
    }

    auto &t = read;
    read = write;
    write = t;
  }

  for (int i = 0; i < N - 1; i++) {
    cout << read[i].second << " ";
  }
  cout << read[N - 1].second << endl;
}