prithivMLmods/Coder-Stat huggingface dataset:
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, x, n) for (int i = (x); i < (n); ++i)
#define ll long long
#define P pair<ll, ll>
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()

const ll mod = 1e9 + 7;
const ll INF = 1e18;
const double pi = acos(-1);

int main(void) {
  ll n, cnt = 0;
  cin >> n;
  vector<ll> p(n);
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, n) {
    if (p[i] == i) {
      swap(p[i + 1], p[i]);
      cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, x, n) for (int i = (x); i < (n); ++i)
#define ll long long
#define P pair<ll, ll>
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()

const ll mod = 1e9 + 7;
const ll INF = 1e18;
const double pi = acos(-1);

int main(void) {
  ll n, cnt = 0;
  cin >> n;
  vector<ll> p(n);
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  p.push_back(0);
  rep(i, n) {
    if (p[i] == i) {
      swap(p[i + 1], p[i]);
      cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
int dx[] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const ll MOD = 1000000007;
const ll INF = 1000000;
const int MAX = 10000000;

int p[10005];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }

  int ans = 0;
  for (int i = 0; i < n - 1; i++) {
    if (p[i] != i)
      continue;
    swap(p[i], p[i + 1]);
    ans++;
  }
  if (p[n - 1] == n - 1)
    ans++;
  cout << ans << endl;
}

Fixed submission:

#include <algorithm>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
int dx[] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const ll MOD = 1000000007;
const ll INF = 1000000;
const int MAX = 10000000;

int p[100005];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }

  int ans = 0;
  for (int i = 0; i < n - 1; i++) {
    if (p[i] != i)
      continue;
    swap(p[i], p[i + 1]);
    ans++;
  }
  if (p[n - 1] == n - 1)
    ans++;
  cout << ans << endl;
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

int main() {
  int n;
  int a[10005];
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int ans = 0, t;
  for (int i = 1; i <= n; i++) {
    if (i == a[i]) {
      t = a[i];
      a[i] = a[i + 1];
      a[i + 1] = t;
      ans++;
    }
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

int main() {
  int n;
  int a[100005];
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int ans = 0, t;
  for (int i = 1; i <= n; i++) {
    if (i == a[i]) {
      t = a[i];
      a[i] = a[i + 1];
      a[i + 1] = t;
      ans++;
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main() {
  ll N;
  cin >> N;
  vector<ll> num(N, 0);
  ll cnt = 0;
  ll streak = 0;
  for (int i = 1; i <= N; i++) {
    cin >> num[i];
    if (num[i] == i) {
      cnt++;
      streak++;
      if (streak % 2 == 0) {
        cnt--;
      }
    } else {
      streak = 0;
    }
  }

  cout << cnt << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main() {
  ll N;
  cin >> N;
  vector<ll> num(N + 1, 0);
  ll cnt = 0;
  ll streak = 0;
  for (int i = 1; i <= N; i++) {
    cin >> num[i];
    if (num[i] == i) {
      cnt++;
      streak++;
      if (streak % 2 == 0) {
        cnt--;
      }
    } else {
      streak = 0;
    }
  }

  cout << cnt << endl;
}
Problem p03612 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <vector>
using namespace std;

typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<long long> vll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<bool>> vvb;
typedef vector<vector<int>> vvi;

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define all(a) a.begin(), a.end()

int main() {
  int n;
  cin >> n;
  vi p(n);
  rep(i, n) { cin >> p.at(i); }
  bool ok = true;
  int cnt = 0;
  while (ok) {
    ok = false;

    rep(i, n) {
      if (p.at(i) == i + 1) {
        if (i != n - 1) {
          swap(p.at(i), p.at(i + 1));
        } else {
          swap(p.at(i), p.at(i - 1));
        }
        cnt++;
        break;
      }
    }

    rep(i, n) {
      if (p.at(i) == i + 1) {
        ok = true;
        break;
      }
    }
  }
  cout << cnt << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <vector>
using namespace std;

typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<long long> vll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<bool>> vvb;
typedef vector<vector<int>> vvi;

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define all(a) a.begin(), a.end()

int main() {
  int n;
  cin >> n;
  vi p(n);
  rep(i, n) { cin >> p.at(i); }
  bool ok = true;
  int cnt = 0;
  while (ok) {
    ok = false;

    rep(i, n) {
      if (p.at(i) == i + 1) {
        if (i != n - 1) {
          swap(p.at(i), p.at(i + 1));
        } else {
          swap(p.at(i), p.at(i - 1));
        }
        cnt++;
      }
    }

    rep(i, n) {
      if (p.at(i) == i + 1) {
        ok = true;
        break;
      }
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int n, a[10000];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int dem = 0;
  for (int i = 1; i <= n; i++) {
    while (a[i] == i) {
      swap(a[i], a[i + 1]);
      i = i + 1;
      dem++;
    }
  }
  cout << dem;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

long long n, a[1000000];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int dem = 0;
  for (int i = 1; i <= n; i++) {
    while (a[i] == i) {
      swap(a[i], a[i + 1]);
      i = i + 1;
      dem++;
    }
  }
  cout << dem;
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <cfloat>
#include <climits>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <string>
#include <unordered_map>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<long long int> p(n);
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> p.at(i);
  }
  for (int i = 0; i < n; i++) {
    if (p.at(i) == i + 1) {
      swap(p.at(i), p.at(i + 1));
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <cfloat>
#include <climits>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <string>
#include <unordered_map>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<long long int> p(n);
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> p.at(i);
  }
  for (int i = 0; i < n; i++) {
    if (p.at(i) == i + 1) {
      if (i != n - 1) {
        swap(p.at(i), p.at(i + 1));
      }
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, a[10005], dem;
bool b[10005];
int main() {
  cin >> n;
  dem = 0;
  for (int i = 0; i <= n; i++)
    b[i] = false;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == i) {
      b[i] = true;
      dem++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (b[i] == true && b[i + 1] == true) {
      dem--;
      b[i] = false;
      b[i + 1] = false;
    }
  }
  cout << dem;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, a[100005], dem;
bool b[100005];
int main() {
  cin >> n;
  dem = 0;
  for (int i = 0; i <= n; i++)
    b[i] = false;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == i) {
      b[i] = true;
      dem++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (b[i] == true && b[i + 1] == true) {
      dem--;
      b[i] = false;
      b[i + 1] = false;
    }
  }
  cout << dem;
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
// #define _GRIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

#define int long long

signed main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; ++i)
    cin >> v[i];

  int ans = 0;
  for (int i = 0; i < n; ++i) {
    if (v[i] == i + 1) {
      int tmp = v[i];
      v[i] = v[i + 1];
      v[i + 1] = tmp;
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
// #define _GRIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

#define int long long

signed main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; ++i)
    cin >> v[i];

  int ans = 0;
  for (int i = 0; i < n; ++i) {
    if (v[i] == i + 1) {
      if (i != n - 1) {
        int tmp = v[i];
        v[i] = v[i + 1];
        v[i + 1] = tmp;
      } else {
        int tmp = v[i];
        v[i] = v[i - 1];
        v[i - 1] = tmp;
      }
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
  ll N;
  cin >> N;

  // 変えるべきもののindexを保管
  vector<ll> ng;
  vector<ll> p(N);
  rep(i, N) {
    cin >> p.at(i);
    if (p.at(i) == (i + 1)) {
      ng.push_back(i);
    }
  }
  ll i = 0;
  ll ans = 0;
  while (i < ng.size()) {
    // 連続しておかしい場合，一気に塗り替えられる
    if ((ng.at(i) + 1) == ng.at(i + 1)) {
      i += 2;
    } else {
      i += 1;
    }
    ans++;
  }
  cout << ans << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
  ll N;
  cin >> N;

  // 変えるべきもののindexを保管
  vector<ll> ng;
  vector<ll> p(N);
  rep(i, N) {
    cin >> p.at(i);
    if (p.at(i) == (i + 1)) {
      ng.push_back(i);
    }
  }
  ll i = 0;
  ll ans = 0;
  while (i < ng.size()) {
    // 連続しておかしい場合，一気に塗り替えられる
    if ((i + 1) < ng.size() && (ng.at(i) + 1) == ng.at(i + 1)) {
      i += 2;
    } else {
      i += 1;
    }
    ans++;
  }
  cout << ans << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define intt long long
#define PI 3.14159265358979323846

using namespace std;
int main() {
  intt N;
  std::cin >> N;
  std::vector<intt> P(N);
  for (auto &r : P) {
    std::cin >> r;
    --r;
  }
  intt ans = 0;
  for (intt i = 0; i < N - 1; ++i) {
    if (P[i] == i) {
      std::swap(P[i], P[i + 1]);
      ++ans;
    }
    if (P[N - 1] == N - 1)
      std::swap(P[i - 1], P[i]);
    ++ans;
  }
  std::cout << ans << std::endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define intt long long
#define PI 3.14159265358979323846

using namespace std;
int main() {
  intt N;
  std::cin >> N;
  std::vector<intt> P(N);
  for (auto &r : P) {
    std::cin >> r;
    --r;
  }
  intt ans = 0;
  for (intt i = 0; i < N - 1; ++i) {
    if (P[i] == i) {
      std::swap(P[i], P[i + 1]);
      ++ans;
    }
  }
  if (P[N - 1] == N - 1) {
    ++ans;
  }
  std::cout << ans << std::endl;

  return 0;
}
Problem p03612 (Python)

Buggy submission (status: Time Limit Exceeded):
def resolve():
    n = int(input())
    p = list(map(int, input().split()))
    ans = 0
    while any(i + 1 == p[i] for i in range(n)):
        for i in range(n - 1):
            if i + 1 == p[i]:
                p[i], p[i + 1] = p[i + 1], p[i]
                ans += 1
    print(ans)


if __name__ == "__main__":
    resolve()


Fixed submission:
def resolve():
    n = int(input())
    p = list(map(int, input().split()))
    ans = 0
    if p[0] == 1:
        p[0], p[1] = p[1], p[0]
        ans += 1
    if p[n - 1] == n:
        p[n - 2], p[n - 1] = p[n - 1], p[n - 2]
        ans += 1
    for i in range(n - 1):
        if p[i] == i + 1:
            p[i], p[i + 1] = p[i + 1], p[i]
            ans += 1
    print(ans)


if __name__ == "__main__":
    resolve()
Problem p03612 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
p = [0] + list(map(int, input().split()))
num = 0
for i in range(1, n + 1):
    if i == p[i]:
        num += 1
        p[i], p[i + 1] = p[i + 1], p[i]

print(num)


Fixed submission:
n = int(input())
p = [0] + list(map(int, input().split()))
num = 0
for i in range(1, n + 1):
    if i == n:
        if i == p[i]:
            num += 1
            break
    if i == p[i]:
        num += 1
        p[i], p[i + 1] = p[i + 1], p[i]

print(num)
Problem p03612 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
P = list(map(int, input().split()))

res = 0
for i in range(N):
    if P[i] == i + 1:
        res += 1
        if i < N:
            P[i], P[i + 1] = P[i + 1], P[i]

print(res)


Fixed submission:
N = int(input())
P = list(map(int, input().split()))

res = 0
for i in range(N):
    if P[i] == i + 1:
        res += 1
        if i < N - 1:
            P[i], P[i + 1] = P[i + 1], P[i]

print(res)
Problem p03612 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
p = list(map(int, input().split()))
ans = 0
for i in range(N):
    if p[i] == i + 1:
        ans += 1
        p[i], p[i + 1] = p[i + 1], p[i]
if p[-1] == N:
    ans += 1
print(ans)


Fixed submission:
N = int(input())
p = list(map(int, input().split()))
ans = 0
for i in range(N - 1):
    if p[i] == i + 1:
        ans += 1
        p[i], p[i + 1] = p[i + 1], p[i]
if p[-1] == N:
    ans += 1
print(ans)
Problem p03612 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int n;
int p[100000];

bool check() {
  for (int i = 0; i < n; i++)
    if (i + 1 == p[i])
      return false;
  return true;
}

int main() {
  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> p[i];

  int ans = 0, accum = 0;
  while (!check()) {
    for (int i = 0; i < n - 1; i++) {
      if (i + 1 == p[i]) {
        ans++;
        swap(p[i], p[i + 1]);
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int n;
int p[100000];

bool check() {
  for (int i = 0; i < n; i++)
    if (i + 1 == p[i])
      return false;
  return true;
}

int main() {
  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> p[i];

  int ans = 0, accum = 0;
  while (!check()) {
    for (int i = 0; i < n - 1; i++) {
      if (i + 1 == p[i]) {
        ans++;
        swap(p[i], p[i + 1]);
      }
    }
    if (n == p[n - 1]) {
      ans++;
      swap(p[n - 2], p[n - 1]);
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03612 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
plist = list(map(int, input().split()))
ans = 0
for i in range(N):
    if plist[i] == i + 1:
        plist[i], plist[i + 1] = plist[i + 1], plist[i]
        ans += 1
if plist[-1] == N:
    ans += 1
print(ans)


Fixed submission:
N = int(input())
plist = list(map(int, input().split()))
ans = 0
for i in range(N - 1):
    if plist[i] == i + 1:
        plist[i], plist[i + 1] = plist[i + 1], plist[i]
        ans += 1
if plist[-1] == N:
    ans += 1
print(ans)
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORR(i, a, b) for (ll i = (a); i <= (b); i++)
#define repR(i, n) for (ll i = n; i >= 0; i--)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define F first
#define S second
#define pb push_back
#define pu push
#define COUT(x) cout << (x) << endl
#define PQ priority_queue<ll>
#define PQR priority_queue<ll, vector<ll>, greater<ll>>
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define mp make_pair
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007LL;
const ll INF = 1LL << 60;
using vll = vector<ll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvll = vector<vll>;
using vstr = vector<string>;
using pll = pair<ll, ll>;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int main() {
  ll n;
  cin >> n;
  vll a(n);
  rep(i, n) cin >> a[i];
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] == i + 1) {
      ans++;
      swap(a[i], a[i + 1]);
    }
  }
  COUT(ans);
}


Fixed submission:
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORR(i, a, b) for (ll i = (a); i <= (b); i++)
#define repR(i, n) for (ll i = n; i >= 0; i--)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define F first
#define S second
#define pb push_back
#define pu push
#define COUT(x) cout << (x) << endl
#define PQ priority_queue<ll>
#define PQR priority_queue<ll, vector<ll>, greater<ll>>
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define mp make_pair
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007LL;
const ll INF = 1LL << 60;
using vll = vector<ll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvll = vector<vll>;
using vstr = vector<string>;
using pll = pair<ll, ll>;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int main() {
  ll n;
  cin >> n;
  vll a(n);
  rep(i, n) cin >> a[i];
  ll ans = 0;
  a.pb(INF);
  for (int i = 0; i < n; i++) {
    if (a[i] == i + 1) {
      ans++;
      swap(a[i], a[i + 1]);
    }
  }
  COUT(ans);
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, ans, i;
  cin >> n;
  int sw;
  vector<int> p(n);
  for (i = 0; i < n; ++i) {
    cin >> p[i];
  }
  ans = 0;
  for (i = 0; i < n; ++i) {
    if (p[i] == (i + 1)) {
      sw = p[i];
      p[i] = p[i + 1];
      p[i + i] = sw;
      ans++;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, ans, i;
  cin >> n;
  int sw;
  vector<int> p(n);
  for (i = 0; i < n; ++i) {
    cin >> p[i];
  }
  ans = 0;
  for (i = 0; i < n; ++i) {
    if (p[i] == (i + 1)) {
      sw = p[i];
      p[i] = p[i + 1];
      p[i + 1] = sw;
      ans++;
    }
  }
  cout << ans << endl;
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int N;
  cin >> N;
  int p[N];
  for (int i = 0; i < N; i++)
    cin >> p[i];
  // ここまで入力

  int ans = 0;
  for (int i = 0; i < N - 1; i++)
    // 左から順に部分的に完全順列を作っていく
    if (p[i] == i + 1) {
      swap(p[i], p[i + 1]);
      ans++;
    }
  // 最後が完全順列でなければ先頭と入れ替えればよい
  if (p[N - 1] == N) {
    swap(p[N - 1], p[0]);
    ans++;
  }

  cout << ans << endl;

  return 90;
}


Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int N;
  cin >> N;
  int p[N];
  for (int i = 0; i < N; i++)
    cin >> p[i];
  // ここまで入力

  int ans = 0;
  for (int i = 0; i < N - 1; i++)
    // 左から順に部分的に完全順列を作っていく
    if (p[i] == i + 1) {
      swap(p[i], p[i + 1]);
      ans++;
    }
  // 最後が完全順列でなければ先頭と入れ替えればよい
  if (p[N - 1] == N) {
    swap(p[N - 1], p[0]);
    ans++;
  }

  cout << ans << endl;

  return 0;
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;

  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    A.at(i)--;
  }

  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (A.at(i) == i) {
      swap(A.at(i), A.at(i + 1));
      cnt++;
    }
  }

  cout << cnt << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;

  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    A.at(i)--;
  }

  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (A.at(i) == i) {
      if (i != N - 1)
        swap(A.at(i), A.at(i + 1));
      cnt++;
    }
  }

  cout << cnt << endl;
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  vector<int> vec(n);
  for (int i = 0; i < n; i++) {
    int num;
    cin >> num;
    num--;
    vec[i] = num;
  }
  int cnt = 0;
  if (vec[n - 1] == n - 1) {
    int tmp = vec[n - 1];
    vec[n - 1] = vec[n - 2];
    vec[n - 2] = tmp;
    cnt++;
  }
  for (int i = 0; i < n - 1; i++) {
    if (vec[i] == i) {
      int tmp = vec[i];
      vec[i] = vec[i + 1];
      vec[i + 1] = tmp;
      cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> vec(n);
  for (int i = 0; i < n; i++) {
    int num;
    cin >> num;
    num--;
    vec[i] = num;
  }
  int cnt = 0;
  if (vec[n - 1] == n - 1) {
    int tmp = vec[n - 1];
    vec[n - 1] = vec[n - 2];
    vec[n - 2] = tmp;
    cnt++;
  }
  for (int i = 0; i < n - 1; i++) {
    if (vec[i] == i) {
      int tmp = vec[i];
      vec[i] = vec[i + 1];
      vec[i + 1] = tmp;
      cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int N;
char p[10100];

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    int p_in;
    cin >> p_in;
    if (p_in == i + 1)
      p[i] = 'x';
    else
      p[i] = 'o';
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (p[i] == 'x') {
      if (i != N - 1 && p[i + 1] == 'x') {
        p[i] = 'o';
        p[i + 1] = 'o';
        ans++;
      } else {
        p[i] = 'o';
        ans++;
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int N;
char p[100100];

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    int p_in;
    cin >> p_in;
    if (p_in == i + 1)
      p[i] = 'x';
    else
      p[i] = 'o';
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (p[i] == 'x') {
      if (i != N - 1 && p[i + 1] == 'x') {
        p[i] = 'o';
        p[i + 1] = 'o';
        ans++;
      } else {
        p[i] = 'o';
        ans++;
      }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define P pair<int, int>
#define PiP pair<int, pair<int, int>>
#define all(x) x.begin(), x.end()
using namespace std;

int main() {
  int n, ans = 0;
  cin >> n;
  vector<int> p(n), cnt(n);
  rep(i, n) {
    cin >> p.at(i);
    if (i + 1 == p.at(i))
      cnt.at(i) = 1;
    else
      cnt.at(i) = 0;
  }
  rep(i, n - 1) {
    if (cnt.at(i) == 1 && cnt.at(i + 1) == 1) {
      ans++;
      cnt.at(i) = 0;
      cnt.at(i + 1) = 0;
    } else if (cnt.at(i) == 1) {
      ans++;
      cnt.at(i) = 0;
    }
  }
  if (cnt.at(n) == 1)
    ans++;
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define P pair<int, int>
#define PiP pair<int, pair<int, int>>
#define all(x) x.begin(), x.end()
using namespace std;

int main() {
  int n, ans = 0;
  cin >> n;
  vector<int> p(n), cnt(n);
  rep(i, n) {
    cin >> p.at(i);
    if (i + 1 == p.at(i))
      cnt.at(i) = 1;
    else
      cnt.at(i) = 0;
  }
  rep(i, n - 1) {
    if (cnt.at(i) == 1 && cnt.at(i + 1) == 1) {
      ans++;
      cnt.at(i) = 0;
      cnt.at(i + 1) = 0;
    } else if (cnt.at(i) == 1) {
      ans++;
      cnt.at(i) = 0;
    }
  }
  if (cnt.at(n - 1) == 1)
    ans++;
  cout << ans;
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
int main() {
  ifstream fin("input.txt");
  long long N, i = 0, p, cnt = 0;
  cin >> N;
  vector<long long> vec(N);
  while (i < N) {
    cin >> p;
    vec[i] = p;
    i++;
  }
  for (long long i = 1; i <= N; i++) {
    if (vec[i - 1] == i) {
      swap(vec[i], vec[i - 1]);
      cnt++;
    }
  }
  cout << cnt << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
int main() {
  ifstream fin("input.txt");
  long long N, i = 0, p, cnt = 0;
  cin >> N;
  vector<long long> vec(N);
  while (i < N) {
    cin >> p;
    vec[i] = p;
    i++;
  }
  for (long long i = 1; i <= N; i++) {
    if (vec[i - 1] == i) {
      if (i == N) {
        swap(vec[i - 1], vec[i - 2]);
        cnt++;
        continue;
      }
      swap(vec[i], vec[i - 1]);
      cnt++;
    }
  }
  cout << cnt << endl;
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int p[10000] = {0};
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
  }
  for (int i = 1; i <= n; i++) {
    if (p[i] == i) {
      swap(p[i], p[i + 1]);
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int p[100001] = {0};
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
  }
  for (int i = 1; i <= n; i++) {
    if (p[i] == i) {
      swap(p[i], p[i + 1]);
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03612 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define TOSTRING(x) #x
#define SZ(x) (int)(x).size()
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REPR(i, n) for (int i = (n)-1; i >= 0; i--)
#define ALL(s) (s).begin(), (s).end()
#define so(V) sort(ALL(V))
#define rev(V) reverse(ALL(V))
#define uni(v) v.erase(unique(ALL(v)), v.end());
#define PAU system("pause")

typedef long long unsigned int llu;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vb> vvb;
const double EPS = 1e-9;
const int MOD = 1e9 + 7;
const int INF = (1 << 28);
const double PI = acos(-1);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vi P(N);
  REP(i, N) cin >> P[i];
  int ans = 0;
  REP(i, N - 1) {
    if (i + 1 == P[i]) {
      swap(P[i], P[i + 1]);
      ans++;
    }
    REP(i, N) cerr << P[i];
    cerr << ":" << ans << endl;
  }
  if (P[N - 1] == N)
    ans++;
  cout << ans << endl;
  PAU;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define TOSTRING(x) #x
#define SZ(x) (int)(x).size()
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REPR(i, n) for (int i = (n)-1; i >= 0; i--)
#define ALL(s) (s).begin(), (s).end()
#define so(V) sort(ALL(V))
#define rev(V) reverse(ALL(V))
#define uni(v) v.erase(unique(ALL(v)), v.end());
#define PAU system("pause")

typedef long long unsigned int llu;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vb> vvb;
const double EPS = 1e-9;
const int MOD = 1e9 + 7;
const int INF = (1 << 28);
const double PI = acos(-1);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vi P(N);
  REP(i, N) cin >> P[i];
  int ans = 0;
  REP(i, N - 1) {
    if (i + 1 == P[i]) {
      swap(P[i], P[i + 1]);
      ans++;
    }
  }
  if (P[N - 1] == N)
    ans++;
  cout << ans << endl;
  PAU;
  return 0;
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long int i = 0; i < n; i++)
typedef long long int ll;

int main() {

  ll n;

  cin >> n;

  vector<ll> p(n);

  rep(i, n) { cin >> p[i]; }

  ll ans = 0;
  ll t;

  if (p[0] == 1) {
    t = p[0];
    p[0] = p[1];
    p[1] = t;
    ans++;
  }

  for (int i = 1; i < n; i++) {
    if (p[i] == i + 1) {
      if (p[i + 1] != i + 1) {
        t = p[i];
        p[i] = p[i + 1];
        p[i + 1] = t;
        ans++;
      }
    }
  }

  if (p[n - 1] == n) {
    t = p[n - 1];
    p[n - 1] = p[n - 2];
    p[n - 1] = t;
    ans++;
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long int i = 0; i < n; i++)
typedef long long int ll;

int main() {

  ll n;

  cin >> n;

  vector<ll> p(n);

  rep(i, n) { cin >> p[i]; }

  ll ans = 0;
  ll t;

  if (p[0] == 1) {
    t = p[0];
    p[0] = p[1];
    p[1] = t;
    ans++;
  }

  for (int i = 1; i < n - 1; i++) {
    if (p[i] == i + 1) {
      if (p[i + 1] != i + 1) {
        t = p[i];
        p[i] = p[i + 1];
        p[i + 1] = t;
        ans++;
      }
    }
  }

  if (p[n - 1] == n) {
    t = p[n - 1];
    p[n - 1] = p[n - 2];
    p[n - 1] = t;
    ans++;
  }

  cout << ans << endl;

  return 0;
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (auto &x : a) {
    cin >> x;
  }
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    if (a[i] == i + 1) {
      swap(a[i], a[i + 1]);
      ans++;
      if (a[i + 1] == i + 2) {
        i++;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (auto &x : a) {
    cin >> x;
  }
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    if (a[i] == i + 1) {
      if (i == n - 1) {
        swap(a[i], a[i - 1]);
        ans++;
        break;
      }
      swap(a[i], a[i + 1]);
      ans++;
      if (a[i + 1] == i + 2) {
        i++;
      }
    }
  }
  cout << ans << endl;
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll INF64 = 1LL << 60;
const int INF32 = 1 << 29;
const int MOD = 1000000007;

const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};

int main() {
#ifdef MYLOCAL
  ifstream in("input.txt");
  cin.rdbuf(in.rdbuf());
#endif

  int n;
  cin >> n;
  vector<int> p(n);
  vector<int> p2(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p2[i] = p[i];
  }

  int count1 = 0;
  if (p[0] == 1) {
    swap(p[0], p[1]);
    ++count1;
  }

  for (int i = 1; i < n; i++) {
    if (p[i] == i + 1) {
      swap(p[i], p[i + 1]);
      ++count1;
    }
  }

  int count2 = 0;
  if (p2[n - 1] == n) {
    swap(p2[n - 1], p2[n - 2]);
    ++count2;
  }

  for (int i = n - 2; i >= 0; --i) {
    if (p2[i] == i + 1) {
      swap(p2[i], p2[i - 1]);
      ++count2;
    }
  }

  int ans = min(count1, count2);

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll INF64 = 1LL << 60;
const int INF32 = 1 << 29;
const int MOD = 1000000007;

const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};

int main() {
#ifdef MYLOCAL
  ifstream in("input.txt");
  cin.rdbuf(in.rdbuf());
#endif

  int n;
  cin >> n;
  vector<int> p(n);
  vector<int> p2(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p2[i] = p[i];
  }

  int count1 = 0;
  if (p[0] == 1) {
    swap(p[0], p[1]);
    ++count1;
  }

  for (int i = 1; i < n; i++) {
    if (p[i] == i + 1) {
      swap(p[i], p[i + 1]);
      ++count1;
    }
  }

  int count2 = 0;
  if (p2[n - 1] == n) {
    swap(p2[n - 1], p2[n - 2]);
    ++count2;
  }

  for (int i = n - 2; i >= 0; --i) {
    if (p2[i] == i + 1) {
      swap(p2[i], p2[i + 1]);
      ++count2;
    }
  }

  int ans = min(count1, count2);

  cout << ans << endl;

  return 0;
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (i + 1 == a.at(i)) {
      if (i + 2 == a.at(i + 1)) {
        ans++;
        i++;
      } else
        ans++;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (i + 1 == a.at(i)) {
      if (i + 1 == n)
        ans++;
      else if (i + 2 == a.at(i + 1)) {
        ans++;
        i++;
      } else
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define pb push_back
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define INF (1 << 30)
#define MOD 998244353
using namespace std;

int main() {
  int n, p[10005], ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
  }
  for (int i = 1; i <= n - 1; i++) {
    if (p[i] == i) {
      ans++;
      swap(p[i], p[i + 1]);
    }
  }
  if (p[n] == n)
    ans++;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define pb push_back
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define INF (1 << 30)
#define MOD 998244353
using namespace std;

int main() {
  int n, p[100005], ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
  }
  for (int i = 1; i <= n - 1; i++) {
    if (p[i] == i) {
      ans++;
      swap(p[i], p[i + 1]);
    }
  }
  if (p[n] == n)
    ans++;
  cout << ans << endl;
  return 0;
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> P;
typedef vector<P> vpl;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int mod = 1e9 + 7;
const int MAX = 510000;

int main() {
  ll n;
  cin >> n;
  vl a(n);
  rep(i, n) cin >> a[i];
  ll ans = 0;
  rep(i, n) {
    if (a[i] == i + 1) {
      swap(a[i], a[i + 1]);
      ans++;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> P;
typedef vector<P> vpl;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int mod = 1e9 + 7;
const int MAX = 510000;

int main() {
  ll n;
  cin >> n;
  vl a(n);
  rep(i, n) cin >> a[i];
  ll ans = 0;
  rep(i, n) {
    if (a[i] == i + 1) {
      if (i != n - 1)
        swap(a[i], a[i + 1]);
      ans++;
    }
  }
  cout << ans << endl;
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int N, c = 0;
  cin >> N;
  vector<int> p(N);

  rep(i, N) cin >> p.at(i);

  rep(i, N) if (p.at(i) - 1 == i) {
    swap(p.at(i), p.at(i + 1));
    c++;
  }
  if (p.at(N - 1) == N) {
    swap(p.at(N - 1), p.at(N - 2));
    c++;
  }

  cout << c << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int N, c = 0;
  cin >> N;
  vector<int> p(N);

  rep(i, N) cin >> p.at(i);

  rep(i, N - 1) if (p.at(i) - 1 == i) {
    swap(p.at(i), p.at(i + 1));
    c++;
  }
  if (p.at(N - 1) == N) {
    swap(p.at(N - 1), p.at(N - 2));
    c++;
  }

  cout << c << endl;
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  ll n, p[10001], count = 0;
  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> p[i];
  }
  for (ll i = 0; i < n; i++) {
    if (i + 1 == p[i]) {
      swap(p[i], p[i + 1]);
      count++;
    }
  }
  cout << count << endl;
  getchar();
  getchar();
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  ll n, p[1000001], count = 0;
  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> p[i];
  }
  for (ll i = 0; i < n; i++) {
    if (i + 1 == p[i]) {
      swap(p[i], p[i + 1]);
      count++;
    }
  }
  cout << count << endl;
  getchar();
  getchar();
}
Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); i++)
using namespace std;
using P = pair<long long, long long>;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double pi = acos(-1);
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<ll> p(n);
  rep(i, n) cin >> p[i];
  int ans = 0;
  rep(i, n) {
    if ((i + 1) == p[i]) {
      swap(p[i], p[i + 1]);
      ans++;
      i++;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); i++)
using namespace std;
using P = pair<long long, long long>;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double pi = acos(-1);
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<ll> p(n);
  rep(i, n) cin >> p[i];
  int ans = 0;
  rep(i, n) {
    if ((i + 1) == p[i]) {
      ans++;
      i++;
    }
  }
  cout << ans << endl;
}
Problem p03613 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  int n;
  std::cin >> n;
  std::vector<int> v(1e5 + 1, 0);
  int a;
  for (int i = 0; i < n; i++) {
    std::cin >> a;
    v[a]++;
    v[a + 1]++;
    v[a - 1]++;
  }

  int ans = 0;
  for (int i = 0; i < 1e5 + 1; i++)
    ans = std::max(ans, v[i]);

  std::cout << ans << std::endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  int n;
  std::cin >> n;
  std::vector<int> v(1e5 + 1, 0);
  int a;
  for (int i = 0; i < n; i++) {
    std::cin >> a;
    v[a]++;
    v[a + 1]++;
    if (a != 0)
      v[a - 1]++;
  }

  int ans = 0;
  for (int i = 0; i < 1e5 + 1; i++)
    ans = std::max(ans, v[i]);

  std::cout << ans << std::endl;
}
Problem p03613 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;

/*
<url:https://arc082.contest.atcoder.jp/tasks/arc082_a>
問題文============================================================
 長さ N の整数列 a1,a2,…,aN が与えられます。

 各 1≤i≤N に対し、ai に 1 足すか、1
引くか、なにもしないかの三つの操作からどれか一つを選んで行います。

 この操作の後、ある整数 X を選んで、ai=X となる i の個数を数えます。

 うまく操作を行い、X を選ぶことで、この個数を最大化してください。
=================================================================

解説=============================================================


================================================================
*/
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  vector<ll> a(N);
  for (auto &in : a)
    cin >> in;
  const int MAX_V = 10 ^ 5;
  vector<ll> X(MAX_V + 10, 0);
  for (int i = 0; i < N; i++) {
    if (a[i] != 0)
      X[a[i] - 1]++;
    X[a[i]]++;
    X[a[i] + 1]++;
  }
  cout << *max_element(X.begin(), X.end()) << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;

/*
<url:https://arc082.contest.atcoder.jp/tasks/arc082_a>
問題文============================================================
 長さ N の整数列 a1,a2,…,aN が与えられます。

 各 1≤i≤N に対し、ai に 1 足すか、1
引くか、なにもしないかの三つの操作からどれか一つを選んで行います。

 この操作の後、ある整数 X を選んで、ai=X となる i の個数を数えます。

 うまく操作を行い、X を選ぶことで、この個数を最大化してください。
=================================================================

解説=============================================================


================================================================
*/
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  vector<ll> a(N);
  for (auto &in : a)
    cin >> in;
  const int MAX_V = 10e5;
  vector<ll> X(MAX_V + 10, 0);
  for (int i = 0; i < N; i++) {
    if (a[i] != 0)
      X[a[i] - 1]++;
    X[a[i]]++;
    X[a[i] + 1]++;
  }
  cout << *max_element(X.begin(), X.end()) << endl;
  return 0;
}
Problem p03613 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define PI 4 * atan(1)
#define INF 1e8

typedef long long ll;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  vector<int> cnt(100000 + 2, 0);
  for (auto a : A) {
    cnt[a - 1]++;
    cnt[a]++;
    cnt[a + 1]++;
  }
  int ans = 0;
  for (int i = 0; i < cnt.size(); i++) {
    ans = max(ans, cnt[i]);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define PI 4 * atan(1)
#define INF 1e8

typedef long long ll;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  vector<int> cnt(100000 + 2, 0);
  for (auto a : A) {
    if (a - 1 >= 0)
      cnt[a - 1]++;
    cnt[a]++;
    cnt[a + 1]++;
  }
  int ans = 0;
  for (int i = 0; i < cnt.size(); i++) {
    ans = max(ans, cnt[i]);
  }
  cout << ans << endl;
}