Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, j, n) for (ll i = j; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define INF (1000000000)
#define MOD (1000000007)
#define MAX (100000)
#define pii pair<int, int>

/////////////////////////////////////////////////////////

class XY {
public:
  ll x, y;
  XY() { x = y = 0; }
  XY(ll u, ll v) {
    x = u;
    y = v;
  }
};

class e {
public:
  ll a, b;
  e() { a = b = -1; }
  e(ll u, ll v) {
    a = u;
    b = v;
  }
};

template <typename T1, typename T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

template <typename T1, typename T2> ll mypow(T1 a, T2 n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return a;
  if (n % 2)
    return a * mypow(a, n - 1);
  ll tmp = mypow(a, n / 2);
  return tmp * tmp;
}

template <typename T> int BS(vector<T> V, int left, int right, T key) {
  int mid = (left + right) / 2;
  if (V[mid] <= key)
    left = mid;
  else
    right = mid;
  if (right - mid == 1)
    return left;
  else
    return BS(V, left, right, key);
}

ll comb(ll n, ll r) {
  ll res = 1;
  rep(i, 0, r) {
    res *= n - i;
    res /= i + 1;
  }
  return res;
}

template <typename T> T euclid(T a, T b) {
  if (b == 0)
    return a;
  else
    return euclid(b, a % b);
}

/////////////////////////////////////////////////////////

void Main() {
  ll N;
  cin >> N;
  vector<ll> P(N);
  vector<ll> Q(N);
  rep(i, 0, N) {
    cin >> P[i];
    P[i]--;
    Q[P[i]] = i;
  }
  ll cnt = 1;
  ll cur = 1;
  ll ma = 0;
  while (true) {
    if (cur == N - 1) {
      chmax(ma, cnt);
      break;
    }
    if (Q[cur + 1] > Q[cur])
      cnt++;
    else {
      chmax(ma, cnt);
      cnt = 1;
    }
    cur++;
  }
  cout << N - ma << endl;
}

/////////////////////////////////////////////////////////

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << std::fixed << std::setprecision(15);

  Main();
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, j, n) for (ll i = j; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define INF (1000000000)
#define MOD (1000000007)
#define MAX (100000)
#define pii pair<int, int>

/////////////////////////////////////////////////////////

class XY {
public:
  ll x, y;
  XY() { x = y = 0; }
  XY(ll u, ll v) {
    x = u;
    y = v;
  }
};

class e {
public:
  ll a, b;
  e() { a = b = -1; }
  e(ll u, ll v) {
    a = u;
    b = v;
  }
};

template <typename T1, typename T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

template <typename T1, typename T2> ll mypow(T1 a, T2 n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return a;
  if (n % 2)
    return a * mypow(a, n - 1);
  ll tmp = mypow(a, n / 2);
  return tmp * tmp;
}

template <typename T> int BS(vector<T> V, int left, int right, T key) {
  int mid = (left + right) / 2;
  if (V[mid] <= key)
    left = mid;
  else
    right = mid;
  if (right - mid == 1)
    return left;
  else
    return BS(V, left, right, key);
}

ll comb(ll n, ll r) {
  ll res = 1;
  rep(i, 0, r) {
    res *= n - i;
    res /= i + 1;
  }
  return res;
}

template <typename T> T euclid(T a, T b) {
  if (b == 0)
    return a;
  else
    return euclid(b, a % b);
}

/////////////////////////////////////////////////////////

void Main() {
  ll N;
  cin >> N;
  vector<ll> P(N);
  vector<ll> Q(N);
  rep(i, 0, N) {
    cin >> P[i];
    P[i]--;
    Q[P[i]] = i;
  }
  ll cnt = 1;
  ll cur = 0;
  ll ma = 0;
  while (true) {
    if (cur == N - 1) {
      chmax(ma, cnt);
      break;
    }
    if (Q[cur + 1] > Q[cur])
      cnt++;
    else {
      chmax(ma, cnt);
      cnt = 1;
    }
    cur++;
  }
  cout << N - ma << endl;
}

/////////////////////////////////////////////////////////

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << std::fixed << std::setprecision(15);

  Main();
}