Problem p03343 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
// #include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
// #include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

/*
unsigned seed1 = std::chrono::system_clock::now().time_since_epoch().count();
mt19937 g1.seed(seed1);
*/
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<pii, pii> ppiipii;
typedef pair<int, ppiipii> pippiipii;
typedef pair<int, pii> pipii;
typedef pair<pii, int> ppiii;
typedef pair<int, ppiii> pippiii;
typedef pair<int, ll> pill;
typedef pair<int, double> pid;
typedef pair<string, string> pss;
// note to self, the below two triggers someone
typedef pair<ll, ll> pll;
typedef pair<ll, int> plli;
typedef long double ld;

int l[2005];
set<int> bad;
int n, k, q;

int main() {
  scanf("%d%d%d", &n, &k, &q);
  for (int i = 0; i < n; i++) {
    scanf("%d", &l[i]);
    bad.insert(l[i]);
  }
  bad.insert(0);
  int ret = 1 << 30;
  for (int thresh : bad) {
    vector<int> all;
    for (int i = 0; i < n;) {
      if (l[i] <= thresh) {
        i++;
        continue;
      }
      int j = i + 1;
      while (j < n && l[j] > thresh)
        j++;
      // i inclusive, j exclusive
      vector<int> inside;
      for (int a = i; a < j; a++) {
        inside.push_back(l[a]);
      }
      sort(inside.begin(), inside.end());
      for (int a = 0; a <= inside.size() - k; a++) {
        all.push_back(inside[a]);
      }
      i = j;
    }
    sort(all.begin(), all.end());
    if (all.size() >= q) {
      ret = min(ret, all[q - 1] - all[0]);
    } else {
      break;
    }
  }
  printf("%d\n", ret);
}


Fixed submission:
#include <algorithm>
#include <cassert>
// #include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
// #include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

/*
unsigned seed1 = std::chrono::system_clock::now().time_since_epoch().count();
mt19937 g1.seed(seed1);
*/
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<pii, pii> ppiipii;
typedef pair<int, ppiipii> pippiipii;
typedef pair<int, pii> pipii;
typedef pair<pii, int> ppiii;
typedef pair<int, ppiii> pippiii;
typedef pair<int, ll> pill;
typedef pair<int, double> pid;
typedef pair<string, string> pss;
// note to self, the below two triggers someone
typedef pair<ll, ll> pll;
typedef pair<ll, int> plli;
typedef long double ld;

int l[2005];
set<int> bad;
int n, k, q;

int main() {
  scanf("%d%d%d", &n, &k, &q);
  for (int i = 0; i < n; i++) {
    scanf("%d", &l[i]);
    bad.insert(l[i]);
  }
  bad.insert(0);
  int ret = 1 << 30;
  for (int thresh : bad) {
    vector<int> all;
    for (int i = 0; i < n;) {
      if (l[i] <= thresh) {
        i++;
        continue;
      }
      int j = i + 1;
      while (j < n && l[j] > thresh)
        j++;
      // i inclusive, j exclusive
      vector<int> inside;
      for (int a = i; a < j; a++) {
        inside.push_back(l[a]);
      }
      sort(inside.begin(), inside.end());
      for (int a = 0; a + k <= inside.size(); a++) {
        all.push_back(inside[a]);
      }
      i = j;
    }
    sort(all.begin(), all.end());
    if (all.size() >= q) {
      ret = min(ret, all[q - 1] - all[0]);
    } else {
      break;
    }
  }
  printf("%d\n", ret);
}