Problem p03346 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define mp make_pair
#define FOR(i, a, b) for (int(i) = a; (i) < (b); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define FFOR(i, a, b) for (int(i) = a; (i) > (b); --(i))
#define rrep(i, n) FFOR(i, 0, n)
#define vsort(v) sort((v).begin(), (v).end());           // 小さい順
#define rvsort(v) sort(v.begin(), v.end(), greater<>()); // 大きい順
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define all(c) (c).begin(), (c).end()
#define ll long long
#define lb long double
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vs vector<string>
#define vc vector<char>
#define vvc vector<vector<char>>
#define Print(p) cout << (p) << endl
#define F first
#define S second
#define pb push_back
#define ll long long
#define mod 1000000007LL
#define INF 123456789012345
typedef pair<int, int> P;
typedef pair<int, P> PP;

const int maxn = 1e5 + 10;
int p[maxn];
int check[maxn];

int main() {
  int n;
  cin >> n;
  rep(i, n) {
    cin >> p[i];
    p[i]--;
    check[i] = -1;
  }

  rep(i, n) {
    if (p[i] == 0) {
      check[0] = 0;
      continue;
    }

    if (check[p[i] - 1] == -1)
      check[p[i]] = 0;
    else
      check[p[i]] = 1;
  }

  int ans = 0;
  int sum = 0;
  rep(i, n) {
    if (check[i] == 1)
      sum++;
    else
      sum = 0;
    ans = max(ans, sum);
  }
  cout << n - ans - 1 << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define mp make_pair
#define FOR(i, a, b) for (int(i) = a; (i) < (b); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define FFOR(i, a, b) for (int(i) = a; (i) > (b); --(i))
#define rrep(i, n) FFOR(i, 0, n)
#define vsort(v) sort((v).begin(), (v).end());           // 小さい順
#define rvsort(v) sort(v.begin(), v.end(), greater<>()); // 大きい順
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define all(c) (c).begin(), (c).end()
#define ll long long
#define lb long double
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vs vector<string>
#define vc vector<char>
#define vvc vector<vector<char>>
#define Print(p) cout << (p) << endl
#define F first
#define S second
#define pb push_back
#define ll long long
#define mod 1000000007LL
#define INF 123456789012345
typedef pair<int, int> P;
typedef pair<int, P> PP;

const int maxn = 2e5 + 10;
int p[maxn];
int check[maxn];

int main() {
  int n;
  cin >> n;
  rep(i, n) {
    cin >> p[i];
    p[i]--;
    check[i] = -1;
  }

  rep(i, n) {
    if (p[i] == 0) {
      check[0] = 0;
      continue;
    }

    if (check[p[i] - 1] == -1)
      check[p[i]] = 0;
    else
      check[p[i]] = 1;
  }

  int ans = 0;
  int sum = 0;
  rep(i, n) {
    if (check[i] == 1)
      sum++;
    else
      sum = 0;
    ans = max(ans, sum);
  }
  cout << n - ans - 1 << endl;

  return 0;
}