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

#include <bits/stdc++.h>
#define fo(i, a, b) for (int i = a; i < b; ++i)
#define rep(i, n) fo(i, 0, n)
#define pln(n) printf("%lld", n)
#define sll(n) scanf("%lld", &n)
#define ss(n) scanf("%s", n)
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define dbg(x)                                                                 \\
  { cout << #x << ": " << (x) << endl; }
#define dbg2(x, y)                                                             \\
  { cout << #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define dbg3(x, y, z)                                                          \\
  {                                                                            \\
    cout << #x << ": " << (x) << " , " << #y << ": " << (y) << " , " << #z     \\
         << ": " << (z) << endl;                                               \\
  }
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define vll vector<long long>
#define vvll vector<vector<long long>>
#define vvvll vector<vector<vector<ll>>>
#define si set<int>
#define tr(c, it) for (decltype(c.begin()) it = c.begin(); it != c.end(); it++)
#define pis pair<int, string>
#define present(c, x) (c.find(x) != c.end())
#define cpresent(c, x) (find(all(c), x) != c.end())
#define ones(x) __builtin_popcount(x)
using namespace std;

int main() {
  // freopen("input.txt", "r", stdin);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int L[n];
  L[0] = 1;
  set<pair<int, int>, greater<pair<int, int>>> s;
  s.insert({L[0], 0});
  for (int i = 1; i < n; i++) {
    L[i] = 1;
    for (auto it = s.begin(); it != s.end(); it++) {
      if (abs(a[it->second] - a[i]) <= k) {
        L[i] = max(L[i], it->first + 1);
        break;
      }
    }
    s.insert({L[i], i});
  }
  cout << *max_element(L, L + n) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define fo(i, a, b) for (int i = a; i < b; ++i)
#define rep(i, n) fo(i, 0, n)
#define pln(n) printf("%lld", n)
#define sll(n) scanf("%lld", &n)
#define ss(n) scanf("%s", n)
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define dbg(x)                                                                 \\
  { cout << #x << ": " << (x) << endl; }
#define dbg2(x, y)                                                             \\
  { cout << #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define dbg3(x, y, z)                                                          \\
  {                                                                            \\
    cout << #x << ": " << (x) << " , " << #y << ": " << (y) << " , " << #z     \\
         << ": " << (z) << endl;                                               \\
  }
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define vll vector<long long>
#define vvll vector<vector<long long>>
#define vvvll vector<vector<vector<ll>>>
#define si set<int>
#define tr(c, it) for (decltype(c.begin()) it = c.begin(); it != c.end(); it++)
#define pis pair<int, string>
#define present(c, x) (c.find(x) != c.end())
#define cpresent(c, x) (find(all(c), x) != c.end())
#define ones(x) __builtin_popcount(x)
using namespace std;

int main() {
  // freopen("input.txt", "r", stdin);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int L[n];
  L[0] = 1;
  for (int i = 1; i < n; i++) {
    L[i] = 1;
    for (int j = i - 1; j >= 0; j--) {
      if (abs(a[j] - a[i]) <= k) {
        L[i] = max(L[i], L[j] + 1);
      }
    }
  }
  cout << *max_element(L, L + n) << endl;
}