TnT/Multi_CodeNet4Repair huggingface dataset:
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
n=int(input())
a=list(map(int,input()))
t=1
p=a[0]
m=0
for i in a:
  if(p==i):
    continue
  if(m>0):
    if(i<p):
      t+=1
      m=0
  elif(m<0):
    if(i>p):
      t+=1
      m=0
  else:
    if(i>p):
      m=1
    eif(i<p):
      m=-1
  p=i
print(t)

n=int(input())
a=list(map(int,input()))
t=1
p=a[0]
m=0
for i in a:
  if(p==i):
    continue
  if(m>0):
    if(i<p):
      t+=1
      m=0
  elif(m<0):
    if(i>p):
      t+=1
      m=0
  else:
    if(i>p):
      m=1
    elif(i<p):
      m=-1
  p=i
print(t)

n=int(input())
a=list(map(int,input().split()))
t=1
p=a[0]
m=0
for i in a:
  if(p==i):
    continue
  if(m>0):
    if(i<p):
      t+=1
      m=0
  elif(m<0):
    if(i>p):
      t+=1
      m=0
  else:
    if(i>p):
      m=1
    elif(i<p):
      m=-1
  p=i
print(t)

Verdicts: Runtime Error

Runtime Error

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
from collections import deque

n = int(input())
a = list(map(int,input().split()))
d = deque(a)

tmp = []
cnt = 0
while d:
  v = d.popleft()
  if len(tmp)<=1:
    pass
  else:
    if not v >= tmp[-1] >= tmp[-2] or v <= tmp[-1] <= tmp[-2]:
      tmp = []
      cnt += 1
  tmp.append(v)
#  print(d,tmp,cnt)
if tmp:
  cnt+=1
print(cnt)

n = int(input())
a = list(map(int,input().split()))

flg = "default" #increase_flg
cnt = 0
for i in range(n-1):
  if a[i+1] > a[i]:
    if not flg:
      cnt+=1
      flg = "default"
    else:
      flg = True
  elif a[i+1] < a[i]:
    if flg:
      cnt+=1
      flg = "default"
    else:
      flg = False
#  print(a[i],a[i+1],flg,cnt)

if not a[i]>=a[i-1]>=a[i-2] or a[i]<=a[i-1]<=a[i-2]:
  cnt +=1 
print(cnt)

from collections import deque

n = int(input())
a = list(map(int,input().split()))
d = deque(a)

tmp = []
cnt = 0
while d:
  v = d.popleft()
  tmp.append(v)
  if len(tmp)<=2:
    pass
  else:
    if not ( tmp[-1] >= tmp[-2] >= tmp[-3] or tmp[-1] <= tmp[-2] <= tmp[-3]):
      tmp = [tmp[-1]]
      cnt += 1
  
  print(d,tmp,cnt)
if tmp:
  cnt+=1
print(cnt)

from collections import deque
 
n = int(input())
a = list(map(int,input().split()))
d = deque(a)
 
tmp = []
cnt = 0
while d:
  v = d.popleft()
  if len(tmp)>=2:
    if not (v >= tmp[-1] >= tmp[0] or v <= tmp[-1] <= tmp[0]):
      tmp = []
      cnt += 1
  tmp.append(v)

print(cnt+1 if tmp else cnt)

Verdicts: Wrong Answer

Runtime Error

Runtime Error

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
import numpy as np
n=int(input())
a=list(map(int,input().split()))
b=a.copy()
b.insert(0,0)


arr=np.array(b)
diff=np.diff(arr)
arr2=np.array(a)
aa=arr2[np.nonzero(diff)]
print(diff,aa)

aaa=aa.tolist()
point=[]
cnt=1
print(aaa)
if len(aaa)<=2:
    print(1)
else:
    for i in range(len(aaa)-2):
        if (aaa[i+1]-aaa[i])*(aaa[i+2]-aaa[i+1])<0:
            cnt+=1
            point.append(i)
    
    if len(point)>1:
        for i in range(1,len(point)):
            if point[i]-1==point[i-1]:
                point[i]-=1
                cnt-=1
    print(cnt)

import numpy as np
n=int(input())
a=list(map(int,input().split()))
b=a.copy()
b.insert(0,0)


arr=np.array(b)
diff=np.diff(arr)
arr2=np.array(a)
aa=arr2[np.nonzero(diff)]

aaa=aa.tolist()
point=[]
cnt=1
print(aaa)
if len(aaa)<=2:
    print(1)
else:
    for i in range(len(aaa)-2):
        if (aaa[i+1]-aaa[i])*(aaa[i+2]-aaa[i+1])<0:
            cnt+=1
            point.append(i)
    
    if len(point)>1:
        for i in range(1,len(point)):
            if point[i]-1==point[i-1]:
                point[i]-=1
                cnt-=1
    print(cnt)

import numpy as np
n=int(input())
a=list(map(int,input().split()))


arr=np.array(a)
diff=np.diff(np.hstack((0,arr)))
aa=arr[np.nonzero(diff)]

aaa=aa.tolist()
point=[]
cnt=1
if len(aaa)<=2:
    print(1)
else:
    for i in range(len(aaa)-2):
        if (aaa[i+1]-aaa[i])*(aaa[i+2]-aaa[i+1])<0:
            cnt+=1
            point.append(i)
    
    if len(point)>1:
        for i in range(1,len(point)):
            if point[i]-1==point[i-1]:
                point[i]-=1
                cnt-=1
    print(cnt)

Verdicts: Wrong Answer

Wrong Answer

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
N = int(input())
P = list(map(int,input().split()))
cnt = 1
flag = 0 
for i in range(1,N+1):
    if P[i-1]!=P[i] and flag == 0:
        flag = 1
    elif flag == 1 and ((P[i-1] > P[i]) != (P[i-2]>P[i-1])):
        cnt += 1
        flag = 0
print(cnt)

N = int(input())
P = list(map(int,input().split()))
cnt = 1
flag = 0 
for i in range(1,N+1):
    if P[i-1]!=P[i] and flag == 0:
        flag = 1
    elif flag == 1 and ((P[i-1] > P[i]) != (P[i-2]>P[i-1])):
        cnt += 1
        flag = 0
print(cnt)



N = int(input())
P = list(map(int,input().split()))
cnt = 1
flag = 0 
for i in range(1,N+1):
    if P[i-1]!=P[i] and flag == 0:
        flag = 1
    elif flag == 1 and ((P[i-1] > P[i]) != (P[i-2]>P[i-1])):
        cnt += 1
        flag = 0
print(cnt)



N = int(input())
P = list(map(int,input().split()))
cnt = 1
flag = 0 
for i in range(1,N+1):
    if P[i-1]!=P[i] and flag == 0:
        flag = 1
    elif flag == 1 and ((P[i-1] > P[i]) != (P[i-2]>P[i-1])):
        cnt += 1
        flag = 0
print(cnt)

N = int(input())
P = list(map(int,input().split()))
cnt = 1
flag = 0 
for i in range(1,N+1):
    if P[i-1]!=P[i] and flag == 0:
        flag = 1
    elif flag == 1 and ((P[i-1] > P[i]) != (P[i-2]>P[i-1])):
        cnt += 1
        flag = 0
print(cnt)

N = int(input())
P = list(map(int,input().split()))
cnt = 1
flag = 0 
for i in range(1,N+1):
    if P[i-1]!=P[i] and flag == 0:
        flag = 1
    elif flag == 1 and ((P[i-1] > P[i]) != (P[i-2]>P[i-1])):
        cnt += 1
        flag = 0
print(cnt)

N = int(input())
P = list(map(int,input().split()))
cnt = 1
flag = 0 
for i in range(1,N):
    if P[i-1]>P[i] and flag == 0:
        flag = 1
    elif P[i-1]<P[i] and flag == 0:
        flag = 2
    elif flag == 1 and (P[i-1] < P[i]) :
        cnt += 1
        flag = 0
    elif flag == 2 and (P[i-1] > P[i]) :
        cnt += 1
        flag = 0
print(cnt)

Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
n = int(input())
A = list(map(int, input().split()))

ans = 1
sq = 0
a = A[0]
for i in range(1,n):
    print(a,A[i], sq)
    if sq == 0 and A[i] < a:
        sq = -1
    elif sq == 0 and A[i] > a:
        sq = 1
    elif (sq == 1 and A[i] < a) or (sq == -1 and A[i] > a):
        print('---')
        ans += 1
        sq = 0
    a = A[i]

print(ans)



n = int(input())
A = list(map(int, input().split()))

ans = 1
sq = 0
a = A[0]
for i in range(1,n):
    #print(a,A[i], sq)
    if sq == 0 and A[i] < a:
        sq = -1
    elif sq == 0 and A[i] > a:
        sq = 1
    elif (sq == 1 and A[i] < a) or (sq == -1 and A[i] > a):
        #print('---')
        ans += 1
        sq = 0
    a = A[i]

print(ans)



Verdicts: Wrong Answer

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
n,c,k=map(int,input().split())
lista=[int(input())for i in range(n)]
listb=sorted(lista)
count=0
bustime=0
buspeople=0
for i in listb:
    if buspeople==0 or bustime<i:
        count+=1
        buspeople=c-1
        bustime=i+k
    else :
        buspeople-=1
print(count)

n = int(input())
lista=list(map(int,input().split()))

count=0
zougen=""
for i in range(n-1):
    if zougen=="":
        if lista[i]<lista[i+1]:
            zougen="up"
        if lista[i]>lista[i+1]:
            zougen="down"
    elif  zougen=="up":
        if lista[i]>lista[i+1]:
            count+=1
            zougen=""
    elif zougen=="down":
        if lista[i]<lista[i+1]:
            count+=1
            zougen=""
print(count+1)

Verdicts: Runtime Error

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
li = lambda: list(map(int,input().split()))

n,l,t = li()
a,ac,au = [],[],[]
for i in range(n):
    x,y = li()
    if i == 0: start = y
    a.append(x)
    if y == 1:
        ac.append(x)
    else:
        au.append(x)

tt = t%l

if start == 2:
    acc = [n-ac[-s] for s in range(1,len(ac)+1)]
    dist = au[0]+acc[0]
    j = 0
    for i in range(1, min(len(au), len(acc))):
        if dist > tt:
            j = i-1
            break
        if i&1:
            dist += au[i]-au[i-1]
        else:
            dist += acc[i]-acc[i-1]
    if j&1:
        pointofone = n - acc[j//2]
    else:
        pointofone = au[j//2]
else:
    acc = [-au[0]] + [n-ac[-s] for s in range(1, len(ac))]
    dist = acc[0] + au[0]
    j = 0
    for i in range(1, min(len(ac), len(acc)) ):
        if dist > tt:
            j = i-1
            break
        if i&1:
            dist += acc[i]-acc[i-1]
        else:
            dist += au[i]-au[i-1]
    if j == 0:
        pointofone = ac[0]
    elif j&1:
        pointofone = au[j//2]
    else:
        pointofone = n - acc[j//2]

ant = sorted([(s-t)%l for s in au] + [(s+t)%l for s in ac])
st = ant.index(pointofone)
ant = ant[st:]+ant[:st]
print(*ant, sep='\n')

n = int(input())
a = list(map(int,input().split()))
mode = 0
ans = 1
for i in range(1,n):
    d = a[i]-a[i-1]
    if mode == 0:
        mode = d
        continue
    if mode>0:
        if d<0:
            ans += 1
    elif mode<0:
        if d>0:
            ans += 1
print(ans)

n = int(input())
a = list(map(int,input().split()))
mode = 0
ans = 1
for i in range(1,n):
    d = a[i]-a[i-1]
    if mode == 0:
        mode = d
        continue
    if mode>0:
        if d<0:
            ans += 1
            mode = 0
    elif mode<0:
        if d>0:
            ans += 1
            mode = 0
print(ans)

Verdicts: Runtime Error

Wrong Answer

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
n=int(input())
a=list(map(int,input().split()))
s=n
for i in range(n-1):
  if a[i]!=a[i+1]:
    s=i
    f=0 if a[s]<a[s+1] else f=1
    break
p=0
for i in range(s+1,n-1):
  if f==0:
    if a[i]>a[i+1]:
      p+=1
      f=1
  else:
    if a[i]<a[i+1]:
      p+=1
      f=0
print(p)

n=int(input())
a=list(map(int,input().split()))
s=n
for i in range(n-1):
  if a[i]!=a[i+1]:
    s=i
    if a[s]<a[s+1]:
      f=0
    else:
      f=1
    break
p=1
for i in range(s+1,n-1):
  if f==0:
    if a[i]>a[i+1]:
      p+=1
      if i==n-2 or a[i+1]>a[i+2]:
        f=1
      else:
        f=0
  else:
    if a[i]<a[i+1]:
      p+=1
      if i==n-2 or a[i+1]>a[i+2]:
        f=1
      else:
        f=0
print(p)

n=int(input())
a=list(map(int,input().split()))
a.append(0)
b=[]
for i in range(n):
  if a[i]!=a[i+1]:
    b.append(a[i])
n=len(b)
s=n
for i in range(n-1):
  if b[i]!=b[i+1]:
    s=i
    if b[s]<b[s+1]:
      f=0
    else:
      f=1
    break
p=1
for i in range(s+1,n-1):
  if f==0:
    if b[i]>b[i+1]:
      p+=1
      if i==n-2 or b[i+1]>b[i+2]:
        f=1
      else:
        f=0
  else:
    if b[i]<b[i+1]:
      p+=1
      if i==n-2 or b[i+1]>b[i+2]:
        f=1
      else:
        f=0
print(p)

Verdicts: Runtime Error

Wrong Answer

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
N = int(input())
A_lst = [int(x) for x in input().split()]
B_lst =[]
for i in range(N - 1):
    B_lst.append(A_lst[i + 1] - A_lst[i])

def my_sign(n):
    return (n > 0) - (n < 0)

cnt = 1
i = 0
pm = my_sign(B_lst[0])
while True:
    if i >= len(B_lst) - 1:
        break
    else:
        if my_sign(B_lst[i + 1]) == 0:
            i += 1
            continue
        elif pm == my_sign(B_lst[i + 1]) or pm == 0:
            pm = my_sign(B_lst[i + 1])
            i += 1
            continue
        else:
            cnt += 1
            i +=2
print(cnt)


def my_sign(n):
    return (n > 0) - (n < 0)
def kugiri(cnt, lst):
    if len(lst) <= 1:
        return cnt
    else:
        pm = lst[0]
        i = 1
        fl = 0
        while i < len(lst):
            if pm == lst[i]:
                i += 1
                continue
            else:
                fl = 1
                break
        if fl == 1:
            new_lst = []
            for j in range(i + 1, len(lst)):
                new_lst.append(lst[j])
            return kugiri(cnt + 1, new_lst)

N = int(input())
A_lst = [int(x) for x in input().split()]
cnt = 1
if N == 1:
    pass
else:
    S_lst = []
    for i in range(1, N):
        S_lst.append(my_sign(A_lst[i] - A_lst[i - 1]))
    for i in range(len(S_lst)):
        if i == 0 and S_lst[i] == 0:
            for j in range(len(S_lst)):
                if not S_lst[j] == 0:
                    S_lst[i] = S_lst[j]
        elif S_lst[i] == 0:
            S_lst[i] = S_lst[i - 1]
    cnt = kugiri(cnt, S_lst)
print(cnt)


def main():
    N = int(input())
    lst = [int(_) for _ in input().split()]
    last = lst[0]
    A = [last]
    for i in range(1, N):
        if lst[i] == last: continue
        last = lst[i]
        A.append(lst[i])
    left = right = output = 0
    while left < len(A):
        if left == len(A)-1:
            output += 1
            break
        if A[left+1] - A[left] > 0:
            while right < len(A)-1 and A[right] < A[right+1]:
                right += 1
        elif A[left+1] - A[left] < 0:
            while right < len(A)-1 and A[right] > A[right+1]:
                right += 1
        output += 1
        left = right + 1
        right = left
    print(output)
    return

if __name__ == '__main__':
    main()


Verdicts: Runtime Error

Wrong Answer

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
n = int(input())
a = [int(c) for c in input().split(' ')]
l = len(a)
c = 1
for i in range(l):
    if i == 0:
        if l == 1:
            pass
        else:
            prev = a[0]
            if a[1] > a[0]:
                inc = True
            else:
                inc = False
        continue
    current = a[i]
    if prev  < current and inc :
        pass
    elif prev > current and not inc:
        inc = True
        c += 1
    elif prev < current and not inc:
        pass
    elif prev > current and inc:
        inc = False
        c +=1
    prev = current
print(c)


n = int(input())
a = [int(c) for c in input().split(' ')]
prev = None
c = 1
inc = None

for i in range(l):
    current = a[i]
    if i == 0 :
        continue
    if inc == None:
        if a[i] > a[i-1] :
            inc = True
        elif             a[i] < a[i - 1]:
            inc = False
        else:
            continue 
    elif inc == True:
        if a[i-1] < a[i]:
            pass
        elif a[i -1] > a[i]:
            c += 1
            inc = None
    elif inc == False:
        if a[i-1] > a[i]:
            pass
        elif a[i - 1] < a[i]:
            c += 1
            inc = None
print(c)


n = int(input())
a = [int(c) for c in input().split(' ')]
#a = [1 ,2 ,1 ,2 ,1 ,2 ,1 ,2 ,1]
l = len(a)
prev = None
c = 1
inc = None

for i in range(l):
    current = a[i]
    if i == 0 :
        continue
    if inc == None:
        if a[i] > a[i-1] :
            inc = True
        elif             a[i] < a[i - 1]:
            inc = False
        else:
            continue 
    elif inc == True:
        if a[i-1] < a[i]:
            pass
        elif a[i -1] > a[i]:
            c += 1
            inc = None
    elif inc == False:
        if a[i-1] > a[i]:
            pass
        elif a[i - 1] < a[i]:
            c += 1
            inc = None
print(c)


Verdicts: Wrong Answer

Runtime Error

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
N = int(input())
A = list(map(int, input().split()))
count = 1

tmp = A[0]
bf = 1 if tmp < A[1] else 2 if tmp > A[1] else 0
af = 0  # {1: '+', 2: '-'}
for i in range(1, N):
    if tmp < A[i]:
        af = 1
    elif tmp > A[i]:
        af = 2

    tmp = A[i]
    if af != bf:
        count += 1
        if i+1 < N:
            af = 1 if tmp < A[i+1] else 2 if tmp > A[i+1] else 0
    bf = af

print(count)

N = int(input())
A = list(map(int, input().split()))
count = 1

conti_flg = 0  # {-1: '-', 0: '=', 1: '+'}
fst = A[0]
for i in A:
    snd = i
    if fst > snd:
        conti_flg = -1 if conti_flg <= 0 else 2
    elif fst < snd:
        conti_flg = 1 if conti_flg >= 0 else 2

    if conti_flg == 2:
        count += 1
        conti_flg = 0
    fst = snd

print(count)

Verdicts: Runtime Error

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
n = int(input())
A = list(map(int, input().split()))

ans = 0
up = dn = False
pre = A[0]
for a in A[1:]:
    if pre < a:
        up = True
    elif pre > a:
        dn = True
    if up and dn:
        ans += 1
        up = dn = False
    pre = a

print(ans)


n = int(input())
A = list(map(int, input().split()))

ans = 1
up = dn = False
pre = A[0]
for a in A[1:]:
    if pre < a:
        up = True
    elif pre > a:
        dn = True
    if up and dn:
        ans += 1
        up = dn = False
    pre = a

print(ans)


Verdicts: Wrong Answer

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
n = int(input())
a_ = list(map(int, input().split(' ')))
a = [-1]
i = 0
while  i < n:
    if a[-1] != a_[i]:
        a.append(a_[i])
    i += 1
a = a[1:]

ret = 1

i = 0
while i < n-2:
    if (a[i] > a[i+1] and a[i+1] < a[i+2]) or (a[i] < a[i+1] and a[i+1] > a[i+2]):
        ret += 1
        i += 2
    else:
        i += 1
        
print(ret)


n = int(input())
a_ = list(map(int, input().split(' ')))
a = [-1]
i = 0
while  i < n:
    if a[-1] != a_[i]:
        a.append(a_[i])
    i += 1
a = a[1:]
n = len(a)

ret = 1

i = 0
while i < n-2:
    if (a[i] > a[i+1] and a[i+1] < a[i+2]) or (a[i] < a[i+1] and a[i+1] > a[i+2]):
        ret += 1
        i += 2
    else:
        i += 1
        
print(ret)

Verdicts: Runtime Error

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
N=int(input())
A=[int(i) for i in input().split()]
T=[[A[0],A[1]]]
k=0
for i in range(2,N):
    if len(T[k])<=1:
        T[k].append(A[i])
        #continue
    if not((T[k][0]>=A[i-2]>=A[i-1]>=A[i]) or (T[k][0]<=A[i-2]<=A[i-1]<=A[i])):
        k+=1
        T.append([])
        T[k].append(A[i])
    else:
        T[k].append(A[i])
print(len(T))


N=int(input())
A=[int(i) for i in input().split()]
if N==1:
    print(1)
    exit()
T=[[A[0],A[1]]]
for i in range(2,N):
    if len(T[-1])<=1:
        T[-1].append(A[i])
        continue
    if not((T[-1][0]>=A[i-2]>=A[i-1]>=A[i]) or (T[-1][0]<=A[i-2]<=A[i-1]<=A[i])):
        T.append([A[i]])
    else:
        T[-1].append(A[i])
print(len(T))


Verdicts: Runtime Error

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
import numpy as np
N, L, T = map(int, input().split())
X = []
W = []
ant = [i for i in range(N)]
for i in range(N):
    xi, wi = map(int, input().split())
    X.append(xi)
    W.append(wi)

npX = np.array(X).astype(np.float)
npW = np.array(W).astype(np.float)
npW[npW == 2] = -1

def swap(ant, i, j):
    tmp = ant[i]
    ant[i] = ant[j]
    ant[j] = tmp
    return ant

ans = (npX + npW * T) % L

T %= L
if T > L - T:
    T = L - T
    npW *= -1
t = 0
while t < T:
    t += 0.5
    npX = (npX + npW * 0.5) % L
    colision = set([e for e in set(npX) if np.sum(npX == e) == 2])
    if len(colision) != 0:
        for e in colision:
            i, j = np.where(npX == e)[0].tolist()
            swap(ant, i, j)

for i in range(N):
    print(int(ans[ant.index(i)]))



N = int(input())
A = list(map(int, input().split()))

sign = 0
c = 0
for i in range(1,len(A)):
    dif = A[i] - A[i-1]
    if dif * sign < 0:
        c += 1
        sign = 0
        continue
    if dif != 0:
        sign = dif
print(c+1)


Verdicts: Runtime Error

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
N = int(input())
A = list(map(int,input().split()))

prev = A[0]
isUp = True
isDown = True

ans = 0
for a in A[1:]:
  if prev == a:
    pass
  elif prev < a:
    isDown = False
    if not isUp:
      isUp = True
      ans += 1
  elif a < prev:
    isUp = False
    if not isDown:
      isDown = True
      ans += 1
      
  prev = a
  
print(ans)

  
  

N = int(input())
A = list(map(int,input().split()))

prev = A[0]
isUp = True
isDown = True

ans = 0
for a in A[1:]:
  if prev == a:
    pass
  elif prev < a:
    isDown = False
    if not isUp:
      isUp = True
      isDown = True
      ans += 1
  elif a < prev:
    isUp = False
    if not isDown:
      isUp = True
      isDown = True
      ans += 1
      
  prev = a
  
print(ans+1)

  
  


Verdicts: Wrong Answer

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
N = int(input())
A = list(map(int, input().split()))

word = []
count = 0
start = 0
for x in range(N):
    if start==N:
        print("break")
        break
    for i in range(N,1,-1):
        word = A[start:i]
        if word == sorted(word, reverse=True):
            count += 1
            start = i
            print(word)
            print(start)
            break
        elif word== sorted(word, reverse=False):
            count += 1
            start = i
            print(word)
            print(start)
            break
            
print("{}".format(count))

N = int(input())
A = list(map(int, input().split()))

ans = 0
pos = 0
pos1 = 0
pos2 = 0

while True:
    if pos==N:
        break
    pos1 = pos
    pos2 = pos
    while pos1<N-1 and A[pos1] <= A[pos1+1]:
        pos1 +=1
#     print(pos1)
    while pos2<N-1 and A[pos2] >= A[pos2+1]:
        pos2 +=1
#     print(pos2)
    large = max(pos1, pos2)
    pos = large+1
    ans += 1
print(ans)

Verdicts: Wrong Answer

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
n=int(input())
a=[int(i) for i in input().split()]

ans=0
def plus_minus(i):
    while True:
        if a[i]==a[i+1]:
            i+=1
            continue
        if a[i]<a[i+1]:
            return 1
        if a[i]>a[i+1]:
            return -1

check=plus_minus(0)            
ans=1
for j in range(i,n-1):
    if check==1:
        if a[j] <= a[j+1]:
            continue
        else:
            ans+=1
            check=plus_minus(j+1)
    else:
        if a[j]>=a[j+1]:
            continue
        else:
            ans+=1
            check=plus_minus(j+1)
print(ans)

n=int(input())
a=[int(i) for i in input().split()]

ans=0
def plus_minus(i):
    while True:
        if a[i]==a[i+1]:
            i+=1
            continue
        if a[i]<a[i+1]:
            return 1
        if a[i]>a[i+1]:
            return -1

plus_minus(0)            
ans=1
for j in range(i,n-1):
    if check==1:
        if a[j] <= a[j+1]:
            continue
        else:
            ans+=1
            check=plus_minus(j+1)
    else:
        if a[j]>=a[j+1]:
            continue
        else:
            ans+=1
            check=plus_minus(j+1)
print(ans)

n=int(input())
a=[int(i) for i in input().split()]

ans=0
def plus_minus(i):
    while True:
        if i==n-1:
            break
        if a[i]==a[i+1]:
            i+=1
            continue
        if a[i]<a[i+1]:
            return 1
        if a[i]>a[i+1]:
            return -1

check=plus_minus(0)            
ans=1
for j in range(n-1):
    if check==1:
        if a[j] <= a[j+1]:
            continue
        else:
            ans+=1
            check=plus_minus(j+1)
    else:
        if a[j]>=a[j+1]:
            continue
        else:
            ans+=1
            check=plus_minus(j+1)
print(ans)

Verdicts: Runtime Error

Runtime Error

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
n = int(input())
array = list(map(int, input().split()))
isHead = True
isIncrease = False
count = 1
for i in range(1, n):
    if(isHead):
        isHead = False
        if(array[i] > array[i-1]):
            isIncrease = True
        elif(array[i] < array[i-1]):
            isIncrease = False
    else:
        if(isIncrease):
            if(array[i] < array[i-1]):
                count += 1
                isHead = True
                continue
        else:
            if(array[i] > array[i-1]):
                count += 1
                isHead = True
                continue
print(count)

n = int(input())
array = list(map(int, input().split()))
status = 0
past = 0
count = 1
for i in range(1, n):
    past = status
    if(array[i-1] < array[i]):
        if(status == 0):
            past = 1
        status = 1
    elif(array[i-1] > array[i]):
        if (status == 0):
            past = 2
        status = 2

    if(status != past):
        count += 1
        i += 2
        past = 0
        status = 0
        continue

print(count)

Verdicts: Wrong Answer

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
n = int(input())
line = list(map(int,input().split()))
count = 0
up = False
down =False
for i in range(n - 1):
  if line[i] < line[i + 1]:
    up = True
  elif line[i] > line[i + 1]:
    down = True
  if up == down == True:
    count += 1
    up =False
    down =False
print(count)

n = int(input())
line = list(map(int,input().split()))
count = 1
up = False
down =False
for i in range(n - 1):
  if line[i] < line[i + 1]:
    up = True
  elif line[i] > line[i + 1]:
    down = True
  if up == down == True:
    count += 1
    up =False
    down =False
print(count)

Verdicts: Wrong Answer

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
#n = int(input())
#n,k = map(int,input().split())
#x = list(map(int,input().split()))

N = int(input())
A = list(map(int,input().split()))

ans = 0
mm = 0
i = 0
while(i<N-2):
    if (A[i]-A[i+1])*(A[i+1]-A[i+2])<0:
        ans += 1
        i+=1
    i+=1
    
print(ans)




#n = int(input())
#n,k = map(int,input().split())
#x = list(map(int,input().split()))

N = int(input())
A = list(map(int,input().split()))

ans = 2
mm = 0
i = 0
while(i<N-2):
    if (A[i]-A[i+1])*(A[i+1]-A[i+2])<0:
        ans += 1
        i+=1
    i+=1
    
print(ans)




#n = int(input())
#n,k = map(int,input().split())
#x = list(map(int,input().split()))

N = int(input())
A = list(map(int,input().split()))

ans = 0
mm = 0
i = 0
while(i<N-2):
    if (A[i]-A[i+1])*(A[i+1]-A[i+2])<0:
        ans += 1
        i+=1
    i+=1
    
print(ans)




#n = int(input())
#n,k = map(int,input().split())
#x = list(map(int,input().split()))

N = int(input())
A = list(map(int,input().split()))

ans = 1
mm = 0
i = 0
flg = 0
while(i<N-1):
    if A[i]>A[i+1]:
        if mm>0:
            ans += 1
            flg = 1
            
            # print(i,A[i])
        mm = -1
        if flg:
            flg = 0
            mm = 0

    elif A[i]<A[i+1]:
        if mm<0:
            ans += 1
            flg = 1
            
            # print(i,A[i])

        mm = 1
        if flg:
            flg = 0
            mm = 0
    i+=1
    
print(ans)




Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
n=int(input())
a=list(map(int,input().split()))
ans=1
temp=0
cnt=0
for i in range(1,n-1):
  cnt=cnt+1
  if a[i-1]<a[i] and temp==0:
    temp=1
  elif a[i-1]>a[i] and temp==0:
    temp=2
  elif a[i-1]>a[i] and temp==1:
    temp=2
    if cnt>1:
      ans=ans+1
      cnt=0
  elif a[i-1]<a[i] and temp==2:
    temp=1
    if cnt>1:
      ans=ans+1
      cnt=0
if cnt==1:
  print(ans+1)
else:
  print(ans)

n=int(input())
a=list(map(int,input().split()))
ans=1
temp=0
for i in range(1,n):
  if a[i-1]<a[i] and temp==0:
    temp=1
  elif a[i-1]>a[i] and temp==0:
    temp=2
  elif a[i-1]>a[i] and temp==1:
    temp=0
    ans=ans+1
  elif a[i-1]<a[i] and temp==2:
    temp=0
    ans=ans+1
print(ans)

Verdicts: Wrong Answer

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
N = int(input())
A = list(map(int, input().split()))

count = 0
ans = 1
bef = 0
buff = True
for i in A:
    if count == 0:
        count = 1
    elif count == 1:
        if bef < i:
            count += 1
        elif bef > i:
            count += 1
            buff = False
        else:
            pass
    else:
        if buff:
            if bef <= i:
                pass
            else:
                ans += 1
                count = 1
        else:
            if bef >= i:
                pass
            else:
                ans += 1
                count = 1
    bef = i

N = int(input())
A = list(map(int, input().split()))

count = 0
ans = 1
bef = 0
buff = True
for i in A:
    if count == 0:
        count = 1
    elif count == 1:
        if bef < i:
            count += 1
            buff = True
        elif bef > i:
            count += 1
            buff = False
        else:
            pass
    else:
        if buff:
            if bef <= i:
                pass
            else:
                ans += 1
                count = 1
        else:
            if bef >= i:
                pass
            else:
                ans += 1
                count = 1
    bef = i
print(ans)


Verdicts: Wrong Answer

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
n = int(input())
lst = list(map(int,input().split()))

m = -1 
if n==1 or n==2:
    print(1)
else:
    cnt = 0
    num = lst[0]
    mode = 0
    for i in range(1,n):
        if mode==0:　
            if lst[i]==num:
                continue            
            elif lst[i] > num:
                mode = 1 
            elif lst[i] < num:
                mode = 2 
        elif mode == 1:
            if lst[i] < num:
                cnt += 1
                mode = 0
        elif mode == 2:
            if lst[i] > num:
                cnt += 1
                mode = 0
        num = lst[i]
    print(cnt + 1)

n = int(input())
lst = list(map(int,input().split()))
if n==1 or n==2:
    print(1)
else:
    cnt = 0
    num = lst[0]
    mode = 0
    for i in range(1,n):
        if mode==0:
            if lst[i]==num:
                continue            
            elif lst[i] > num:
                mode = 1
            elif lst[i] < num:
                mode = 2
        elif mode == 1:
            if lst[i] < num:
                cnt += 1
                mode = 0
        elif mode == 2:
            if lst[i] > num:
                cnt += 1
                mode = 0
        num = lst[i]
    print(cnt + 1)

Verdicts: Runtime Error

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
N = int(input())
A = list(map(int, input().split()))
def solve(N,A):
    liss = []
    lis = []
    lis.append(A[0])
    a = A[0]
    for i in range(1,N):
        if len(lis)==1 or prev==0:
            if A[i]>a:
                prev = 1
            elif A[i]<a:
                prev = -1
            else:
                prev = 0
        elif ((A[i]-a)*prev<0:
            liss.append(lis)
            lis = []
        lis.append(A[i])
        a = A[i]
    liss.append(lis)
    ans = len(liss)
    return ans
print(solve(N,A))

N = int(input())
A = list(map(int, input().split()))
def solve(N,A):
    ans = 0
    lis = []
    lis.append(A[0])
    a = A[0]
    for i in range(1,N):
        if len(lis)==1 or prev==0:
            prev = A[i]-a
        elif (A[i]-a)*prev<0:
            ans += 1
            lis = []
        lis.append(A[i])
        a = A[i]
    return ans
print(solve(N,A))

def solve():
  ans = 1
  N = int(input())
  A = list(map(int, input().split()))
  ud = 0
  for i in range(N-1):
    a = A[i+1]-A[i]
    if a*ud<0:
      ans += 1
      ud = 0
    elif ud==0:
      ud = a
  return ans
print(solve())

Verdicts: Runtime Error

Wrong Answer

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
n = int(input())
a = list(map(int, input().split()))
ans = 0
if n <= 2:
    print(1)
    exit()
pre = a[1] - a[0]
i = 2
while i < n:
    if pre * (a[i] - a[i-1]) < 0:
        ans += 1
        i += 1
        if i == n:
            break
    if a[i] != a[i-1]:
        pre = a[i] - a[i-1]
    i += 1
print(ans + 1)


n = int(input())
a = list(map(int, input().split()))
ans = 0
if n <= 2:
    print(1)
    exit()
pre = a[1] - a[0]
i = 2
while i < n:
    if pre * (a[i] - a[i-1]) < 0:
        ans += 1
        pre = 0
        i += 1
        if i == n:
            break
    if a[i] != a[i-1]:
        pre = a[i] - a[i-1]
    i += 1
print(ans + 1)


Verdicts: Wrong Answer

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
N=int(input())
A=[int(i) for i in input().split()]
B=[]
endflag=False
ans=1
if N>2:
	for i in range(N-1):
		if A[i]<A[i+1]:
			B.append(1)
		elif A[i]==A[i+1]:
			B.append(0)
		else:
			B.append(-1)
	count=0
	while B[count]==0:
		count+=1
		if count>N-1:
			endflag=True
			break
	print(A)
	print(B)
	if not endflag:
		mode=B[count]
		
		while count<N-1:
			if B[count]+mode==0:
				print(count,mode)
				ans+=1
				if count==N-2:
					break
				while B[count+1]==0:
					count+=1
					if count>N-1:
						endflag=True
						break
				mode=B[count+1]
			count+=1
			if endflag:
				break
print(ans)

N=int(input())
A=[int(i) for i in input().split()]
B=[]
endflag=False
ans=1
if N>2:
	for i in range(N-1):
		if A[i]<A[i+1]:
			B.append(1)
		elif A[i]==A[i+1]:
			B.append(0)
		else:
			B.append(-1)
	count=0
	while B[count]==0:
		count+=1
		if count>N-1:
			endflag=True
			break
	#print(A)
	
	if not endflag:
		mode=B[count]
		
		while count<N-1:
			if B[count]+mode==0:
				#print(count,mode)
				ans+=1
				if count==N-2:
					break
				while B[count+1]==0:
					count+=1
					if count>N-1:
						endflag=True
						break
				mode=B[count+1]
			count+=1
			if endflag:
				break
print(ans)

Verdicts: Wrong Answer

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
N = int(input())
A = input().split()

c = 1
i = 1
while i <= N-2:
	if int(A[i])-int(A[i-1]) > 0:
		if int(A[i+1])-int(A[i]) < 0:
			c += 1
			i += 2
		else:
			i += 1
	elif int(A[i])-int(A[i-1]) < 0:
		if int(A[i+1])-int(A[i]) > 0:
			c += 1
			i += 2
		else:
			i += 1
	else:
		c += 1
		i += 2

print(c)

N = int(input())
A = input().split()

c = 1
i = 1

while i <= N-1:
	if int(A[i])-int(A[i-1]) == 0:
		del A[i-1]
		N -= 1
	else:
		i += 1

i = 1

while i <= N-2:
	if int(A[i])-int(A[i-1]) > 0:
		if int(A[i+1])-int(A[i]) < 0:
			c += 1
			i += 2
		else:
			i += 1
	else:
		if int(A[i+1])-int(A[i]) > 0:
			c += 1
			i += 2
		else:
			i += 1
 
print(c)

Verdicts: Wrong Answer

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
import numpy as np
n = int(input())
a = [int(i) for i in input().split()]
answer= 1
if n!=1: np.sign(a[1]-a[0])
for i in range(1,n-1):
	num = np.sign(a[i+1]-a[i])
	if flag==0:
		flag = num
	elif num==-flag:
		answer += 1
		flag = 0
print(answer)

import numpy as np
n = int(input())
a = [int(i) for i in input().split()]
answer = 1
if n!=1: flag = np.sign(a[1]-a[0])
for i in range(1,n-1):
	num = np.sign(a[i+1]-a[i])
	if flag==0:
		flag = num
	elif num==-flag:
		answer += 1
		flag = 0
print(answer)

Verdicts: Runtime Error

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):


N = int(input())
A = list(map(int,input().split()))

count = 0
sign = 0
for i in range(N-1):
    if sign == 0:
        sign = A[i+1]-A[i]
    elif sign*(A[i+1]-A[i]) < 0:
        count += 1
        sign = 0
print(count)



N = int(input())
A = list(map(int,input().split()))

count = 0
sign = 0
for i in range(N):
    if sign == 0:
        sign = A[i+1]-A[i]
    elif sign*(A[i+1]-A[i]) < 0:
        count += 1
        sign = 0
print(count)



N = int(input())
A = list(map(int,input().split()))

count = 1
sign = 0
for i in range(N-1):
    if sign == 0:
        sign = A[i+1]-A[i]
    elif sign*(A[i+1]-A[i]) < 0:
        count += 1
        sign = 0
print(count)

Verdicts: Wrong Answer

Runtime Error

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
N = int(input())
A = list(map(int, input().split()))

li = []

for i in range(N-1):
    if A[i+1] - A[i] > 0:
       li.append('+')
    elif A[i+1] - A[i] < 0:
        li.append('-')
    else:
        pass
print(li)
ans = 1
p = 0
m = 0
for i in li:
    if i == '+':
        p = 1
    elif i == '-':
        m = 1
    if p == 1 & m == 1:
        ans += 1
        p = 0
        m = 0
print(ans)

N = int(input())
A = list(map(int, input().split()))

li = []

for i in range(N-1):
    if A[i+1] - A[i] > 0:
       li.append('+')
    elif A[i+1] - A[i] < 0:
        li.append('-')
    else:
        pass
# print(li)
ans = 1
p = 0
m = 0
for i in li:
    if i == '+':
        p = 1
    elif i == '-':
        m = 1
    if p == 1 & m == 1:
        ans += 1
        p = 0
        m = 0
print(ans)

Verdicts: Wrong Answer

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
N=int(input())
a=list(map(int,input().split()))
a.insert(0,0)
print(a,len(a))
cnt=1
val=0
l=len(a)-1
for n in range(1,l):
	if a[n-1]<=a[n]:
		val=1
		continue
	elif a[n-1]>a[n] and val==1:
		if n==l-2:
			cnt+=1
		val=0
		cnt+=1
	elif a[n-1]>=a[n]:
		val=2
		continue
	elif a[n-1]<a[n] and val==2:
		if n==l-2:
			cnt+=1
		val=0
		cnt+=1
print(cnt)

N=int(input())
a=list(map(int,input().split()))
cnt=1
val='0'
for n in range(1,N):
	if a[n-1]==a[n] or (val=='+' and a[n-1]<a[n]) or (val=='-' and a[n-1]>a[n]):
		pass
	elif val=='0':
		 if a[n-1]<a[n]:
		 	val='+'
		 elif a[n-1]>a[n]:
		 	val='-'
	else:
		val='0'
		cnt+=1
print(cnt)

Verdicts: Wrong Answer

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int,input().split()))
ans = 1
for i in range(n-1):
    if A[i] > A[i+1]:
        ans += 1
print(ans)

n = int(input())
a = list(map(int, input().split()))

ans = 1
status = 0
prev = a[0]

for i in a:
    diff = i - prev

    if diff == 0:
        pass
    elif diff > 0:
        if status >= 0:
            status = diff
        else:
            ans += 1
            status = 0
    else:
        if status <= 0:
            status = diff
        else:
            ans += 1
            status = 0

    prev = i

print(ans)

Verdicts: Wrong Answer

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
N = int(input())
A = list(map(int, input().split()))

ans = 1
up,down = False,False
for i in range(N-1):
  if A[i] < A[i+1]:
    up = True
  elif A[i] > A[i+1]:
    down = True
  if up and dn:
    ans +=1
    up,down = False,False
    
print(ans)

N = int(input())
A = list(map(int, input().split()))

ans = 1
up, dn = False, False
for i in range(N-1):
    if A[i] < A[i+1]:
        up = True
    elif A[i] > A[i+1]:
        dn = True
    if up and dn:
        ans += 1
        up, dn = False, False
print(ans)


Verdicts: Runtime Error

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
N = int(input())
A = list(map(int,input().split()))

ans = 1
i = 0

ls = [0]*(N-1)
for i in range(N-1):
  if A[i] > A[i+1]:
    ls[i] = 1
  elif A[i] < A[i+1]:
    ls[i] = -1
  else:
    if i == 0:
      ls[i] = 0
    else:
      ls[i] = ls[i-1]

print(ls)

j = 0
while j < N-2:
  if ls[j] == 1:
    if ls[j+1] == -1:
      ans += 1
      j += 1
  elif ls[j] == -1:
    if ls[j+1] == 1:
      ans += 1
      j += 1
  j += 1
print(ans)


N = int(input())
A = list(map(int,input().split()))
 
ans = 1
mode = 0
for i in range(N-1):
  if mode == 0:
    if A[i] < A[i+1]:
      mode = 1
    elif A[i] > A[i+1]:
      mode = -1
    else:
      mode = 0
  if mode == 1:
    if A[i] > A[i+1]:
      ans += 1
      mode = 0
  elif mode == -1:
    if A[i] < A[i+1]:
      ans += 1
      mode = 0
  else:
    pass
print(ans)

Verdicts: Wrong Answer

Accepted
Problem: You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?

Submission attempts (in order):
N = int(input())
src = map(int,input().split())
asc = 0
ans = 1
for i in range(N-1):
    d = src[i+1] - src[i]
    if asc * d < 0:
        ans += 1
        asc = 0
    else:
        asc = d
print(ans)

N = int(input())
A = list(map(int,input().split()))
up = dn = False
ans = 1
p = A[0]
for a in A:
    if p < a:
        up = True
    elif p > a:
        dn = True
    if up and dn:
        ans += 1
        up = dn = False
    p = a
print(ans)

Verdicts: Runtime Error

Accepted