Problem p02537 (C++)

Buggy submission (status: Time Limit Exceeded):

// Problem : D - Flat Subsequence
// Contest : AtCoder - ACL Beginner Contest
// URL : https://atcoder.jp/contests/abl/tasks/abl_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

// #pragma GCC optimize "trapv"//  WA to RE int!!
#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < n; i++)
#define FORR(i, n) for (int i = n; i >= 0; i--)
#define pb push_back
#define vint vector<int>
#define sint set<int>
#define mint map<int, int>
#define pint pair<int, int>
#define int long long int
#define test                                                                   \
  int tt1234;                                                                  \
  cin >> tt1234;                                                               \
  while (tt1234--)
#define endl "\n"
#define SI(a, n)                                                               \
  int a[n];                                                                    \
  FOR(i, n) cin >> a[i];
#define input(a, n)                                                            \
  int n;                                                                       \
  cin >> n;                                                                    \
  SI(a, n)
#define all(zz) zz.begin(), zz.end()
#define rep(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define error(x) cerr << #x << " is " << x << endl;
#define Error(a, b)                                                            \
  cerr << "( " << #a << " , " << #b << " ) = ( " << (a) << " , " << (b)        \
       << " )\n";
#define precision(x) cout << fixed << setprecision(x);
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define present(container, element) (container.find(element) != container.end())
#define fint                                                                   \
  fast;                                                                        \
  test
#define set(a, x) memset(a, x, sizeof(a))
#define L(x) ((x) << 1)
#define R(x) (((x) << 1) + 1)
#define ft first
#define se second
#define MOD 1000000007
using namespace std;
template <typename... T> void rd(T &...args) { ((cin >> args), ...); }
template <typename... T> void pp(T... args) {
  ((cout << args << " "), ...);
  cout << "\n";
}

void solve() {
  int n, k;
  rd(n, k);
  vint dp(n, 1);
  vint arr(n);
  for (int &v : arr)
    rd(v);
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < i; j++) {
      if (abs(arr[i] - arr[j]) <= k)
        dp[i] = max(dp[i], 1ll + dp[j]);
    }
  }
  pp(*max_element(all(dp)));

  return;
}

int32_t main() {
  // fint{
  fast;
  solve();
  // }
  return 0;
}


Fixed submission:

// Problem : D - Flat Subsequence
// Contest : AtCoder - ACL Beginner Contest
// URL : https://atcoder.jp/contests/abl/tasks/abl_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

// #pragma GCC optimize "trapv"//  WA to RE int!!
#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < n; i++)
#define FORR(i, n) for (int i = n; i >= 0; i--)
#define pb push_back
#define vint vector<int>
#define sint set<int>
#define mint map<int, int>
#define pint pair<int, int>
#define int long long int
#define test                                                                   \
  int tt1234;                                                                  \
  cin >> tt1234;                                                               \
  while (tt1234--)
#define endl "\n"
#define SI(a, n)                                                               \
  int a[n];                                                                    \
  FOR(i, n) cin >> a[i];
#define input(a, n)                                                            \
  int n;                                                                       \
  cin >> n;                                                                    \
  SI(a, n)
#define all(zz) zz.begin(), zz.end()
#define rep(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define error(x) cerr << #x << " is " << x << endl;
#define Error(a, b)                                                            \
  cerr << "( " << #a << " , " << #b << " ) = ( " << (a) << " , " << (b)        \
       << " )\n";
#define precision(x) cout << fixed << setprecision(x);
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define present(container, element) (container.find(element) != container.end())
#define fint                                                                   \
  fast;                                                                        \
  test
#define set(a, x) memset(a, x, sizeof(a))
#define L(x) ((x) << 1)
#define R(x) (((x) << 1) + 1)
#define ft first
#define se second
#define MOD 1000000007
using namespace std;
template <typename... T> void rd(T &...args) { ((cin >> args), ...); }
template <typename... T> void pp(T... args) {
  ((cout << args << " "), ...);
  cout << "\n";
}

void solve() {
  int n, k;
  rd(n, k);
  vint dp(n, 1);
  vint arr(n);
  for (int &v : arr)
    rd(v);
  for (int i = 1; i < n; i++) {
    int L = max(0ll, i - 99);
    for (int j = L; j < i; j++) {
      if (abs(arr[i] - arr[j]) <= k)
        dp[i] = max(dp[i], 1ll + dp[j]);
    }
  }
  pp(*max_element(all(dp)));

  return;
}

int32_t main() {
  // fint{
  fast;
  solve();
  // }
  return 0;
}