Problem p02904 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int solve(const vector<int> &a, int k) {
  const int n = a.size();
  int count = 1;
  set<int> seg;
  vector<int> s(n);
  s[0] = 1;
  for (int i = 1; i < n; ++i) {
    if (a[i] > a[i - 1])
      s[i] = s[i - 1] + 1;
    else
      s[i] = 1;
  }
  if (*max_element(s.begin(), s.end()) >= k && s[k - 1] < k)
    ++count;
  for (int i = 0; i < k; ++i)
    seg.insert(a[i]);
  for (int i = k; i < n; ++i) {
    if (a[i - k] == *seg.begin() && a[i] > *(seg.rbegin()))
      ;
    else if (s[i] < k) {
      ++count;
    }
    seg.erase(a[i - k]);
    seg.insert(a[i]);
  }
  return count;
}
int solve_naive(const vector<int> &a, int k) {
  const int n = a.size();
  set<vector<int>> s;
  for (int i = k; i <= n; ++i) {
    vector<int> b = a;
    sort(b.begin() + i - k, b.begin() + i);
    s.insert(b);
  }
  return s.size();
}
int main() {
  //    int n = 7;
  //    vector<int> a(n);
  //    iota(a.begin(), a.end(), 0);
  //    do {
  //        for (int k = 2; k <= n; ++k) {
  //            if (solve(a, k) != solve_naive(a, k)) {
  //                cout << n << " " << k << "\n";
  //                cout << solve(a, k) << " " << solve_naive(a, k) << "\n";
  //                for (int v: a)
  //                    cout << v << " ";
  //                cout << "\n";
  //            }
  //        }
  //    } while (next_permutation(a.begin(), a.end()));
  //    cout << "done\n";
  //    return 0;
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  cout << solve_naive(a, k) << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int solve(const vector<int> &a, int k) {
  const int n = a.size();
  int count = 1;
  set<int> seg;
  vector<int> s(n);
  s[0] = 1;
  for (int i = 1; i < n; ++i) {
    if (a[i] > a[i - 1])
      s[i] = s[i - 1] + 1;
    else
      s[i] = 1;
  }
  if (*max_element(s.begin(), s.end()) >= k && s[k - 1] < k)
    ++count;
  for (int i = 0; i < k; ++i)
    seg.insert(a[i]);
  for (int i = k; i < n; ++i) {
    if (a[i - k] == *seg.begin() && a[i] > *(seg.rbegin()))
      ;
    else if (s[i] < k) {
      ++count;
    }
    seg.erase(a[i - k]);
    seg.insert(a[i]);
  }
  return count;
}
int solve_naive(const vector<int> &a, int k) {
  const int n = a.size();
  set<vector<int>> s;
  for (int i = k; i <= n; ++i) {
    vector<int> b = a;
    sort(b.begin() + i - k, b.begin() + i);
    s.insert(b);
  }
  return s.size();
}
int main() {
  //    int n = 7;
  //    vector<int> a(n);
  //    iota(a.begin(), a.end(), 0);
  //    do {
  //        for (int k = 2; k <= n; ++k) {
  //            if (solve(a, k) != solve_naive(a, k)) {
  //                cout << n << " " << k << "\n";
  //                cout << solve(a, k) << " " << solve_naive(a, k) << "\n";
  //                for (int v: a)
  //                    cout << v << " ";
  //                cout << "\n";
  //            }
  //        }
  //    } while (next_permutation(a.begin(), a.end()));
  //    cout << "done\n";
  //    return 0;
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  cout << solve(a, k) << "\n";
  return 0;
}