Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define il inline
#define rg register
#define lch(x) ((x) << 1)
#define rch(x) ((x) << 1 ^ 1)
#define rep(i, st, ed) for (rg int i = (st); i <= (ed); ++i)
#define per(i, ed, st) for (rg int i = (ed); i >= (st); --i)
#define rep0(i, n) for (rg int i = 0; i < (n); ++i)
#define per0(i, n) for (rg int i = (n)-1; i >= 0; --i)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef double dbl;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename T> il T qmin(const T &x, const T &y) {
  return x < y ? x : y;
}
template <typename T> il T qmax(const T &x, const T &y) {
  return x > y ? x : y;
}
template <typename T> il void getmin(T &x, const T &y) {
  if (y < x)
    x = y;
}
template <typename T> il void getmax(T &x, const T &y) {
  if (y > x)
    x = y;
}
il void fileio(const string &s) {
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}

const int inf = (int)1e9 + 7;
const ll linf = (ll)1e18 + 7;
const int N = 1e5 + 7;

int a[N], pos[N], n;

int main() {
  scanf("%d", &n);
  rep(i, 1, n) scanf("%d", a + i), pos[a[i]] = i;
  int cur = 0, ans = 0;
  rep(i, 1, n) {
    if (pos[i] > pos[i - 1])
      ++cur;
    else
      cur = 1;
    getmax(ans, cur);
  }
  printf("%d\n", n - ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define il inline
#define rg register
#define lch(x) ((x) << 1)
#define rch(x) ((x) << 1 ^ 1)
#define rep(i, st, ed) for (rg int i = (st); i <= (ed); ++i)
#define per(i, ed, st) for (rg int i = (ed); i >= (st); --i)
#define rep0(i, n) for (rg int i = 0; i < (n); ++i)
#define per0(i, n) for (rg int i = (n)-1; i >= 0; --i)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef double dbl;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename T> il T qmin(const T &x, const T &y) {
  return x < y ? x : y;
}
template <typename T> il T qmax(const T &x, const T &y) {
  return x > y ? x : y;
}
template <typename T> il void getmin(T &x, const T &y) {
  if (y < x)
    x = y;
}
template <typename T> il void getmax(T &x, const T &y) {
  if (y > x)
    x = y;
}
il void fileio(const string &s) {
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}

const int inf = (int)1e9 + 7;
const ll linf = (ll)1e18 + 7;
const int N = 2e5 + 7;

int a[N], pos[N], n;

int main() {
  scanf("%d", &n);
  rep(i, 1, n) scanf("%d", a + i), pos[a[i]] = i;
  int cur = 0, ans = 0;
  rep(i, 1, n) {
    if (pos[i] > pos[i - 1])
      ++cur;
    else
      cur = 1;
    getmax(ans, cur);
  }
  printf("%d\n", n - ans);
  return 0;
}