qyliang/CodeNanoFix huggingface dataset:
Buggy code:
import sys

readline = sys.stdin.buffer.readline


def main():
    N = int(readline())
    P = list(map(int, readline().split()))
    P = list(map(lambda x: x-1, P))

    cnt = 0
    for i in range(1, N-1):
        if P[i] == i:
            if P[i-1] == i-1:
                P[i], P[i-1] = P[i-1], P[i]
                cnt += 1
            elif P[i+1] == i+1:
                P[i], P[i+1] = P[i+1], P[i]
                cnt += 1
            else:
                P[i], P[i+1] = P[i+1], P[i]
                cnt += 1

    if P[-1] == N-1:
        print(cnt+1)
    else:
        print(cnt)


main()


Fixed code:
import sys

readline = sys.stdin.buffer.readline


def main():
    N = int(readline())
    P = list(map(int, readline().split()))
    P = list(map(lambda x: x-1, P))

    cnt = 0
    for i in range(0, N):
        if i == N-1:
            if P[i] == i:
                P[i], P[i-1] = P[i-1], P[i]
                cnt += 1
        elif P[i] == i:
            if P[i+1] == i+1 and i != N-1:
                P[i], P[i+1] = P[i+1], P[i]
                cnt += 1
            elif i != 0:
                P[i], P[i-1] = P[i-1], P[i]
                cnt += 1
            else:
                P[i], P[i+1] = P[i+1], P[i]
                cnt += 1

    print(cnt)


main()
Buggy code:
N=int(input())
P=input()
P=P.rstrip().split()
P.append('0')

ans=0
l=0
for i in range(N+1):
    if P[l]==str(l+1):
        ans+=1
        l+=2
        if l>=N-1:
            break
print(ans)

Fixed code:
N=int(input())
P=input()
P=P.rstrip().split()
P.append('0')

ans=0
l=0
for i in range(N+1):
    
    if l>N-1:
            break
    elif P[l]==str(l+1):
        ans+=1
        l+=2
    else:
        l+=1
        
print(ans)
Buggy code:
N=int(input())
*P,=map(int,input().split())

i=c=0
while i<L:
  if P[i]==i+1:
    c+=1
    i+=1
  i+=1
print(c)

Fixed code:
N=int(input())
*P,=map(int,input().split())

i=c=0
while i<N:
  if P[i]==i+1:
    c+=1
    i+=1
  i+=1
print(c)
Buggy code:
n = int(input())
p = list(map(int,input().split()))
ans = 0
i = 0
while i < n-1:
	if p[i] == i+1 and p[i+1] == i+2:
		i += 2
		ans += 1
	elif p[i] == i+1 and p[i+1] != i+2:
		i += 1
		ans += 1
	else:
		i += 1

if p[-1] == n:
	ans += 1

print(ans)

Fixed code:
n = int(input())
p = list(map(int,input().split()))
ans = 0
i = 0

p.append(0)

while i <= n-1:
	if p[i] == i+1 and p[i+1] == i+2:
		i += 2
		ans += 1
	elif p[i] == i+1 and p[i+1] != i+2:
		i += 1
		ans += 1
	else:
		i += 1

print(ans)
Buggy code:
def main():
    N = int(input())
    P = [int(_) for _ in input().split()]

    i = 1
    output = 0
    while i <= N:
        if i == P[i-1]:
            output += 1
            if i < N-1 and i+1 == P[i]:
                i += 2
                continue
        i += 1
    print(output)
    return

if __name__ == '__main__':
    main()


Fixed code:
def main():
    N = int(input())
    P = [int(_) for _ in input().split()]

    i = 1
    output = 0
    while i <= N:
        if i == P[i-1]:
            output += 1
            if i < N and i+1 == P[i]:
                i += 2
                continue
        i += 1
    print(output)
    return

if __name__ == '__main__':
    main()
Buggy code:
def swap(a, b):
    tmp = a
    a = b
    b = tmp
    return a, b


def solve(): # 072D- Derangement
    N = int(input())
    p = list(map(int, input().split(" ")))
    cnt = 0

    for i in range(1, N):
        if i == p[i-1]:
            p[i], p[i+1] = swap(p[i], p[i+1])
            cnt += 1
        elif i == N:
            p[i-1], p[i] = swap(p[i-1], p[i])
            cnt += 1
        else:
            pass

    print(cnt + 1)
    

solve()

Fixed code:
def swap(a, b):
    tmp = a
    a = b
    b = tmp
    return a, b


def solve(): # 072D- Derangement
    N = int(input())
    p = list(map(int, input().split(" ")))
    p.insert(0, 0)
    cnt = 0

    for i in range(1, N+1):
        if i == p[i] and i != N:
            p[i], p[i+1] = swap(p[i], p[i+1])
            cnt += 1
        elif i == N and i == p[i]:
            p[i-1], p[i] = swap(p[i-1], p[i])
            cnt += 1
        else:
            pass

    print(cnt)
    
    
solve()
Buggy code:
N=int(input())
p=list(map(int,input().split()))
q=['x' for _ in range(N)]
ans=0
for i in range(N):
    if p[i]!=i+1:
        q[i]='o'
for i in range(N-1):
    if q[i]=='x':
        if i<n-1:
            p[i], p[i+1]=p[i+1], p[i]
        else:
            p[i], p[i-a1]=p[i-1], p[i]
        q[i]='o'
        q[i+1]='o'
        ans+=1

print(ans)


Fixed code:
N=int(input())
p=list(map(int,input().split()))
q=['x' for _ in range(N)]
ans=0
for i in range(N):
    if p[i]!=i+1:
        q[i]='o'
for i in range(N):
    if q[i]=='x':
        if i<N-1:
            p[i], p[i+1]=p[i+1], p[i]
            q[i+1]='o'
        else:
            p[i], p[i-1]=p[i-1], p[i]
            q[i-1]='o'
        q[i]='o'
        ans+=1

print(ans)
Buggy code:
N = int(input())
pl = list(map(int, input().split()))

cnt = 0
if pl[N-1] == N:
    cnt += 1
for i in range(1,N):
    if pl[i-1] == i:
        a = pl[i - 1]
        pl[i - 1] = pl[i]
        pl[i] = a
        cnt += 1
print(cnt)


Fixed code:
N = int(input())
pl = list(map(int, input().split()))

cnt = 0
for i in range(1,N):
    if pl[i-1] == i:
        a = pl[i - 1]
        pl[i - 1] = pl[i]
        pl[i] = a
        cnt += 1
if pl[N-1] == N:
    cnt += 1
print(cnt)
Buggy code:
n = int(input())
p = list(map(int, input().split()))
ans = 0
for i in range(n-1):
    if p[i] == i+1:
        print(p)
        p[i], p[i+1] = p[i+1], p[i]
        ans += 1
if p[-1] == n:
    ans += 1
print(ans)

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

for i in range(n):
    if i == p[i]:
        ans += 1

print(ans)

Fixed code:
n = int(input())
p = list(map(int, input().split()))  

ans = 0
for i in range(n):
    if p[i] == i+1:
        ans += 1
        if i != n-1:
            p[i+1] = 0


print(ans)
Buggy code:
n = int(input())
p = list(map(int, input().split()))
ans = 0

for i in range(n-1):
    if i+1 == p[i]:
        p[i], p[i+1] = p[i+1], p[i]
        ans += 1

if p[n-1] == n-1:
    ans += 1

print(ans)
        

Fixed code:
N = int(input())
p = list(map(int,input().split()))
ans = 0
for i in range(N-1):
  if p[i] == i+1:
    p[i],p[i+1] = p[i+1],p[i]
    ans += 1
if p[-1] == N:
  ans += 1
print(ans)
Buggy code:
import math
n,l =  input() , [ q-p for p,q in enumerate([ i for i,j in enumerate( map( int , input().split())) if i+1 == j ] ) ]

dict = {}
for i in l:
  if i in dict:
    dict[i] += 1
  else:
    dict[i] = 1


print( sum( [ math.ceil(i/2) for i in dict ] ) )

Fixed code:
import math
n,l =  input() , [ q-p for p,q in enumerate([ i for i,j in enumerate( map( int , input().split())) if i+1 == j ] ) ]

dict = {}
for i in l:
  if i in dict:
    dict[i] += 1
  else:
    dict[i] = 1


print( sum( [ math.ceil(dict[i]/2) for i in dict ] ) )
Buggy code:
n = int(input())
*l, = map(int,input().split())
ans = 0
for i in range(n-1):
  if l[i] == i+1:
    l[i+1] = l[i]
    ans += 1
if l[-1] = n:
  ans += 1
print(ans)

Fixed code:
n = int(input())
*l, = map(int,input().split())
ans = 0
for i in range(n-1):
  if l[i] == i+1:
    l[i+1] = l[i]
    ans += 1
if l[-1] == n:
  ans += 1
print(ans)
Buggy code:
N = int(input())
P = list(map(int, input().split()))

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

Fixed code:
N = int(input())
p =list(map(int, input().split()))
ans = 0
for i in range(N - 1):
  if i + 1 == p[i]:
    p[i], p[i+1] = p[i+1], [i]
    ans += 1
print(ans + int(N == p[N-1]))
Buggy code:
N=int(input())
k=0
s=0
m=0
L=list(map(int,input().split()))
for i in range(N):
  if L[i]==i+1:
    m+=1
  else:
    s+=m//2+m%2
    m=0
print(s)

Fixed code:
N=int(input())
k=0
s=0
m=0
L=list(map(int,input().split()))
for i in range(N):
  if L[i]==i+1:
    m+=1
  else:
    s+=m//2+m%2
    m=0
s+=m//2+m%2
print(s)
Buggy code:
N = int(input())
P = list(map(int, input().split()))
ans = 0
for i in range(N - 1):
    a, b = P[i], P[i + 1]
    if a == i:
        ans += 1
        P[i] = b
        P[i + 1] = a
print(ans)


Fixed code:
N = int(input())
P = list(map(int, input().split()))
ans = 0
for i in range(N):
    if i == N-1:
        if P[i] == i+1:
            ans +=1
        break
    a, b = P[i], P[i + 1]
    if a == i + 1:
        ans += 1
        P[i] = b
        P[i + 1] = a
print(ans)
Buggy code:
N = int(input())
P = list(map(int, input().split()))
ans = 0
for i in range(N):
    if P[i] == i + 1:
        ans += 1
print(max(ans - 1, 0))


Fixed code:
N = int(input())
P = list(map(int, input().split()))
ans = 0
for i in range(N-1):
    if i + 1 == P[i]:
        ans += 1
        P[i+1] = P[i]
if N == P[-1]:
    ans += 1
print(ans)
Buggy code:
N=int(input())
A=list(map(int, input().split()))

pre=(A[0]==1)
count=0
ans=0
if pre:
  count+=1
for i in range(1,N):
  if pre:
    if A[i]!=i+1:
      ans+=ceil(count/2)
      count=0
      pre=False
    else:
      count+=1
  else:
    if A[i]==i+1:
      count+=1
      pre=True
if pre:
  ans+=ceil(count/2)
print(ans)

Fixed code:
from math import ceil
N=int(input())
A=list(map(int, input().split()))

pre=(A[0]==1)
count=0
ans=0
if pre:
  count+=1
for i in range(1,N):
  if pre:
    if A[i]!=i+1:
      ans+=ceil(count/2)
      count=0
      pre=False
    else:
      count+=1
  else:
    if A[i]==i+1:
      count+=1
      pre=True
if pre:
  ans+=ceil(count/2)
print(ans)
Buggy code:
n = int(input())
p = list(map(int, input().split()))
temp = []

for i in range(n):
    # print(i, p[i])
    if p[i] == i+1:
        temp.append('y')
        continue

    temp.append(p[i])

a = []

for i in range(1, len(temp)):
    if temp[i] == 'y' and temp[i-1] == 'y':
        a.append('x')
    else:
        a.append(temp[i])

# idx = 1
# while True:
#     if idx > len(temp) - 1:
#         break
#
#     if temp[idx] == 'y' and temp[idx-1] == 'y':
#         temp[idx-1] = 'x'
#         del temp[idx]
#         # del temp[idx-1]
#         # temp.append('x')
#
#     idx += 1


#print(a)
ans = a.count('x') + a.count('y')
print(ans)


'''
9
1 2 4 9 5 8 7 3 6
'''


Fixed code:
n = int(input())
p = list(map(int, input().split()))
temp = []

for i in range(n):
    # print(i, p[i])
    if p[i] == i+1:
        temp.append('y')
        continue

    temp.append(p[i])

a = []

idx = 1
while True:
    if idx > len(temp) - 1:
        break

    if temp[idx] == 'y' and temp[idx-1] == 'y':
        temp[idx-1] = 'x'
        temp[idx] = 'x'
        #del temp[idx]
        # del temp[idx-1]
        # temp.append('x')

    idx += 1


#print(a)
ans = temp.count('x')//2 + temp.count('y')
print(ans)


'''
9
1 2 4 9 5 8 7 3 6
'''
Buggy code:
N = int(input())
p = list(map(int,input().split()))
ans = 0
for i in range(N-1):
    if p[i] == i+1:
        if i < N-1:
            p[i+1],p[i] = p[i+1],p[i]
        ans += 1

print(ans) 

Fixed code:
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[N-1] == N:
    ans += 1
print(ans)
Buggy code:
n = int(input())
*p, = map(int, input().split())
ans = 0
for i in range(n-1, 0, -1):
    if p[i] == (i+1):
        ans += 1
        p[i], p[i-1] = p[i-1], p[i]
print(ans)


Fixed code:
n = int(input())
*p, = map(int, input().split())
p = p+[-1]
ans = 0
for i in range(n):
    if p[i] == (i+1):
        ans += 1
        p[i], p[i+1] = p[i+1], p[i]
print(ans)
Buggy code:
N = int(input())
A = list(map(int, input().split()))
cnt = 0
for i, a in enumerate(A, 1):
  if a == i:
    if i == len(A) + 1:
      x = a[i - 1]
      y = a[i]
      a[i - 1] = y
      a[i] = x    
      cnt += 1
    else:
      x = a[i - 2]
      y = a[i - 1]
      a[i - 2] = y
      a[i - 1] = x    
      cnt += 1
print(cnt)


Fixed code:
N = int(input())
A = list(map(int, input().split()))
cnt = 0
for i, a in enumerate(A, 1):
  if a == i:
    if i != len(A):
      x = A[i - 1]
      y = A[i]
      A[i - 1] = y
      A[i] = x    
      cnt += 1
    else:
      x = A[i - 2]
      y = A[i - 1]
      A[i - 2] = y
      A[i - 1] = x    
      cnt += 1
print(cnt)
Buggy code:
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpl()
res = 0
for i in range(n):
    if a[i] == i + 1:
        res += 1

print(res-1)

Fixed code:
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpl()
res = 0
for i in range(n):
    if a[i] == i + 1:
        res += 1
        if i != n-1:
            a[i],a[i+1] = a[i+1],a[i]
print(res)
Buggy code:
n = int(input())
ps = list(map(int, input().split()))

swap_count = 0
counter = 0
skip = False
for i in range(1, n+1):
  p = ps[counter]
  counter += 1
  if skip:
    skip = False
    continue
  if i != p:
    skip = True
    swap_count += 1
    
print(swap_count)

Fixed code:
n = int(input())
ps = list(map(int, input().split()))

swap_count = 0
counter = 0
skip = False
for i in range(1, n+1):
  p = ps[counter]
  counter += 1
  if skip:
    skip = False
    continue
  if i == p:
    skip = True
    swap_count += 1
    
print(swap_count)
Buggy code:
n=int(input())
p=list(map(int,input().split()))
p.append(0)
ansls=[0]
if p[0]==1:
  ansls[0]=1
for i in range(1,n):
  if p[i] ==i+1:
    if p[i-1] == i:
      ansls[-1]+=1
  else:
    ansls.append(1)
ans=0
for i in range(len(ansls)):
  ans+= (ansls[i]+1)//2
print(ans)

Fixed code:
n=int(input())
p=list(map(int,input().split()))
p.append(0)
ansls=[0]
if p[0]==1:
  ansls[0]=1
for i in range(1,n):
  if p[i] ==i+1:
    if p[i-1] == i:
      ansls[-1]+=1
    else:
      ansls.append(1)
ans=0
for i in range(len(ansls)):
  ans+= (ansls[i]+1)//2
print(ans)
Buggy code:
N=int(input())
P=list(map(int,input().split()))
Q=[]

for i in range(N):
  Q.append(1 if P[i]==i+1 else 0)
#print(Q)

cnt=0
ans=0
for i in range(N):
  if Q[i]==1:
    cnt+=1
  else:
    ans+=cnt%2+cnt//2
    cnt=0
print(ans)

Fixed code:
N=int(input())
P=list(map(int,input().split()))
Q=[]

for i in range(N):
  Q.append(1 if P[i]==i+1 else 0)
#print(Q)

cnt=0
ans=0
for i in range(N):
  if Q[i]==1:
    cnt+=1
  else:
    ans+=cnt%2+cnt//2
    cnt=0
  if i==N-1:
    ans+=cnt%2+cnt//2  
print(ans)
Buggy code:
#!/usr/bin/env pypy3
import sys
input = sys.stdin.readline

def main():
    N = int(input())
    p = [0] + list(map(int, input().split()))
    ans = 0
    for i in range(1,N+1):
        if p[i] == i:
            ans += 1
            if i != 1 and p[i] != i-1 and not (i<N and p[i+1]==i+1):
                p[i], p[i-1] = p[i-1], p[i]
            else:
                p[i], p[i+1] = p[i+1], p[i]
        print(*p)
    print(ans)

if __name__ == '__main__':
    main()

Fixed code:
#!/usr/bin/env pypy3
import sys
input = sys.stdin.readline

def main():
    N = int(input())
    p = [0] + list(map(int, input().split()))
    ans = 0
    for i in range(1,N+1):
        if p[i] == i:
            ans += 1
            if i == 1 or p[i] == i-1 or (i < N and p[i+1] == i+1):
                p[i], p[i+1] = p[i+1], p[i]
            else:
                p[i], p[i-1] = p[i-1], p[i]
    print(ans)

if __name__ == '__main__':
    main()
Buggy code:
N = int(input())
p_list = list(map(int, input().split()))
count = 0
for i in range(N-1):
    if p_list[i] == i + 1:
        count += 1
        p_list[i+1] = p_list[i]
if p_list[N-1] == N -1:
    count += 1
print(count)



Fixed code:
N = int(input())
p_list = list(map(int, input().split()))

count = 0
for i in range(N-1):
    if p_list[i] == i + 1:
        count += 1
        p_list[i+1] = p_list[i]
if p_list[N-1] == N:
    count += 1
print(count)
Buggy code:
N = int(input())
nums = list(map(int, input().split()))
cnt = 0
ren = 0
for i in range(N):
  print(cnt, ren)
  if nums[i] == i+1:
    cnt += 1
    ren += 1
  elif ren > 1:
    cnt -= ren//2
    ren = 0
  else:
    ren = 0
if ren > 1:
  cnt -= ren//2
print(cnt)

Fixed code:
N = int(input())
nums = list(map(int, input().split()))
cnt = 0
ren = 0
for i in range(N):
  if nums[i] == i+1:
    cnt += 1
    ren += 1
  elif ren > 1:
    cnt -= ren//2
    ren = 0
  else:
    ren = 0
if ren > 1:
  cnt -= ren//2
print(cnt)
Buggy code:
N = int(input())
p = [int(x) for x in input().split(' ')]

num = 0
res = 0
for i in range(N):
    if p[i] == i + 1:
        num += 1
    else:
        res += (num + 1) // 2
        num = 0

print(res)


Fixed code:
N = int(input())
p = [int(x) for x in input().split(' ')]

num = 0
res = 0
for i in range(N):
    if p[i] == i + 1:
        num += 1
    else:
        res += (num + 1) // 2
        num = 0

res += (num + 1) // 2
print(res)
Buggy code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, atan2
from operator import mul
from functools import reduce
sys.setrecursionlimit(10 ** 9)

INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


n = I()
L = LI() + [-1]
ans = 0
cnt = 0
for i in range(n):
    if L[i] == i + 1:
        cnt += 1
    else:
        ans += (cnt + 1) // 2
        cnt = 0


print(ans)

Fixed code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, atan2
from operator import mul
from functools import reduce
sys.setrecursionlimit(10 ** 9)

INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


n = I()
L = LI() + [-1]
ans = 0
cnt = 0
for i in range(n + 1):
    if L[i] == i + 1:
        cnt += 1
    else:
        ans += (cnt + 1) // 2
        cnt = 0


print(ans)
Buggy code:
n = int(input())
num_list = list(map(int, input().split())) + [-1]


cnt = 0
ret = 0


for i in range(n):
    num = num_list[i]
    if num == i+1:
        cnt += 1
    else:
        if cnt:
            ret += (cnt+1) // 2
            cnt = 0


print(ret)




Fixed code:
n = int(input())
num_list = list(map(int, input().split())) + [-1]


cnt = 0
ret = 0


for i in range(n+1):
    num = num_list[i]
    if num == i+1:
        cnt += 1
    else:
        if cnt:
            ret += (cnt+1) // 2
            cnt = 0


print(ret)
Buggy code:
import itertools
def solve():
    N = int(input())
    p = list(map(int, input().split()))

    one_cnt = 0
    swap_cnt = 0
    for i in range(N):
        if p[i] == i + 1:
            one_cnt += 1
        else:
            if one_cnt % 2 == 0:
                swap_cnt += one_cnt / 2
            else:
                swap_cnt += one_cnt // 2 + 1
            one_cnt = 0
    
    print(int(swap_cnt))

if __name__ == '__main__':
    solve()


Fixed code:
import itertools
def solve():
    N = int(input())
    p = list(map(int, input().split()))

    one_cnt = 0
    swap_cnt = 0
    for i in range(N):
        if p[i] == i + 1:
            one_cnt += 1
        else:
            if one_cnt % 2 == 0:
                swap_cnt += one_cnt / 2
            else:
                swap_cnt += one_cnt // 2 + 1
            one_cnt = 0
    
    else:
        if one_cnt % 2 == 0:
            swap_cnt += one_cnt / 2
        else:
            swap_cnt += one_cnt // 2 + 1

    print(int(swap_cnt))

if __name__ == '__main__':
    solve()
Buggy code:
N = int(input())
p = list(map(int, input().split()))

ans = 0
flag = False
for i in range(N):
    if flag:
        n = p[i+1]
    else:
        n = p[i]
    if n == i+1:
        ans += 1
        flag = True
    else:
        flag = False
print(ans)

Fixed code:
n=int(input())
p=list(map(int,input().split()))
sousa=0
flag=True
for i in range(n):
  if i+1==p[i]:
    if flag:
      sousa+=1
      flag=False
    else:
      flag=True
  else:
    flag=True
print(sousa)
Buggy code:
def main():
    N = int(sys.stdin.readline())
    p = list(map(int, input().split()))
    count = 0
    for i, n in enumerate(p, 1):
        if i == n:
            p[i - 1], p[i] = p[i], p[i - 1]
            count += 1
    print(count)

if __name__ == '__main__':
    main()


Fixed code:
import sys

def main():
    N = int(sys.stdin.readline())
    p = list(map(int, input().split()))
    count = 0
    for i, n in enumerate(p, 1):
        if i == n:
            count += 1
            if len(p) != i:
                p[i - 1], p[i] = p[i], p[i - 1]
    print(count)

if __name__ == '__main__':
    main()
Buggy code:
n = int(input())
p = list(map(int,input().split()))
i = 1
res = 0
while i < n-1:
    if p[i-1] == i:
        res += 1
        i += 1
    i += 1
print(res)

Fixed code:
n = int(input())
p = list(map(int,input().split()))
i = 1
res = 0
while i < n+1:
    if p[i-1] == i:
        res += 1
        i += 1
    i += 1
print(res)
Buggy code:
import numpy as np
N = int(input())
p = np.array(list(map(int, input().split())))
v = p == np.arange(1, N + 1)
print('v', v)
ans = 0
for i in range(N):
    if i == N - 1 and v[i]:
        ans += 1
        continue
    if v[i]:
        v[i], v[i + 1] = False, False
        ans += 1
print(ans)


Fixed code:
import numpy as np
N = int(input())
p = np.array(list(map(int, input().split())))
v = p == np.arange(1, N + 1)
# print('v', v)
ans = 0
for i in range(N):
    if i == N - 1 and v[i]:
        ans += 1
        continue
    if v[i]:
        v[i], v[i + 1] = False, False
        ans += 1
print(ans)
Buggy code:
# -*- coding: utf-8 -*-
N = int(input())
p = list(map(int, input().split()))

ans = 0
for i in range(N):
    if p[i] == i+1:
        ans += 1

print(ans)


Fixed code:
# -*- coding: utf-8 -*-
N = int(input())
p = list(map(int, input().split()))

ans = 0
for i in range(N):
    if p[i] == i+1:
        ans += 1
        tmp = p[i]
        if i+1 < N:
            p[i] = p[i+1]
            p[i+1] = tmp
        else:
            p[i] = p[i-1]
            p[i-1] = tmp

print(ans)
Buggy code:
n = int(input())
p = list(map(int, input().split()))
k = 0
flag = False
for i in range(n):
  if p[i] == i+1:
    k += 1
    if flag:
      k -= 1
      flag = False
    else:
      flag = True

print(k)

Fixed code:
n = int(input())
p = list(map(int, input().split()))
k = 0
flag = False
for i in range(n):
  if p[i] == i+1:
    k += 1
    if flag:
      k -= 1
      flag = False
    else:
      flag = True
  else:
    flag = False

print(k)