Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<pii> vpii;

#define ALL(x) (x).begin(), (x).end()
#define RESET(x) memset((x), 0, sizeof(x))
#define FOR(i, b, e) for (int i = (b); i < (e); ++i)
#define RE(i, n) FOR(i, 0, (n))
#define FOREACH(i, x, y) for (int i = (x); i <= (y); ++i)
#define two(X) (1 << (X))                 // NOTES:two(
#define contain(S, X) (((S)&two(X)) != 0) // NOTES:contain(
#define chkmin(X, Y) (X) = min((X), (Y))
#define chkmax(X, Y) (X) = max((X), (Y))
#define sqr(X) ((X) * (X))
#define MP(x, y) make_pair((x), (y))

const int LOGN = 22;
const ll INF = (1 << 30) + 10;
const ll MOD = 1e9 + 7;
const double PI = acos(-1.0);
const double EPS = 1e-12;

const long long maxn = 1e5 + 100;
const ll maxm = (ll)1e6;

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n;
  while (cin >> n) {
    vi a(n);
    RE(i, n) cin >> a[i];
    vi b(n + 1, -1);
    int ans = 0;
    RE(i, n) {
      int x = a[i];
      if (b[x - 1] == -1) {
        b[x] = 1;
        chkmax(ans, b[x]);
        continue;
      }
      b[x] = b[x - 1] + 1;
      chkmax(ans, b[x]);
    }
    cout << n - ans << endl;
  }

  return 0;
}

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

using namespace std;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<pii> vpii;

#define ALL(x) (x).begin(), (x).end()
#define RESET(x) memset((x), 0, sizeof(x))
#define FOR(i, b, e) for (int i = (b); i < (e); ++i)
#define RE(i, n) FOR(i, 0, (n))
#define FOREACH(i, x, y) for (int i = (x); i <= (y); ++i)
#define two(X) (1 << (X))                 // NOTES:two(
#define contain(S, X) (((S)&two(X)) != 0) // NOTES:contain(
#define chkmin(X, Y) (X) = min((X), (Y))
#define chkmax(X, Y) (X) = max((X), (Y))
#define sqr(X) ((X) * (X))
#define MP(x, y) make_pair((x), (y))

const int LOGN = 22;
const ll INF = (1 << 30) + 10;
const ll MOD = 1e9 + 7;
const double PI = acos(-1.0);
const double EPS = 1e-12;

const long long maxn = 1e5 + 100;
const ll maxm = (ll)1e6;

int main() {
#ifndef ONLINE_JUDGE
  //   freopen("in.txt", "r", stdin);
#endif

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n;
  while (cin >> n) {
    vi a(n);
    RE(i, n) cin >> a[i];
    vi b(n + 1, -1);
    int ans = 0;
    RE(i, n) {
      int x = a[i];
      if (b[x - 1] == -1) {
        b[x] = 1;
        chkmax(ans, b[x]);
        continue;
      }
      b[x] = b[x - 1] + 1;
      chkmax(ans, b[x]);
    }
    cout << n - ans << endl;
  }

  return 0;
}