Problem p03343 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <limits>
using ll = long long;
constexpr int INF = std::numeric_limits<int>::max() / 2;
constexpr ll LNF = std::numeric_limits<ll>::max() / 2;

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

template <typename T> void print(vector<T> const &x, string s = "") {
  cerr << s;
  copy(x.begin(), x.end(), ostream_iterator<ll>(cerr, ", "));
  cerr << endl;
}

ll compute(vector<ll> v, int K, int Q, vector<int> &seps) {
  int N = v.size();
  sort(seps.begin(), seps.end());
  seps.push_back(N);
  print(seps, "seps: ");
  for (int i = 1; i < (int)seps.size(); i++) {
    sort(v.begin() + seps[i - 1] + 1, v.begin() + seps[i]);
  }
  print(v, "sorted v: ");

  vector<ll> ans;
  ans.reserve(N);
  for (int i = 1; i < (int)seps.size(); i++) {
    ans.insert(ans.end(), v.begin() + seps[i - 1] + 1,
               v.begin() + seps[i - 1] + 1 +
                   max(0, seps[i] - seps[i - 1] - 1 - K + 1));
  }
  print(ans, "ans: ");
  auto a = *min_element(ans.begin(), ans.end());
  nth_element(ans.begin(), ans.begin() + Q - 1, ans.end());
  sort(ans.begin(), ans.end());
  seps.pop_back();
  return Q <= (int)ans.size() ? ans[Q - 1] - a : LNF;
}

int main() {
  int N, K, Q;
  cin >> N >> K >> Q;
  vector<ll> orig(N);
  for (int i = 0; i < N; i++) {
    cin >> orig[i];
  }

  vector<pair<ll, int>> cp(N);
  for (int i = 0; i < N; i++) {
    cp[i].first = orig[i];
    cp[i].second = i;
  }
  sort(cp.begin(), cp.end());

  vector<int> seps = {-1};
  ll mi = compute(orig, K, Q, seps);
  for (auto const &p : cp) {
    seps.push_back(p.second);
    ll res = compute(orig, K, Q, seps);
    if (res == LNF)
      break; // ??????
    mi = min(mi, res);
  }
  cout << mi << endl;
  return 0;
}


Fixed submission:
#include <limits>
using ll = long long;
constexpr int INF = std::numeric_limits<int>::max() / 2;
constexpr ll LNF = std::numeric_limits<ll>::max() / 2;

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

template <typename T> void print(vector<T> const &x, string s = "") {
  // cerr << s;
  // copy(x.begin(), x.end(), ostream_iterator<ll>(cerr, ", "));
  // cerr << endl;
}

ll compute(vector<ll> v, int K, int Q, vector<int> &seps) {
  int N = v.size();
  sort(seps.begin(), seps.end());
  seps.push_back(N);
  print(seps, "seps: ");
  for (int i = 1; i < (int)seps.size(); i++) {
    sort(v.begin() + seps[i - 1] + 1, v.begin() + seps[i]);
  }
  print(v, "sorted v: ");

  vector<ll> ans;
  ans.reserve(N);
  for (int i = 1; i < (int)seps.size(); i++) {
    ans.insert(ans.end(), v.begin() + seps[i - 1] + 1,
               v.begin() + seps[i - 1] + 1 +
                   max(0, seps[i] - seps[i - 1] - 1 - K + 1));
  }
  print(ans, "ans: ");
  auto a = *min_element(ans.begin(), ans.end());
  nth_element(ans.begin(), ans.begin() + Q - 1, ans.end());
  sort(ans.begin(), ans.end());
  seps.pop_back();
  return Q <= (int)ans.size() ? ans[Q - 1] - a : LNF;
}

int main() {
  int N, K, Q;
  cin >> N >> K >> Q;
  vector<ll> orig(N);
  for (int i = 0; i < N; i++) {
    cin >> orig[i];
  }

  vector<pair<ll, int>> cp(N);
  for (int i = 0; i < N; i++) {
    cp[i].first = orig[i];
    cp[i].second = i;
  }
  sort(cp.begin(), cp.end());

  vector<int> seps = {-1};
  ll mi = compute(orig, K, Q, seps);
  for (auto const &p : cp) {
    seps.push_back(p.second);
    ll res = compute(orig, K, Q, seps);
    if (res == LNF)
      break; // ??????
    mi = min(mi, res);
  }
  cout << mi << endl;
  return 0;
}