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;

#ifndef DEBUG_HPP_OB5Y82L9
#define DEBUG_HPP_OB5Y82L9

#ifndef STREAM_HPP_L5EPV267
#define STREAM_HPP_L5EPV267
#include <iostream>

namespace pcl {
struct empty_stream_buf : public std::streambuf {
  char dummybuf[8];

protected:
  virtual int overflow(int c) {
    setp(dummybuf, dummybuf + sizeof(dummybuf));
    return c == traits_type::eof() ? '\0' : c;
  }
};

struct empty_stream : private empty_stream_buf, public std::ostream {
public:
  empty_stream() : std::ostream(this) {}
  empty_stream_buf const *rdbuf() const { return this; }
};
} // namespace pcl

#endif /* end of include guard: STREAM_HPP_L5EPV267 */

#include <iostream>

#ifndef PA_DEBUG
namespace pcl {
pcl::empty_stream _debg;
#define cerr pcl::_debg
} // namespace pcl
#endif

#endif /* end of include guard: DEBUG_HPP_OB5Y82L9 */

#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;
}