Problem:


Score : 300 points

Problem Statement

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?

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Each A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible number of subarrays after division of A.

Sample Input 1

6
1 2 3 2 2 1

Sample Output 1

2

One optimal solution is to divide the array into [1,2,3] and [2,2,1].

Sample Input 2

9
1 2 1 2 1 2 1 2 1

Sample Output 2

5

Sample Input 3

7
1 2 3 2 1 999999999 1000000000

Sample Output 3

3



Sample input:
6
1 2 3 2 2 1


Sample output:
2

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 1; togU = 0; togD = 0
if a[1]>a[0]: togU = 1
elif a[1]<a[0]: togD = 1
for i in range(1,n):
  if not (togD or togU):
    if a[i]>a[i-1]: togU = 1; continue
    if a[i]<a[i-1]: togD = 1; continue
  if a[i]>a[i-1]:
    if togD:
      ans += 1
      togD = 0
      togU = 0
  elif a[i]<a[i-1]:
    if togU:
      ans += 1
      togU = 0
      togD = 0
print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
flag=0
answer=1
if A[0]<A[1]:
    flag=1
else:
    flag=-1
for n in range(N-1):
    if flag==1:
        if A[n]<=A[n+1]:
            pass
        else:
            flag=0
            answer+=1
    elif flag==-1:
        if A[n]>=A[n+1]:
            pass
        else:
            flag=0
            answer+=1
    else:
        if A[n]==A[n+1]:
            pass
        elif A[n]<A[n+1]:
            flag=1
        else:
            flag=-1
print(answer)

Submission status: Runtime Error

Solution code:
n=int(input())
lis=list(map(int,input().split()))
ans=1
bb=lis.pop(0)
b=lis.pop(0)
f=False
for i in lis:
  if f:
    f^=1
  elif (bb>b and b<i) or (bb<b and b>i):
    ans+=1
    f^=1
  bb=b
  b=i
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
lis=list(map(int,input().split()))
ans=1
bb=lis.pop(0)
b=lis.pop(0)
for i in lis:
  if (bb>b and b<i) or (bb<b and b>i):
    ans+=1
  bb=b
  b=i
print(ans)

Submission status: Runtime Error

Solution code:
def solve(): 

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

    i = 0
    result = 0

    while i < N:
        while i+1 < n and A[i] == A[i+1]:
            i += 1
        if i+1 < N and A[i] < A[i+1]:
            while i+1 < n and A[i] <= A[i+1]:
                i += 1
        elif i+1 < N and A[i] > A[i+1]:
            while i+1 < n and A[i] >= A[i+1]:
                i += 1
        
        result += 1
        i += 1

    print(result)    

if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
def solve(): 

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

    i = 0
    result = 0

    while i < n:
        while i+1 < n and A[i] == A[i+1]:
            i += 1
        if i+1 < n and A[i] < A[i+1]:
            while i+1 < n and A[i] <= A[i+1]:
                i += 1
        elif i+1 < n and A[i] > A[i+1]:
            while i+1 < n and A[i] >= A[i+1]:
                i += 1
        
        result += 1
        i += 1

    print(result)    

if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
div_flg=False
inc_flg=False
dec_flg=False
ans=0

for i in range(N):
  if inc_flg:
    if i==N-1:
      break
    if A[i]<=A[i+1]:
      continue
    else:
      inc_flg=False
      div_flg=False
  elif dec_flg:
    if i==N-1:
      break
    if A[i]>=A[i+1]:
      continue
    else:
      dec_flg=False
      div_flg=False
  else:
    if i==0:
      ans+=1
      div_flg=True
    elif i==N-1:
      if not div_flg:
        ans+=1
      break
    if A[i]==A[i+1]:
      if not div_flg:
        ans+=1
      div_flg=True
      continue
    elif A[i]<A[i+1]:
      if not div_flg:
        ans+=1
      inc_flg=True
      continue
    elif A[i]>A[i+1]:
      if not div_flg:
        ans+=1
      dec_flg=True
      continue
print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
div_flg=False
inc_flg=False
dec_flg=False
ans=0

for i in range(N):
  if inc_flg:
    if i==N-1:
      break
    if A[i]<=A[i+1]:
      continue
    else:
      inc_flg=False
      div_flg=False
  elif dec_flg:
    if i==N-1:
      break
    if A[i]>=A[i+1]:
      continue
    else:
      dec_flg=False
      div_flg=False
  else:
    if i==0:
      ans+=1
      div_flg=True
    elif i==N-1:
      ans+=1
      break
    if A[i]==A[i+1]:
      ans+=1
      div_flg=True
      continue
    elif A[i]<A[i+1]:
      if not div_flg:
        ans+=1
      inc_flg=True
      continue
    elif A[i]>A[i+1]:
      if not div_flg:
        ans+=1
      dec_flg=True
      continue
print(ans)

Submission status: Runtime Error

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

for i in range(N-2):
    if A[i]==A[i+1]:
        del A[i+1]

for i in range(len(A)-1):
    if A[i]<A[i+1]:
        A[i]="a"
    elif A[i]>A[i+1]:
        A[i]="b"
ans1=1
ans2=1
for i in range(len(A)-1):
    if A[i]=="a" and A[i+1]=="b":
        ans1=ans1+1
    elif A[i]=="b" and A[i+1]=="a":
        ans2=ans2+1
if ans1==ans2:
    ans1=ans1+1
print(max(ans1,ans2))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
B=[2]*(N)
if n<=2:
    print(1)
    exit(0)
 
for i in range(N-1):
    if A[i]>A[i+1]:
        B[i]=-1
    elif A[i]==A[i+1]:
        B[i]=0
    elif A[i]<A[i+1]:
        B[i]=1
i=0
cnt=[0]*N
while True:
    if B[i]==1:
        if B[i+1]==0 or B[i+1]==1:
            i+=1
        elif B[i+1]==-1:
            if cnt[i-1]==0 or i==0:
                cnt[i]=1
            i+=1
        else:
            i+=1
    if B[i]==0:
        i+=1
    if B[i]==-1:
        if B[i+1]==0 or B[i+1]==-1:
            i+=1
        elif B[i+1]==1:
            if cnt[i-1]==0 or i==0:
                cnt[i]=1
            i+=1
        else:
            i+=1
    if B[i]==2:
        break
print(cnt.count(1)+1)

Submission status: Runtime Error

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

turn=A[1]-A[0]
ans=1

if N>2:
    for i in range(1,N-1):
        judge=turn * (A[i+1]-A[i])

        if judge<0:
            ans+=1
            turn=0
        else:
            turn=A[i+1]-A[i]

print(ans)

Submission status: Runtime Error

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

turn=A[1]-A[0]
ans=1
for i in range(1,N-1):
    judge=turn * (A[i+1]-A[i])

    if judge<0:
        ans+=1
        turn=0
    else:
        turn=A[i+1]-A[i]

print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
counter=1

while len(A)>1:
    if A[0]=A[1]:
        A=A[1:]
    else:
        res=(A[1]>A[0])
        for i in range(len(A)-1):
            if (A[i+1]<A[i] and res) or (A[i+1]>A[i] and not res):
                A=A[i+1:]
                counter+=1
                break


print(counter)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
counter=1

while len(A)>1:
    res=(A[1]>=A[0])
    for i in range(len(A)):
        if (A[i+1]<A[i] and res) or (A[i+1]>A[i] and not res):
            A=A[i+1:]
            counter+=1
            break

print(counter)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
counter=1
res=(A[1]>=A[0])
if N==1:
    print(1)
else:
    for i in range(N-1):
        if A[i+1]<A[i] and res:
            res=False
            counter+=1
        elif A[i+1]>A[i] and not res:
            res=True
            counter+=1
    print(counter)

Submission status: Runtime Error

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

ans = 0
def main(i, A):
  global N
  global ans
  if A[i] == A[i+1]:
    if i == N-2:
      ans += 1
      return ans
    else:
      return main(i+1, A)
  elif A[i] < A[i+1]:
    while i < N-1:
      if A[i] <= A[i+1]:
        i += 1
      else:
        ans += 1
        if i == N-2:
          ans += 1
          return ans
        else:
          return main(i+1, A)
    else:
      ans += 1
      return ans
  else:
    while i < N-1:
      if A[i] >= A[i+1]:
        i += 1
      else:
        ans += 1
        if i == N-2:
          ans += 1
          return ans
        else:
          return main(i+1, A)
    else:
      ans += 1
      return ans
    
if N == 1:
  print(1)
else:
  print(main(0, A))

Submission status: Runtime Error

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

ans = 0
def main(i, A):
  global N
  global ans
  if A[i] == A[i+1]:
    if i == N-2:
      ans += 1
      return ans
    else:
      return main(i+1, A)
  elif A[i] < A[i+1]:
    while i < N-1:
      if A[i] <= A[i+1]:
        i += 1
      else:
        ans += 1
        if i == N-2:
          ans += 1
          return ans
        else:
          return main(i+1, A)
    else:
      ans += 1
      return ans
  else:
    while i < N-1:
      if A[i] >= A[i+1]:
        i += 1
      else:
        ans += 1
        if i == N-2:
          ans += 1
          return ans
        else:
          return main(i+1, A)
    else:
      ans += 1
      return ans
    
print(main(0, A))

Submission status: Runtime Error

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

ans = 0
def main(i, A):
  global N
  global ans
  if A[i] == A[i+1]:
    return main(i+1, A)
  elif A[i] < A[i+1]:
    while i < N-1:
      if A[i] <= A[i+1]:
        i += 1
      else:
        ans += 1
        if i == N-2:
          ans += 1
          return ans
        else:
          return main(i+1, A)
    else:
      ans += 1
      return ans
  else:
    while i < N-1:
      if A[i] >= A[i+1]:
        i += 1
      else:
        ans += 1
        if i == N-2:
          ans += 1
          return ans
        else:
          return main(i+1, A)
    else:
      ans += 1
      return ans
    
print(main(0, A))

Submission status: Runtime Error

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

f = 0
cnt = 0
if a[0] < a[1]:
    f = 1
elif a[0] > a[1]:
    f = 2
tmp = a[1]

for i in range(2,n):
    if tmp < a[i]:
        if f == 2:
            cnt += 1
            f = 0
        elif f == 0:
            f = 1
    elif tmp > a[i]:
        if f == 1:
            cnt += 1
            f = 0
        elif f == 0:
            f = 2
    tmp = a[i]
cnt += 1

print(cnt)

Submission status: Runtime Error

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

f = 0
cnt = 0
if a[0] < a[1]:
    f = 1
elif a[0] > a[1]:
    f = 2
tmp = a[1]

for i in range(2,n):
    if tmp < a[i]:
        if f == 2:
            cnt += 1
            f = 0
        elif f == 0:
            f = 1
    elif tmp > a[i]:
        if f == 1:
            cnt += 1
            f = 0
        elif f == 0:
            f = 2
    else:
        continue
    tmp = a[i]
cnt += 1

print(cnt)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 1
if A[0] < A[1]:
  status = "plus"

elif A[0] > A[1]:
  status = "minus"

else:
  status = "even"

for n in range(1, N):
  if status == "even":
    if A[n] == A[n-1]:
      pass
    elif A[n] > A[n-1]:
      status = "plus"
    else:
      status = "minus"
  elif status == "plus":
    if A[n] >= A[n-1]:
      pass
    else:
      status = "minus"
      ans += 1
  elif status == "minus":
    if A[n] <= A[n-1]:
      pass
    else:
      status = "plus"
      ans += 1


print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')


def solve():
    n = int(input())
    a = [int(x) for x in input().split()]
    b = []
    p = 0
    for e in a:
        if p != e:
            b.append(e)
        p = e

    a = b
    n = len(a)
    p = -1
    ans = 0
    i = 1
    while i < n:
        if a[i-1] > a[i]:
            x = 0
        else:
            x = 1
        if p != x:
            ans += x
        p = x
        i += 1

    print(ans + (not x))

t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""



"""

Submission status: Runtime Error

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

i = 1
ans = 1
plus = False
minus = False
while True:
    while A[i] - A[i-1] == 0:
        i += 1
    if A[i] - A[i - 1] > 0:
        plus = True
    else:
        minus = True
    if plus:
        while A[i] - A[i - 1] >= 0:
            i += 1
            if i == n:
                break
    else:
        while A[i] - A[i -1] <= 0:
            i += 1
            if i == n:
                break
    if i == n:
        break
    if plus:
        minus = True
        plus = False
    else:
        plus = True
        minus = False
    ans += 1
    if i != n -1:
        i += 1

print(ans)

Submission status: Runtime Error

Solution code:
n,*a=map(int,open(0).read().split())
b=0
if a[0]>a[1]:
    b=-1
elif a[0]==a[1]:
    b=0
else:b=1
c=[[a[0]]]
for i in range(1,n):
    if (a[i]-a[i-1])*b>0:
        c[-1]+=[a[i]]
    else:
        if len(c[-1])==1 or b==0:
            c[-1]+=[a[i]]
        else:c+=[[a[i]]]  
    if a[i-1]>a[i]:
        b=-1
    elif a[i-1]==a[i]:
        b=0
    else:b=1
print(len(c))

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
s=''
ans=1
for i in range(n-1):
  if a[i]<a[i+1]:
    s+='x'
  elif a[i]>a[i+1]:
    s+='y'
for i in range(len(s)):
  if s[i]!=s[i+1]:
    ans+=1
print(ans)

Submission status: Runtime Error

Solution code:
p = -1
xp = -1
ans = 0
input()
for i in range(input().split(" ")):
  i = int(i)
  if i == p: continue
  if p != -1:
    x = p < i
    if xp != -1
      ans += x != xp
    xp = x
  p = i

Submission status: Runtime Error

Solution code:
A=list(map(int, input().split()))
if N<=2:
    print(1)
    exit()

now=0
ans=1
for i in range(1,N):
    if i==1:
        if A[i]-A[i-1]==0:
            now=0
        else:
            now=(A[i]-A[i-1])//(abs(A[i]-A[i-1]))
        if now>=1:
            plus=True
        elif now==0:
            plus=-1
        else:
            plus=False
            
    else:
        if A[i]-A[i-1]==0:
            nex=0
        else:
            nex=(A[i]-A[i-1])//(abs(A[i]-A[i-1]))
        if plus==-1:
            if nex>=1:
                plus=True
            elif nex<0:
                plus=False
        elif plus:
            if nex<0:
                plus=-1
                ans+=1
        else:
            if nex>0:
                plus=-1
                ans+=1

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
if a[-1] >= a[-2]:
    a.append(float('inf'))
else:
    a.append(-float('inf'))
ans = 1
i = 0
while i < n-1:
    if a[i] < a[i+1] > a[i+2]:
        ans += 1
        i += 2
    elif a[i] > a[i+1] < a[i+2]:
        ans += 1
        i += 2
    else:
        i += 1
print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
L=[]
ans=1
for i in range(N-1):
  L.append(A[i+1]-A[i])
L.append(0)
#print(L)
L.remove(0)
#print(L)
M=len(L)
tmp=L[0]
for i in range(1,M):
  if tmp*L[i]<0:
    ans+=1
    tmp*=-1
    #print(tmp)
print(ans)

Submission status: Runtime Error

Solution code:
n,l,t = map(int,input().split())
x = []
y = []
for i in range(n):
    x.append(list(map(int,input().split())))
d = x[0][1]
b = x[0][0]
k = 2 * t
for i in range(1,n):
    if(x[i][1] == d):
        y.append(0)
    else:
        if d == 1:
            sa = (x[i][0] - b)%l
        else:
            sa = (b - x[i][0])%l
        
        if k >= sa:
            y.append(1+int((k-sa)/l))
        else:
            y.append(0)

for i in range(n):
    x[i][0] += t * pow(-1,x[i][1]+1)
    x[i][0] %= l

sy = sum(y)
sx = sorted(x,key=lambda x:x[0])
if d==2:
    sx = sx[-sy:] + sx[:-sy]
else:
    sx = sx[sy:] + sx[:sy]

for i in range(n):
    print(sx[i][0])

Submission status: Runtime Error

Solution code:
n = int(input())
l = list(map(int, input().split()))
x=[]
ans, count, q, xx = 0, 1, "<", 0
for i in range(n+1):
    if q=='<':
        if l[count]<=l[count-1]:
            count+=1
            continue
        else:
            if xx==0:
                q=">"
                xx+=1
                continue
            else:
                ans+=1
                q=">"
                continue
    if q=='>':
        if l[count]>=l[count-1]:
            count+=1
            continue
        else:
            ans+=1
            q="<"
            continue
print(ans+1)

Submission status: Runtime Error

Solution code:
def main():
    # N = int(input())
    # A = [int(a) for a in input().split()]

    f = open("subtask_1_02.txt", "r")
    N = int(f.readline())
    A = [int(a) for a in f.readline().split()]

    idx = 0
    ans = 0
    for i in range(len(A)):
        if idx + 1 > len(A) - 1 :
            break

        m = ""
        if A[idx] < A[idx + 1]:
            m = "+"
        elif A[idx] > A[idx + 1]:
            m = "-"
        else:
            find = False
            for j in range(idx, len(A)-1):
                if A[j] < A[j + 1]:
                    m = "+"
                    find = True
                    idx = j
                    break
                elif A[j] > A[j + 1]:
                    m = "-"
                    find = True
                    idx = j
                    break
            if find == False:
                print(ans + 1)
                exit()

        for j in range(idx, len(A) - 1):
            if m == "+":
                if A[j] <= A[j + 1]:
                    idx = j + 1
                else:
                    idx = j + 1
                    ans += 1
                    break
            elif m == "-":
                if A[j] >= A[j + 1]:
                    idx = j + 1
                else:
                    idx = j + 1
                    ans += 1
                    break

    print(ans + 1)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

j=0
for i in range(N+1):
  k=i
  print(A[j:k])
  if (A[j:k]==sorted(A[j:k]) or A[j:k]==sorted(A[j:k],reverse=True)):
    continue
  else:
    count+=1
    j=i-1
    
print(count)

Submission status: Runtime Error

Solution code:
#from statistics import median
import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
#from fractions import gcd
#from itertools import combinations # (string,3) 3回
#from collections import deque
#from collections import deque,defaultdict
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
S = readInts()
i = 0
cnt = 0
while i < n:
    while i+1 < n:
        if S[i] == S[i+1]:
            i += 1
        else:
            break
    if i + 1 < n:
        if S[i] < S[i+1]:
            while i + 1 < n:
                if S[i] =< S[i+1]:
                    i += 1
                else:
                    break
        else:
            while i + 1 < n:
                if S[i] >= S[i+1]:
                    i += 1
                else:
                    break
    cnt += 1
    i += 1
print(cnt)

Submission status: Runtime Error

Solution code:
n = int(input())
li = list(map(int,input().split()))
x = 0 #まだ未定なら0、単調増加なら1、単調減少なら-1にする
i = 0 #index
ans = 0

while True:
    if x == 0:
        if li[i] < li[i+1]:
            x = 1
        elif li[i] > li[i+1]:
            x = -1
        else:
            x = 0
    if x == 1:
        if li[i] > li[i+1]:
            ans += 1
            x = 0
    if x == -1:
        if li[i] < li[i+1]:
            ans += 1
            x = 0
    i += 1
    if i == n-1:
        break

print(ans+1)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
  #include "LOCAL_DEBUG.hpp"
#endif
#define int long long
const int INF = 1LL << 60;

signed main(){

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

  int ans = 0;
  for(int i = 0; i < n; i++){
    int right1 = i, right2 = i;
    while(right1+1 < n && a[right1] <= a[right1+1]) right1++;
    while(right2+1 < n && a[right2] >= a[right2+1]) right2++;
    i = max(right1, right2);
    ans++;
  }
  cout << ans << endl;

  return 0;
}

Submission status: Runtime Error

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    end = len(A)
    judge = 0
    ans = 0
    i = 0

    while True:
        if A[i] < A[i+1]:
            ans += 1
            while True:
                i += 1
                if i == end-1:
                    return ans
                elif A[i] <= A[i+1]:
                    continue
                else:
                    i += 1
                    if i == end-1:
                        ans += 1
                        return ans
                    else:
                        break

        elif A[i] > A[i+1]:
            ans += 1
            while True:
                i = i+1
                if i == end-1:
                    return ans
                elif A[i] >= A[i+1]:
                    continue
                else:
                    i += 1
                    if i == end-1:
                        ans += 1
                        return ans
                    else:
                        break

        elif A[i] == A[i+1]:
            i += 1
            if i == end-1:
                ans += 1
                return ans
            else:
                continue

print(main())

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
count = 1
skip = False
for i in range(0, N-1):
    if not skip:
        before = A[i+1] - A[i]
        after = A[i+2] - A[i+1]
        if ((before > 0) - (before < 0))*((after > 0) - (after < 0)) == -1:
            count += 1
            skip = True
    else:
        skip = False
print(count)

Submission status: Runtime Error

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

#連続する重複を除外
arr=np.array(a)
diff=np.diff(arr,prepend=0)
aa=arr[np.nonzero(diff)]
#print(diff,aa)

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)

Submission status: Runtime Error

Solution code:
N  = int(input())
A = list(map(int, input().split()))
status = 0 # 0 = defalt, 1 = up, 2 = down
cnt = 1
for i in range(1,N):
    if A[i-1] < A[i]: #up petern
        if status == 0 or status == 1:
            status = 1
        else:
            status = 0
            cnt += 1
    elif A[i-1] > A[i]:
        if status == 0 or status == 2
                status = 2
        else:
            status = 0
            cnt += 1
    else:
        continue
print(cnt)

Submission status: Runtime Error

Solution code:
N  = int(input())
A = list(map(int, input().split()))
status = 0 # 0 = defalt, 1 = up, 2 = down
cnt = 1
for i in range(1,N):
    if A[i-1] < A[i]: #up petern
        if status == 0 or status == 1:
            status = 1
        else status == 2:
            status = 0
            cnt += 1
    elif A[i-1] > A[i]:
        if status == 0 or status == 2
                status = 2
        else status:
            status = 0
            cnt += 1
    else:
        continue
print(cnt)

Submission status: Runtime Error

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

increase_flag=False
decrease_flag=False
answer=0

for i in range(len(a)-1):
  if increase_flag==False and decrease_flag==False:

    if a[i+1]>a[i]:
      increase_flag=True
      answer+=1
    elif a[i+1]<a[i]:
      decrease_flag=True
      answer+=1
      
  else:
    if decrease_flag==True and a[i+1]>a[i]:
      decrease_flag=False
    elif increase_flag==True and a[i+1]<a[i]:
      increase_flag=False

if increase_flag==False and decrease_flag==False:
  if a[i+1]>a[i]:
    increase_flag=True
    answer+=1
  elif a[i+1]<a[i]:
    decrease_flag=True
    answer+=1
print(answer)

Submission status: Runtime Error

Solution code:
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
# import math
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
# sys.setrecursionlimit(int(pow(10, 2)))
sys.stdin = open("input.txt", "r")
sys.stdout = open("output.txt", "w")
mod = int(pow(10, 9) + 7)
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]





# @lru_cache(None)

# def seive():
#     prime=[1 for i in range(10**6+1)]
#     prime[0]=0
#     prime[1]=0
#     for i in range(10**6+1):
#         if(prime[i]):
#             for j in range(2*i,10**6+1,i):
#                 prime[j]=0


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)

Submission status: Runtime Error

Solution code:
def solve():
  n = int(input())
  a = list(map(int, input().split()))
  g, z = 0, 0
  cut = 1
  for i in range(n-1):
    if a[i+1] > a[i]:
      g += 1
    elif a[i+1] < a[i]:
      z += 1
    if g != 0 and z != 0:
      ans += 1
      g, z = 0, 0
  print(ans)
  return 0
 
if __name__ == "__main__":
  solve()

Submission status: Runtime Error

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
from enum import Enum, auto
#======================================================#
class State(Enum):
    UP = auto()
    DOWN = auto()
    HORI = auto()

def main():
    n = II()
    prev, *aa = MII()
    state = State.HORI
    cnt = 1
    for a in aa:
        if prev < a:
            if state == State.HORI:
                state = State.UP
            elif state == State.DOWN:
                cnt += 1
                state = State.HORI
        elif prev > a:
            if state == State.HORI:
                state = State.DOWN
            elif state == State.UP:
                cnt += 1
                state = State.HORI
        prev = a
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n = int(input())
al = list(map(int, input().split()))

if al[1] > al[0]:
    now = 'U'
elif al[1] < al[0]:
    now = 'D'
else:
    now = 'E'

cnt = 1
for i in range(2, n):
    if now == 'U':
        if al[i] > al[i-1] or al[i] == 'E':
            now = 'U'
        elif al[i] < al[i-1]:
            cnt += 1
            i += 1
            now = 'E'
    elif now == 'D':
        if al[i] < al[i-1] or al[i] == 'E':
            now = 'D'
        elif al[i] > al[i-1]:
            cnt += 1
            now = 'E'
    elif now == 'E':
        if al[i] == al[i - 1]:
            now = 'E'
        elif al[i] > al[i - 1]:
            now = 'U'
        elif al[i] < al[i - 1]:
            now = 'D'
print(cnt)

Submission status: Runtime Error

Solution code:
from otsutil.funcs import parg
_ = input()
A = map(int, input().split())
prev = next(A)
up = None
count = 1
for i in A:
    if prev < i:
        if up is None:
            up = True
        elif not up:
            count += 1
            up = None
    else:
        if up is None:
            up = False
        elif up:
            count += 1
            up = None
    prev = i
print(count)

Submission status: Runtime Error

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



if A[0] > A[1]:
    status = 'dec'

elif A[1] > A[0]:
    status = 'inc'

else:
    status = 'equal'


cnt = 0

for i in range(2,N):
    if status == 'dec' and A[i] > A[i-1]:
        cnt += 1
        status = 'none'
    
    elif status == 'inc' and A[i] < A[i-1]:
        cnt += 1
        status = 'none'
    
    elif status == 'equal' and A[i] > A[i-1]:
        status = 'inc'
    
    elif status == 'equal' and A[i] < A[i-1]:
        status = 'dec'
    
    elif status == 'none' and A[i] < A[i-1]:
        status = 'dec'
    
    elif status == 'none' and A[i] > A[i-1]:
        status = 'inc'
    
    elif status == 'none' and A[i] == A[i-1]:
        status = 'equal'

print(cnt+1)

Submission status: Runtime Error

Solution code:
import sys

read= sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
MOD = 1000000007
sys.setrecursionlimit(10**7)

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



#増減のメモ 
# A[i] > A[i+1]  :  1
# A[i] < A[i+1]  : -1
# A[i] == A[i+1] :  0
x = []
for i in range(N-1):
    if A[i]  < A[i+1]:
        x.append(1)
    elif A[i] > A[i+1]:
        x.append(-1)
    else:
        x.append(0)


res = 1  #答え

#print(len(x))
state = x[0] #単調増加 or 単調減少の二種で現在の状態を記録
i = 1
while  i < N-1:
    if x[i] ==  state or state == 0:
        pass
    else:
        if i != N -2:
            state = x[i+1]
            i+=1
            res+=1
    i+=1

if x[-1] != x[-2] and x[-1] != 0 and x[-2]!= 0 :
    print(res+1)
else:
    print(res)