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

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

if n <= 2:
    print(1)
    exit()

dec, inc = False, False
cnt = 1

for i in range(n-1):
    if a[i+1] - a[i] < 0:
        dec = True
    elif a[i+1] - a[i] > 0:
        inc = True

    if dec and inc:
        cnt += 1
        dec, inc = False, False

print(cnt)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
ascend = True
for j in range(1,N):
    if A[j-1] < A[j]:
        break
    if A[j-1] > A[j]:
        ascend = False
        break
ans = 1
for i in range(1,N):
    if ascend:
        if A[i-1] > A[i]:
            ans += 1
            ascend = True
            for j in range(i+1,N):
                if A[j-1] < A[j]:
                    break
                if A[j-1] > A[j]:
                    ascend = False
                    break
    else:
        if A[i-1] < A[i]:
            ans += 1
            ascend = True
            for j in range(i+1,N):
                if A[j-1] < A[j]:
                    break
                if A[j-1] > A[j]:
                    ascend = False
                    break
print(ans)

Solution (Python):
n = int(input())
a_li = list(map(int,input().split()))

cnt = 0
swich = ""

for i in range(n):
    if i == 0:
        s = a_li[i]
        continue
    
    if a_li[i] == s:
        continue
    
    if swich == "":
        if a_li[i] > s:
            swich = "+"
        else:
            swich = "-"
            
    elif a_li[i] > s:
        if swich == "+":
            pass
        else:
            cnt += 1
            swich = ""
    elif a_li[i] < s:
        if swich == "-":
            pass
        else:
            cnt += 1
            swich = ""
            
    s = a_li[i]

print(cnt + 1)

Solution (Python):
N = input()
As = [int(x) for x in input().split()]

new_As = [As[0]]
for i in range(1, len(As)):
    if As[i-1] != As[i]:
        new_As.append(As[i])
As = new_As

idx = 0
mode_change_count = 1
while True:            
    if idx >= (len(As)-2):
        break
    if (As[idx+1] - As[idx])*(As[idx+2] - As[idx+1]) > 0:
        idx += 1
    elif (As[idx+1] - As[idx])*(As[idx+2] - As[idx+1]) < 0:
        idx += 2
        mode_change_count += 1
  
print(mode_change_count)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
counter = 1
flag = 0
for i in range(N-1):
  if A[i+1] > A[i]:
    if flag < 0:
      counter +=1
      flag = 0
    else:
      flag +=1
  elif A[i+1] < A[i]:
    if flag > 0:
      counter += 1
      flag = 0
    else:
      flag -=1

print(counter)

Solution (Python):
def upper_or_lower(i):
    if A[i] < A[i + 1]:
        return "upper"
    elif A[i] > A[i + 1]:
        return "lower"
    else:
        return upper_or_lower(i + 1)


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

state = "upper"  
ans = 1


if (len(A) > 2) and (A[0] > A[1]):
    state = "lower"

for i in range(2, N):
    if (state == "upper") and (A[i - 1] > A[i]):
        ans += 1
        if i < N - 1:
            state = upper_or_lower(i)
    elif (state == "lower") and (A[i - 1] < A[i]):
        ans += 1
        if i < N - 1:
            state = upper_or_lower(i)
    else:
        continue

print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
cnt,b=1,0
for i in range(1,n):
    s=a[i]-a[i-1]
    if b==0:
        b=s
    elif s*b<0:
        cnt+=1
        b=0
print(cnt)

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

b = 0
s = 0
ans = 1
for i in range(n-1):
    if a[i] < a[i+1]:
        b = 1
    elif a[i] > a[i+1]:
        s = 1
    if b and s:
        ans += 1
        b = 0
        s = 0
print(ans)

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

f = 0
ans = 1
for i in range(n-1):
    if f == 0:
        if a[i] == a[i+1]:
            f = 0
        elif a[i] < a[i+1]:
            f = 1
        else:
            f = 2
    elif f == 1:
        if a[i] <= a[i+1]:
            pass
        else:
            ans += 1
            f = 0
    elif f == 2:
        if a[i] >= a[i+1]:
            pass
        else:
            ans += 1
            f = 0

print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
a.append(a[-1])
f = 0
ans = 1
for i in range(n-1):
    if f == 0:
        if a[i] == a[i+1]:
            f = 0
        elif a[i] < a[i+1]:
            f = 1
        else:
            f = 2
    elif f == 1:
        if a[i] <= a[i+1]:
            pass
        else:
            ans += 1
            f = 0
    elif f == 2:
        if a[i] >= a[i+1]:
            pass
        else:
            ans += 1
            f = 0

print(ans)

Solution (Python):

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

if N==1:
    print(1)
    exit()


plus_minus = 0
ans = 0
for i in range(N-1):

    if A[i+1]-A[i]>0:
        if plus_minus == 0:
            ans += 1
            plus_minus = 1
        elif plus_minus == -1:
            plus_minus = 0
        
    elif A[i+1]-A[i]<0:
        if plus_minus == 0:
            ans += 1
            plus_minus = -1
        elif plus_minus == 1:
            plus_minus = 0
    
    if i+1==N-1:
        if plus_minus == 0:
            ans += 1
            continue

print(ans)

Solution (Python):
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7



N = int(sys.stdin.buffer.readline())
A = list(map(int, sys.stdin.buffer.readline().split()))

A = np.array(A, dtype=int)

dp = np.zeros((N, 2), dtype=int)
dp[0] = 1
D = np.diff(A) <= 0
U = np.diff(A) >= 0

for i, (d, u) in enumerate(zip(D, U), 1):
    if u and d:
        dp[i] = dp[i - 1]
    if u and not d:
        dp[i][0] = dp[i - 1][0]
        dp[i][1] = dp[i - 1].min() + 1
    if not u and d:
        dp[i][0] = dp[i - 1].min() + 1
        dp[i][1] = dp[i - 1][1]

print(dp[-1].min())

Solution (Python):
from sys import stdin
N = int(stdin.readline().rstrip())
A = [int(_) for _ in stdin.readline().rstrip().split()]

pmz = 0
ans = 1

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

Solution (Python):
import sys
n = int(input())
a = list(map(int, sys.stdin.readline().split()))
cnt = 1
up = False
down = False

for i in range(n - 1):
    if up == False and down == False:
        if a[i] < a[i + 1]:
            up = True
        elif a[i] > a[i + 1]:
            down = True
        else:
            pass
    elif up == True:
        if a[i] > a[i + 1]:
            cnt += 1
            up = False
    elif down == True:
        if a[i] < a[i + 1]:
            cnt += 1
            down = False

print(cnt)

Solution (Python):
def hantei(current):
    
    if current == n-1:
        up = True
        return up
    for i in range(current, n):
        if a[i+1] != a[i]:
            if a[i+1] > a[i]:
                up = True
            else:
                up = False
            break
    return up
    

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

ans = 0
ans_list = [[]]

up = hantei(0)

for i in range(n):
    
    if i == 0:
        ans_list[ans].append(a[i])
        continue
    if ((up == True and a[i-1] < a[i]) or (up == False and a[i-1] > a[i]) or (a[i-1] == a[i])):
        
        
        ans_list[ans].append(a[i])
    elif up == False and a[i-1] < a[i]:
        
        ans += 1
        ans_list.append([])
        
        ans_list[ans].append(a[i])
        up = hantei(i)
    elif up == True and a[i-1] > a[i]:
        
        ans += 1
        ans_list.append([])
        
        ans_list[ans].append(a[i])
        up = hantei(i)
print(len(ans_list))

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

sgn = 0
answer = 1
for i in range(N-1):
    cur = A[i+1] - A[i]
    if cur * sgn < 0:
        answer += 1
        sgn = 0
        continue
    if cur != 0:
        sgn = cur
print(answer)

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

pre = a[0]
now = ""
cnt = 1
for i in a:
    if now == "":
        if i > pre:
            now = ">"
        elif i < pre:
            now = "<"
    else:
        if i > pre and now == "<":
            now = ""
            cnt += 1
        elif i < pre and now == ">":
            now = ""
            cnt += 1
    pre = i
print(cnt)

Solution (Python):


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

trend=0
ans=0
for i in range(N-1):
    temp=A[i+1]-A[i]
    if trend==0:
        if temp>0:
            trend=1
            ans+=1
        elif temp<0:
            trend=-1
            ans+=1
        else:
            pass
    elif trend==1:
        if temp>0:
            pass
        elif temp<0:
            trend=0
        else:
            pass
    elif trend==-1:
        if temp>0:
            trend=0
        elif temp<0:
            pass
        else:
            pass

print(ans if trend!=0 else ans+1)

Solution (Python):

N = int(input())
A = list(map(int, input().split()))
if N == 1:
    print(1)
    exit()
for i in range(N):
    if A[i] == A[i+1]:
        pass
    elif A[i] > A[i+1]:
        now = "N"
        break
    else:
        now = "P"
        break
ans = 1
for i in range(N-1):
    if now == "?":
        if A[i] == A[i+1]:
            pass
        elif A[i] > A[i+1]:
            now = "N"
        else:
            now = "P"
    elif now == "P":
        if A[i] <= A[i+1]:
            pass
        else:
            ans += 1
            now = "?"
    else:
        if A[i] >= A[i+1]:
            pass
        else:
            ans += 1
            now = "?"
print(ans)

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

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

count = 0

equal = 0
minus = 1
plus = 2

status = equal

for i in range(n - 1):
    if status == equal:
        if a[i] < a[i+1]:
            status = plus
        elif a[i] > a[i+1]:
            status = minus 
    elif status == plus:
        if a[i] > a[i+1]:
            status = equal
            count += 1
    elif status == minus:
        if a[i] < a[i+1]:
            status = equal
            count +=1
print(count+1)

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

cnt = 0

jug = 2
for i in range(n - 1):
    if jug == 2:
        if a[i] < a[i+1]:
            jug = 0
        elif a[i] > a[i+1]:
            jug = 1 
    elif jug == 0:
        if a[i] > a[i+1]:
            jug = 2
            cnt += 1
    elif jug == 1:
        if a[i] < a[i+1]:
            jug = 2
            cnt +=1
print(cnt+1)

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

cut=0
if n==0:
  print(1)
else:
  status=2 
  for i in range(n-1):
    if status==2:
      if a[i]<a[i+1]:
        status=0
      elif a[i]>a[i+1]:
        status=1
    elif status==0:
      if a[i]>a[i+1]:
        cut+=1
        status=2
    elif status==1:
      if a[i]<a[i+1]:
        cut+=1
        status=2
print(cut+1)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
up=False
down=False
ans=0
for i in range(n-1):
    if(not up and not down):
        if(a[i]<a[i+1]):
            up=True
        elif(a[i]>a[i+1]):
            down=True
    elif(up):
        if(a[i]>a[i+1]):
            ans+=1
            up=False
    elif(down):
        if(a[i]<a[i+1]):
            ans+=1
            down=False
ans+=1
print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
if N == 1:
  print(1)
  exit()
ans = 0
i = 0
while i < N-1:
  ans += 1
  tag = A[i]
  if tag == A[i+1]:
    while tag == A[i+1]:
      i += 1
      if i >= N - 1:
        break
  if tag < A[i+1]:
    
    while tag <= A[i+1]:
      tag = A[i+1]
      i += 1
      if i >= N - 1:
        break
    i += 1
    if i == N - 1:
      ans += 1
  else:
    
    while tag >= A[i+1]:
      tag = A[i+1]
      i += 1
      if i >= N - 1:
        break
    i += 1
    if i == N - 1:
      ans += 1
print(ans)

Solution (Python):
def main():
    n = int(input())
    a = list(map(int, input().split()))

    UP = 1
    NEUTRAL = 0
    DOWN = -1

    sgn = NEUTRAL

    ans = 0
    for i in range(n-1):
        if sgn == UP:
            if a[i] > a[i+1]:
                sgn = NEUTRAL
                ans += 1
        elif sgn == NEUTRAL:
            if a[i] < a[i+1]:
                sgn = UP
            elif a[i] > a[i+1]:
                sgn = DOWN
        else:
            if a[i] < a[i+1]:
                sgn = NEUTRAL
                ans += 1

    ans += 1
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
ans=1
for i, a in enumerate(A):
  if i==0:
    flag=0
  elif flag==0 and a==A[i-1]:
    pass
  elif a>=A[i-1] and flag>=0:
    flag=1
  elif a<=A[i-1] and flag<=0:
    flag=-1
  else:
    flag=0
    ans+=1
print(ans)

Solution (Python):
from operator import lt, gt

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

ans = n + 1
for first_checker in (lt, gt):
    cnt = 1
    prev = a[0]
    f = first_checker
    for x in a:
        if x == prev:
            pass
        elif f is None:
            if x > prev:
                f = gt
            else:
                f = lt
        elif f(x, prev):
            pass
        else:
            f = None
            cnt += 1
        prev = x
    ans = min(ans, cnt)
print(ans)

Solution (Python):
n=int(input())
a=list(map(int, input().split()))
f=None 
s=1
for i in range(n-1):
    if a[i]==a[i+1]:
        pass
    elif f==None:
        f=(a[i]<a[i+1]) 
    elif f==(a[i]<a[i+1]):
        pass
    else:
        s+=1
        f=None 
print(s)

Solution (Python):
n = input()
iter_ = iter(list(map(int, input().rstrip("\n").split())))
ans = 0

last = next(iter_)
try:
    while True:
        ans += 1
        now = next(iter_)
        while now == last:
            now = next(iter_)
        laststate = (last < now)
        state = laststate
        while state == laststate:
            last = now
            now = next(iter_)
            while now == last:
                now = next(iter_)
            state = (last < now)
        last = now
except StopIteration:
    print(ans)

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

flg = None 
slash = 0
for i in range(n - 1):
    if a[i] == a[i + 1]:
        pass
    elif flg == None:
        flg = (a[i] < a[i + 1]) 
    elif flg == (a[i] < a[i + 1]):
        pass
    else:
        slash += 1
        flg = None 

print(slash + 1)

Solution (Python):
import collections
import sys
input = sys.stdin.readline

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

count=1
mode=0
for i,Num in enumerate(A):
    if(mode==0):
        mode=1
    elif(mode==1):
        if(A[i]==A[i-1]):
            mode=1
        elif(A[i]>A[i-1]):
            mode=2
        elif(A[i]<A[i-1]):
            mode=3
    elif(mode==2):
        if(A[i]<A[i-1]):
            mode=1
            count+=1
    elif(mode==3):
        if(A[i]>A[i-1]):
            mode=1
            count+=1

print(count)

Solution (Python):
n=int(input())
a=list(map(lambda x: int(x), input().split()))
last=a[0]
inc=None
ans=1
for i in range(1,len(a)):
    if inc==True and a[i]>=last:
        pass
    elif inc==True and a[i]<last:
        inc=None
        ans+=1
    elif inc==False and a[i]<=last:
        pass
    elif inc==False and a[i]>last:
        inc=None
        ans+=1
    elif inc==None and a[i]>last:
        inc=True
    elif inc==None and a[i]<last:
        inc=False
    elif inc==None and a[i]==last:
        pass
    last=a[i]
print(ans)

Solution (Python):
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)

Solution (Python):
n=int(input())
a = list(map(int,input().split()))
cnt=1
index=0
state=""
while index+1 < n:
    if state=="low" and a[index]<a[index+1]:
        state=""
        cnt+=1
        index+=1
    elif state=="high" and a[index]>a[index+1]:
        state=""
        cnt+=1
        index+=1
    elif a[index]<a[index+1]:
        state="high"
        index+=1
    elif a[index]>a[index+1]:
        state="low"
        index+=1
    else:
        index+=1
print(cnt)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
pm = 0
ans = 1

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

print(ans)

Solution (Python):
N = int(input())
l = list(map(int, input().split()))

result = 0
i = 0

while(i < N - 1):
  if(l[i] == l[i + 1]):
    i += 1
    continue
  elif(l[i] < l[i + 1]):
    while(i < N - 1 and l[i] <= l[i + 1]):
      i += 1
  else:
    while(i < N - 1 and l[i] >= l[i + 1]):
      i += 1
  result += 1
  i += 1

if(i == N - 1):
  result += 1
  
print(result)

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



























B = []
for i in range(1, n):
    if A[i] != A[i-1]:
        B.append(int((A[i]-A[i-1]) / abs(A[i]-A[i-1])))


ans = 1
t = 0
m = 0
for i in range(len(B)):
    
    if B[i] * t < 0:
        
        ans += 1
        t = 0
        if i == len(B):
            ans += 1
    else:
        t = B[i]

print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
c = 0
C = 1

for i in range(1,N):
  d = A[i] - A[i-1]
  if c == 0:
    c = d
  elif c*d < 0:
    C += 1
    c = 0
print(C)

Solution (Python):


N = int(input().split()[0])
a_list = list(map(int, input().split()))

c = 0
is_minus = True
start_flag = True

for i in range(1, N):
    if start_flag:
        if a_list[i] == a_list[i - 1]:
            continue
        is_minus = True if a_list[i] < a_list[i - 1] else False
        start_flag = False
        continue

    if is_minus:
        if a_list[i] > a_list[i - 1]:
            c += 1
            start_flag = True
    else:
        if a_list[i] < a_list[i - 1]:
            c += 1
            start_flag = True

ans = c + 1
print(ans)

Solution (Python):
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    b = 0
    cnt = 1
    for i in range(1, n):
        if a[i] > a[i-1]:
            if b == 0:
                b = 1
            elif b == -1:
                cnt += 1
                b = 0
        elif a[i] < a[i-1]:
            if b == 0:
                b = -1
            elif b == 1:
                cnt += 1
                b = 0
    print(cnt)


if __name__ == '__main__':
    slove()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
now=0
p=a[0]
cnt=1
for i in range(n):
	if a[i]>p:
		if now=="-":
			cnt+=1
			now=0
		else:
			now="+"
	elif a[i]<p:
		if now=="+":
			cnt+=1
			now=0
		else:
			now="-"
	p=a[i]
print(cnt)

Solution (Python):
_ = input()
N = list(map(int, input().split()))
pren = N[0]
preud = 0 
cnt = 0
for n in N:
  
  if pren < n:
    ud = 1
  elif pren > n:
    ud = -1
  else:
    ud = 0
  
  if preud * ud == - 1:
    cnt += 1
    preud = 0
  else:
    if ud != 0:
      preud = ud
  pren = n

print(cnt+1)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = [A[0]]

for a in A:
    if B[-1] != a:
        B.append(a)

N = len(B)
ans = 1
prev = -1
for i in range(1, N - 1):
    if B[i - 1] < B[i] and B[i + 1] < B[i]:
        if i - prev > 1:
            ans += 1
            prev = i
    elif B[i - 1] > B[i] and B[i + 1] > B[i]:
        if i - prev > 1:
            ans += 1
            prev = i

print(ans)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
m=0
c=1
for i in range(1,N):
  d=A[i]-A[i-1]
  if m==0:m=d
  elif m*d<0:
    c+=1
    m=0
print(c)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
mono=0
c=1
for i in range(1,N):
  d=A[i]-A[i-1]
  if mono==0:
    mono=d
  elif mono*d<0:
    c+=1
    mono=0
print(c)

Solution (Python):
n = input()
l = list(map(int, input().split()))
s = 0
now = l[0]
count = 0
for i in range(1, len(l)):
    if s == 0:
        if l[i] > now:
            s = 1
        elif l[i] < now:
            s = -1
    elif s == 1:
        if not (l[i] >= now):
            count += 1
            s = 0
    else:
        if l[i] > now:
            count += 1
            s = 0
    now = l[i]
print(count + 1)

Solution (Python):
N,*A=map(int,open(0).read().split());f=a=0
for i,j in zip(A,A[1:]):
 if f:
  if(f<0)&(i<j)|(f>0)&(i>j):a+=1;f=0
 else:f=(i<j)-(i>j)
print(a+1)

Solution (Python):
N,*A=map(int,open(0).read().split());f=a=0
for i,j in zip(A,A[1:]):
 if f<0:
  if i<j:a+=1;f=0
 elif f<1:f=(i<j)-(i>j)
 else:
  if i>j:a+=1;f=0
print(a+1)

Solution (Python):
N = int(input())
a = list(map(int,input().split()))

data = []
for i in range(N-1):
  if a[i+1]-a[i] == 0:
    continue
  else:
    data.append(a[i+1]-a[i])


if len(data) <= 1:
  print(1)
else:
  ans = 0
  left = 0
  i = 0
  while left < len(data)-1:
    if data[left]*data[left+1] < 0: 
      ans += 1
      left += 1
      i = left
    left += 1
  else:
    if i == len(data):
      pass
    else:
      ans += 1
    print(ans)

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

for i in range(1, N):
    if A[i-1] == A[i]:
        pass
    elif Flag == None:
        Flag = (A[i-1] < A[i])
    elif Flag == (A[i-1] < A[i]):
        pass
    else:
        count += 1
        Flag = None

print(count)

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


if N==1:
  print(1)
else:
  ans=1
  c=0
  
  for i in range(1,N):
    if A[i]>A[i-1]:
      if c==-1:
        c=0
        ans+=1
      else:
        c=1
    elif A[i]<A[i-1]:
      if c==+1:
        c=0
        ans+=1
      else:
        c=-1
    
  print(ans)

Solution (Python):
n = int(input())
arr = list(map(int, input().split()))

cnt = 1

if len(arr) == 1:
    print(cnt)
    exit()

a = arr[0]
cmode = 0
nmode = 0
sz = 1
for i in range(1, len(arr)):

    if a > arr[i]:
        nmode = 1
    elif a == arr[i]:
        nmode = cmode
    else:
        nmode = 2

    if cmode != 0:
        if nmode != cmode:
            cnt += 1
            cmode = 0
            sz = 1
        else:
            cmode = nmode
            sz += 1
    else:
        cmode = nmode

    a = arr[i]

print(cnt)

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

Solution (Python):
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    f = 0
    cnt = 0
    for i in range(n):
        if i != 0:
            if f == 0:
                if a[i] > a[i-1]:
                    f = 1
                elif a[i] < a[i-1]:
                    f = -1
                else:
                    f = 0
            else:
                if f == 1 and a[i] >= a[i-1]:
                    continue
                elif f == -1 and a[i] <= a[i-1]:
                    continue
                else:
                    cnt += 1
                    f = 0
    print(cnt + 1)


if __name__ == '__main__':
    slove()

Solution (Python):


n = int(input())
l = list(map(int, input().split()))
flag = None
ans = 1
for i in range(1, n):
    if l[i-1] == l[i]:
        pass
    elif flag == None:
        flag = l[i-1] < l[i]
    elif flag == (l[i-1] < l[i]):
        pass
    else:
        ans += 1
        flag = None
print(ans)

Solution (Python):

def main():
    N, *A = map(int, open(0).read().split())
    ans, cur = 0, A[0]
    flg = 0  
    for i in A:
        if (flg == 1 and i < cur) or (flg == 2 and i > cur):  
            ans += 1
            flg = 0
        elif not flg and cur < i:  
            flg = 1
        elif not flg and cur > i:  
            flg = 2
        cur = i
    print(ans + 1)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
result = 1
d = 0
for i in range(1, N):
  if A[i - 1] == A[i]:
    continue
  if d == 0:
    if A[i - 1] > A[i]:
      d = -1
    else:
      d = 1
  elif d == 1 and A[i - 1] > A[i]:
    d = 0
    result += 1
  elif d == -1 and A[i - 1] < A[i]:
    d = 0
    result += 1
print(result)

Solution (Python):
N = int(input())
As = list(map(int, input().split()))
r = 0
mode = "0"
for i in range(1, N):
  if mode == "-" and As[i] > As[i-1]:
    r += 1
    mode = "0"
  elif mode == "+" and As[i] < As[i-1]:
    r += 1
    mode = "0"
  elif As[i] < As[i-1]:
    mode = "-"
  elif As[i] > As[i-1]:
    mode = "+"
r += 1
print(r)

Solution (Python):
N = input()
N =int(N)
nums= list(map(int,input().split(" ")))
ans = 1
tend = 0
before = nums[0]
for a in nums[1:]:
    if (before - a) * tend < 0:
        tend = 0
        ans += 1
    else:
        tend = 1 if before - a > 0  else -1 if before - a < 0 else tend

    before = a

print(ans)

Solution (Python):
import numpy as np
N = int(input())
As = input().split()
for i in range(N):
    As[i] = int(As[i])
As = np.array(As)


def get_sign(x):
    if x>0:
        return +1
    elif x<0:
        return -1
    else:
        return 0

ans = 1
a0 = None
a1 = None
for a in As:
    if a0 is None:
        a0 = a
    elif a1 is None:
        a1 = a
    else:
        if (get_sign(a-a1)==get_sign(a1-a0))|(get_sign(a1-a0)==0):
            a0 = a1
            a1 = a
        elif get_sign(a-a1)==0:
            pass
        else:
            ans += 1
            a0 = a
            a1 = None
print(ans)

Solution (Python):
import sys
input=sys.stdin.readline

def main():
    _ = int(input())
    A = list(map(int, input().split()))
    pre = A[0]
    ans = 1
    state = 0
    for a in A:
        if state == 0:
            if a > pre:
                state = 1
            if a < pre:
                state = -1
        if state == 1:
            if a < pre:
                ans += 1
                state = 0
        elif state == -1:
            if a > pre:
                ans += 1
                state = 0
        pre = a
    print(ans)

if __name__ == '__main__':
    main()

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

ans=1
count=0

for i in range(n-1):
  if a[i]<a[i+1]:
    if count<0:
      ans+=1
      count=0
    else:
      count+=1
  elif a[i]>a[i+1]:
    if count>0:
      ans+=1
      count=0
    else:
      count-=1

print(ans)

Solution (Python):
n = int(input())

arr = list(map(int, input().split(" ")))

ans = 1
case = 0

for i in range(1, n):
  judge = arr[i] - arr[i-1]
  if case == 0:
    if judge > 0:
      case = 1
    elif judge < 0:
      case = -1
  elif case == 1 and judge < 0 or case == -1 and judge > 0:
    case = 0
    ans += 1
print(ans)

Solution (Python):

import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N = int(input())
        A = list(map(int, input().split()))

        ans = 1
        state = 0
        for i in range(1, N):
            if state == 1:
                if A[i - 1] <= A[i]:
                    continue
                else:
                    ans += 1
                    state = 0
            elif state == -1:
                if A[i - 1] >= A[i]:
                    continue
                else:
                    ans += 1
                    state = 0
            elif state == 0:
                if A[i - 1] == A[i]:
                    continue
                elif A[i - 1] > A[i]:
                    state = -1
                elif A[i - 1] < A[i]:
                    state = 1
        print(ans)



if __name__ == '__main__':
    AtCoder().main()

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

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

Solution (Python):
n = int(input())
arr = list(map(int, input().split()))

count = 1
mode = None
for i in range(n):
    if i+1 == n or arr[i+1] - arr[i] == 0:
        continue
    elif mode is None:
        if arr[i+1] > arr[i]:
            mode = 'up'
        elif arr[i+1] < arr[i]:
            mode = 'down'
    elif mode == 'up':
        if arr[i+1] < arr[i]:
            count += 1
            mode = None
    elif mode == 'down':
        if arr[i+1] > arr[i]:
            count += 1
            mode = None

print(count)

Solution (Python):
n = int(input())
arr = list(map(int, input().split()))

count = 1
mode = None
for i in range(n):
    if i+1 == n or arr[i+1] - arr[i] == 0:
        continue
    elif mode is None:
        if arr[i+1] > arr[i]:
            mode = 'up'
        elif arr[i+1] < arr[i]:
            mode = 'down'
    elif mode == 'up':
        if arr[i+1] < arr[i]:
            count += 1
            mode = None
    elif mode == 'down':
        if arr[i+1] > arr[i]:
            count += 1
            mode = None

print(count)

Solution (Python):
n,*l=map(int,open(0).read().split())
a=b=c=0;l+=[0,1]
while a<n:
	while l[a]<=l[a+1]:a+=1
	while l[b]>=l[b+1]:b+=1
	a=b=max(a,b)+1
	c+=1
print(c)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
f=0
ans=1
for i in range(1,n):
    if a[i]>a[i-1]:
        if f==0 or f==1:
            f=1
        else:
            ans+=1
            f=0
    elif a[i]<a[i-1]:
        if f==2 or f==0:
            f=2
        else:
            ans+=1
            f=0
    else:
        continue
print(ans)