Problem p03271 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define N 500500
using namespace std;

int n, p[N], a[N], mi[N];

bool check(int u) {
  if (1 < u && u < n && p[u - 1] > p[u] && p[u] > p[u + 1])
    return swap(p[u - 1], p[u + 1]), true;
  else
    return false;
}

int stk[N], top;

int main() {
#ifdef isLOCAL
  freopen("work.in", "r", stdin);
  freopen("work.out", "w", stdout);
#endif

  cin >> n;
  for (int i = 1; i <= n; ++i)
    scanf("%d", p + i);

  p[n + 1] = n + 1, p[0] = 0;
  for (int i = 1; i <= n; ++i) {
    if (p[i - 1] != i - 1 && p[i] != i && p[i + 1] != i + 1)
      return puts("No"), 0;
    a[i] = i == p[i];
  }

  for (int i = 1, j; i <= n; i = j + 1) {
    for (j = i; j < n && a[j] != a[j + 1]; ++j)
      ;
    top = 0;
    for (int k = i; k <= j; ++k) {
      if (p[k] < i || j < p[k])
        return puts("No"), 0;
      if (!a[k])
        stk[++top] = p[k];
    }
    mi[top] = stk[top];
    for (int k = top - 1; k; --k)
      mi[k] = min(mi[k + 1], stk[k]);
    int mx = stk[1];
    for (int k = 2; k < top; ++k) {
      mx = max(mx, stk[k]);
      if (mi[k] < stk[k] && stk[k] < mx)
        return puts("No"), 0;
    }
  }

  return puts("Yes"), 0;

  queue<int> q;
  for (int i = 2; i < n; ++i)
    if (check(i))
      q.push(i);

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (check(u - 2))
      q.push(u - 2);
    if (check(u + 2))
      q.push(u + 2);
  }

  for (int i = 1; i <= n; ++i)
    if (p[i] != i)
      return puts("No"), 0;
  puts("Yes");

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define N 500500
using namespace std;

int n, p[N], a[N], mi[N];

bool check(int u) {
  if (1 < u && u < n && p[u - 1] > p[u] && p[u] > p[u + 1])
    return swap(p[u - 1], p[u + 1]), true;
  else
    return false;
}

int stk[N], top;

int main() {
#ifdef isLOCAL
  freopen("work.in", "r", stdin);
  freopen("work.out", "w", stdout);
#endif

  cin >> n;
  for (int i = 1; i <= n; ++i)
    scanf("%d", p + i);

  p[n + 1] = n + 1, p[0] = 0;
  for (int i = 1; i <= n; ++i) {
    if (p[i - 1] != i - 1 && p[i] != i && p[i + 1] != i + 1)
      return puts("No"), 0;
    a[i] = i == p[i];
  }

  for (int i = 1, j; i <= n; i = j + 1) {
    for (j = i; j < n && a[j] != a[j + 1]; ++j)
      ;
    top = 0;
    for (int k = i; k <= j; ++k) {
      if (p[k] < i || j < p[k])
        return puts("No"), 0;
      if (!a[k])
        stk[++top] = p[k];
    }
    mi[top] = stk[top];
    for (int k = top - 1; k >= 1; --k)
      mi[k] = min(mi[k + 1], stk[k]);
    int mx = stk[1];
    for (int k = 2; k < top; ++k) {
      mx = max(mx, stk[k]);
      if (mi[k] < stk[k] && stk[k] < mx)
        return puts("No"), 0;
    }
  }

  return puts("Yes"), 0;

  queue<int> q;
  for (int i = 2; i < n; ++i)
    if (check(i))
      q.push(i);

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (check(u - 2))
      q.push(u - 2);
    if (check(u + 2))
      q.push(u + 2);
  }

  for (int i = 1; i <= n; ++i)
    if (p[i] != i)
      return puts("No"), 0;
  puts("Yes");

  return 0;
}