Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e7 + 1;
const long long MOD = 1000000007;
const long double PI = 3.1415926;
#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
typedef long long int ll;
typedef vector<ll> vi;
typedef vector<pair<ll, ll>> vp;
typedef vector<string> vs;
typedef vector<char> vc;
typedef list<ll> lst;

ll n, m, k, ans = 0, sum = 0, cnt = 0;
string s;
char c;

#define Endl endl

/*--------------------template--------------------*/

int main() {
  cin >> n;
  vi v(n), a(n);
  REP(i, n) cin >> v[i];
  REP(i, n) {
    if (v[i] == i + 1) {
      a[i] = 1;
    } else {
      a[i] = 0;
    }
  }

  if (a[0] == 1) {
    swap(v[0], v[1]);
    a[0] = 0;
    if (v[1] == 2) {
      a[1] = 1;
    } else {
      a[1] = 0;
    }
    cnt++;
  }
  REP(i, n) {
    if (a[i] == 1) {
      a[i] = 0;
      if (a[i + 1] == 1) {
        swap(v[i], v[i + 1]);
        a[i] = a[i + 1] = 0;
      } else if (v[i + 1] > i) {
        swap(v[i], v[i + 1]);
        if (v[i + 1] == i + 2) {
          a[i + 1] = 1;
        } else {
          a[i + 1] = 0;
        }
      } else {
        swap(v[i], v[i - 1]);
        if (v[i - 1] == i) {
          a[i - 1] = 1;
        } else {
          a[i - 1] = 0;
        }
      }
      cnt++;
    }
  }

  if (a[n - 1] == 1) {
    swap(v[n - 1], v[n - 2]);
    a[n - 1] = 0;
    if (v[n - 1] == n) {
      a[n - 2] = 1;
    } else {
      a[n - 2] = 0;
    }
    cnt++;
  }

  cout << cnt << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e7 + 1;
const long long MOD = 1000000007;
const long double PI = 3.1415926;
#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
typedef long long int ll;
typedef vector<ll> vi;
typedef vector<pair<ll, ll>> vp;
typedef vector<string> vs;
typedef vector<char> vc;
typedef list<ll> lst;

ll n, m, k, ans = 0, sum = 0, cnt = 0;
string s;
char c;

#define Endl endl

/*--------------------template--------------------*/

int main() {
  cin >> n;
  vi v(n), a(n);
  REP(i, n) cin >> v[i];
  REP(i, n) {
    if (v[i] == i + 1) {
      a[i] = 1;
    } else {
      a[i] = 0;
    }
  }

  if (a[0] == 1) {
    swap(v[0], v[1]);
    a[0] = 0;
    if (v[1] == 2) {
      a[1] = 1;
    } else {
      a[1] = 0;
    }
    cnt++;
  }
  REP(i, n - 1) {
    if (a[i] == 1) {
      a[i] = 0;
      if (a[i + 1] == 1) {
        swap(v[i], v[i + 1]);
        a[i] = a[i + 1] = 0;
      } else if (v[i + 1] > i) {
        swap(v[i], v[i + 1]);
        if (v[i + 1] == i + 2) {
          a[i + 1] = 1;
        } else {
          a[i + 1] = 0;
        }
      } else {
        swap(v[i], v[i - 1]);
        if (v[i - 1] == i) {
          a[i - 1] = 1;
        } else {
          a[i - 1] = 0;
        }
      }
      cnt++;
    }
  }

  if (a[n - 1] == 1) {
    swap(v[n - 1], v[n - 2]);
    a[n - 1] = 0;
    if (v[n - 1] == n) {
      a[n - 2] = 1;
    } else {
      a[n - 2] = 0;
    }
    cnt++;
  }

  cout << cnt << endl;
}