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()))
state = None
cnt = 1
for i in range(1, n):
    if state == None:
        if a[i] > a[i-1]:
            state = 'up'
        elif a[i] < a[i-1]:
            state = 'down'
        else:
            continue
    elif state == 'up' and a[i] < a[i-1]:
        cnt += 1
        state = None
    elif state == 'down' and a[i] > a[i-1]:
        cnt += 1
        state = None
print(cnt)

Solution (Python):
n, *a = map(int, open(0).read().split())

isInc = False
isDec = False
cnt = 1
bef_a = a[0]

for ai in a:
    if isInc and bef_a > ai:
        cnt += 1
        isInc = False
    elif isDec and bef_a < ai:
        cnt += 1
        isDec = False
    elif isInc == False and isDec == False:
        if bef_a < ai:
            isInc = True
        elif bef_a > ai:
            isDec = True
    bef_a = ai
print(cnt)

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


import copy

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

arr = []
ans = []



status = 0

for i in range(N):
  if status == 0:
    if len(arr) > 0:
      if A[i] > arr[-1]:
        status = 1
      elif A[i] < arr[-1]:
        status = 2

    arr.append(A[i])
  elif status == 1 and A[i] >= arr[-1]:
    arr.append(A[i])
  elif status == 2 and A[i] <= arr[-1]:
    arr.append(A[i])
  else:
    ans.append(copy.copy(arr))
    arr = [A[i]]
    status = 0

if len(arr) > 0:
  ans.append(arr)

print(len(ans))

'''
for a in ans:
  print(a)
'''

Solution (Python):
if __name__ == '__main__':
    n = int(input())

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

    dp = [[10 ** 9 for j in range(2)] for i in range(n)]
    dp[0][0] = 1 
    dp[0][1] = 1 

    for i in range(1, n):
        if arr[i] > arr[i-1]:
            dp[i][0] = min(dp[i-1][0], dp[i-1][1] + 1)
            dp[i][1] = min(dp[i-1][0], dp[i-1][1]) + 1
        elif arr[i] < arr[i-1]:
            dp[i][0] = min(dp[i-1][0], dp[i-1][1]) + 1
            dp[i][1] = min(dp[i-1][0] + 1, dp[i-1][1])
        else:
            dp[i][0] = dp[i-1][0]
            dp[i][1] = dp[i-1][1]
    
    print(min(dp[-1]))

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

ans = 0
up = None
prev = None
for v in a:
    if prev == v:
        continue
    if prev is not None:
        if up is None:
            up = (prev <= v)
        else:
            if (prev <= v) != up:
                ans += 1
                up = None
    prev = v
print(ans + 1)

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

cnt = n
prev = a[0]
trend ='N'

for i in range(1, n):
    if a[i] > prev:
        if (trend == 'U' or trend == 'N'):
            cnt -= 1
            trend ='U'
        else:
            trend = 'N'
    if a[i] < prev:
        if (trend == 'D' or trend == 'N'):
            cnt -= 1
            trend = 'D'
        else:
            trend = 'N'
    if a[i] == prev:
        cnt -= 1
    prev = a[i]
print(cnt)

Solution (Python):
N = int(input())
nums = list(map(int, input().split()))
upper = True
lower = True
before = nums[0]
answer = 0
for i, num in enumerate(nums):
  if before < num:
    lower = False
  elif before > num:
    upper = False
  before = num
  if not upper and not lower:
    answer += 1
    upper = True
    lower = True
print(answer+1)

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

sw='n'
ans=0
for i in range(1,n):
    if sw=='u':
        if l[i-1]-l[i]<=0:
            continue
        else:
            ans+=1
            sw='n'
    elif sw=='d':
        if l[i-1]-l[i]>=0:
            continue
        else:
            ans+=1
            sw='n'
    else:
        if l[i-1]-l[i]<0:
            sw='u'
        elif l[i-1]-l[i]>0:
            sw='d'
        else:
            continue

print(ans+1)

Solution (Python):
import sys
import itertools

import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
from itertools import permutations
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


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

d = [0] * N
for i in range(1, N):
    d[i] = A[i] - A[i - 1]

l = 0
r1 = 1
r2 = 1
ans = 0
while l < N:
    while r1 < N and d[r1] >= 0:
        r1 += 1
    while r2 < N and d[r2] <= 0:
        r2 += 1
    r = max(r1, r2)
    l = r
    ans += 1
    r1 = l + 1
    r2 = l + 1
print(ans)

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

list1 =[0] * n
cnt = 1  
for i in range(n-1):
  if aa[i+1] > aa[i]:
    list1[i+1] = 1
  elif aa[i+1] < aa[i]:
    list1[i+1] = -1
  elif aa[i+1] == aa[i]:    
    list1[i+1] = 0

while 1 in list1 and -1 in list1:
  del list1[:max(list1.index(1), list1.index(-1))]
  list1[0] = 0
  cnt += 1
  
print(cnt)

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

Solution (Python):
n = int(input())
num_list = list(map(int, input().split()))
ans = 1
cnt_list = [0] * n
a = int()

for i in range(1,n):
    if num_list[i-1] > num_list[i]:
        cnt_list[i] = -1
    if num_list[i-1] < num_list[i]:
        cnt_list[i] = 1

while 1 in cnt_list and -1 in cnt_list:
    del cnt_list[:max(cnt_list.index(1),cnt_list.index(-1))]
    cnt_list[0] = 0
    ans += 1

print(ans)

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

ans = 0
up = 0
down = 0
i=0

while (i+1)<n:
    if up == down:
        if a[i+1] > a[i]:
            up += 1
            down = 0
        elif a[i+1] < a[i]:
            up = 0
            down += 1
        else:
            up += 1
            down += 1
    elif up == 0 and down !=0:
        if a[i+1] <= a[i]:
            down += 1
        else:
            ans += 1
            down = 0
    elif down == 0 and up!= 0:
        if a[i+1] >= a[i]:
            up += 1
        else:
            ans += 1
            up = 0
    i += 1

print(ans+1)

Solution (Python):
def f(n,a):
    if n==1:
        print(1)
        exit()
    pre=a[1]-a[0]
    ans=1
    for i in range(2,n):
        if pre*(a[i]-a[i-1])<0:
            pre=0
            ans+=1
        else:
            if a[i]-a[i-1]:
                pre=a[i]-a[i-1]
    return ans

n=int(input())
*a,=map(int,input().split())
print(f(n,a))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
ans=0
i=1
up=0
down=0
while i<n:
  if up==down:
    if a[i]>a[i-1]:
      up+=1
      down=0
    elif a[i]<a[i-1]:
      down+=1
      up=0
    elif a[i]==a[i-1]:
      up+=1
      down+=1
  elif up==0 and down!=0:
    if a[i]<=a[i-1]:
      down+=1
    else:
      ans+=1
      down=0
  elif down==0 and up!=0:
    if a[i]>=a[i-1]:
      up+=1
    else:
      ans+=1
      up=0
  i+=1
print(ans+1)

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

up = down = 0

cur = a[0]
ans = 0
for x in a:
    if cur < x:
        up = 1
    elif cur > x:
        down = 1
    
    if up and down:
        ans += 1
        up = down = 0

    cur = x

print(ans+1)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
ans = 0
i = 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
    ans += 1
    i += 1
print(ans)

Solution (Python):


def check(A,i,j):
    tmp = []
    for k in range(i,j):
        tmp.append(A[k])

    print(tmp)

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

cnt = i = 0
while True:
    flag = 0
    for j in range(i+1,N):
        if flag == 0:
            if A[i] < A[j]:
                flag = 1
            elif A[i] > A[j]:
                flag = -1
        else:
            if flag == 1:
                if A[j-1] > A[j]:
                    
                    i = j
                    cnt += 1
                    break
            elif flag == -1:
                if A[j-1] < A[j]:
                    
                    i = j
                    cnt += 1
                    break
    else:
        cnt += 1
        break

    if i>=N:
        break

print(cnt)

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

ans = 1
d = 0
for i in range(1,n):
    diff = A[i] - A[i-1]
    if not d:
        d = diff
    elif d*diff < 0:
        d = 0
        ans += 1
print(ans)

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

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

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

Solution (Python):
n = int(input())
ls = list(map(int,input().split()))
cnt = 1
i = 0
flag = 10
while i < n-1:
    if ls[i] < ls[i+1] and flag == 10:
        flag = True
    elif ls[i] > ls[i+1] and flag == 10:
        flag = False
    if flag == True:
        if ls[i] > ls[i+1]:
            cnt += 1
            flag = 10
    elif flag == False:
        if ls[i] < ls[i+1]:
            cnt += 1
            flag = 10
    i += 1
print(cnt)

Solution (Python):
import sys
input = sys.stdin.buffer.readline
N = int(input())
A = list(map(int, input().split()))
d = 0
ans = 1
for i in range(1, N):
    if A[i - 1] < A[i]:
        if d < 0:
            ans += 1
            d = 0
        elif d == 0:
            d = 1
    elif A[i - 1] > A[i]:
        if d > 0:
            ans += 1
            d = 0
        elif d == 0:
            d = -1
print(ans)

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

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

if l[0]>l[1]:
  ans="low"
elif l[0]<l[1]:
  ans="high"
else:
  ans="even"
  
for i in range(2,n):
  if l[i-1]>l[i] and (ans=="low" or ans=="even"):
    ans="low"
  elif l[i-1]<l[i] and (ans=="high" or ans=="even"):
    ans="high"
  elif l[i-1]==l[i]:
    continue
  else:
    cnt+=1
    ans="even"
    
print(cnt+1)

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

dir = 0
old = a[0]
ans = 1
for i in range(1, len(a)):
    now = a[i]
    if dir == 0:
        if old < now:
            dir = 1
        elif old > now:
            dir = -1
    elif dir == 1:
        if old > now:
            dir = 0
            ans += 1
    elif dir == -1:
        if old < now:
            dir = 0
            ans += 1
    old = now
print(ans)

Solution (Python):













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

if(n<3):
    print(1)
    exit()

ans = 1
state = 0
now = a[0]
for ai in a[1:]:
    if(state==0):
        if(now == ai):
            state = 0
        elif(now > ai):
            state = -1
        else:
            state = 1
    elif(state==1):
        if(now > ai):
            state = 0
            ans += 1
    else:
        if(now < ai):
            state = 0
            ans += 1
    now = ai

print(ans)

Solution (Python):
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right 
from heapq import heapify, heappop, heappush
from math import *

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
        
def sgn(x):
    if x==0:
        return x
    return x//abs(x)

N,A = ilint()
tmp = A[0]
ans = 1
s = 0
for a in A[1:]:
    if s==0:
        s = sgn(a-tmp)
        tmp = a
    elif s*(a-tmp)>=0:
        tmp = a
    else:
        ans += 1
        tmp = a
        s = 0
print(ans)

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

for i in range(N-1):
    if sw=="1":
        if A[i]>A[i+1]:
            sw="2"
        if A[i]<A[i+1]:
            sw="3"
    elif sw=="2":
        if A[i]<A[i+1]:
            ans+=1
            sw="1"
    elif sw=="3":
        if A[i]>A[i+1]:
            ans+=1
            sw="1"

print(ans)

Solution (Python):
N = int(input())
As = list(map(int,input().split()))
zoka = 0
before = As[0]
c = 1
for i in range(1,N):
    if before < As[i]:
        if zoka == 0 or zoka == 1:
            zoka = 1
        else:
            c+=1
            zoka = 0
        before = As[i]
    elif before > As[i]:
        if zoka == 0 or zoka == 2:
            zoka = 2
        else:
            c+=1
            zoka = 0
        before = As[i]
print(c)

Solution (Python):
N = int(input())
As = list(map(int,input().split()))
zoka = 0
before = As[0]
c = 1
for i in range(1,N):
    if before == As[i]:
        continue
    elif before < As[i]:
        if zoka == 0 or zoka == 1:
            before = As[i]
            zoka = 1
        else:
            c+=1
            zoka = 0
            before = As[i]
    else:
        if zoka == 0 or zoka == 2:
            before = As[i]
            zoka = 2
        else:
            c+=1
            zoka = 0
            before = As[i]
print(c)

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

B=[A[i]-A[i-1] for i in range(1,N)]
ans=0
flag=False
for i in range(N-1):
    if B[i]!=0:
        first=B[i]
        flag=True
    if flag:
        break
for i in range(N-1):
    if first*B[i]>=0:
        pass
    else:
        ans+=1
        j=i+1
        while j<=N-2:
            if B[j]!=0:
                first=B[j]
                break
            else:
                j+=1

print(ans+1)

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

L = [A[0]]
for i in range(1, N):
  if A[i] != A[i - 1]:
    L.append(A[i])

N = len(L)

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

f = L[1] > L[0]
ans = 1

i = 2
while i < N:
  if (L[i] > L[i - 1]) != f:
    ans += 1
    if i == N - 1:
      break
    i += 1
    f = L[i] > L[i - 1]

  i += 1
 
print(ans)

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

c = 1; w = 0
for i in range(N-1):
    if w == 0:
        if A[i] < A[i+1]:
            w = 1
        elif A[i] > A[i+1]:
            w = -1
    elif (w > 0 and A[i] > A[i+1]) or (w < 0 and A[i] < A[i+1]):
         w = 0
         c += 1

print(c)

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
count=0
i=0
while i < n:
    j=i
    while j < n-1 and A[j]<=A[j+1]:
        j += 1
    k=i
    while k < n-1 and A[k]>=A[k+1]:
        k += 1
    count += 1
    i = max(j,k)+1
print(count)

Solution (Python):



def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = 1
    flg = 0
    cursol = 0
    for i in a:
        if cursol == 0:
            cursol = i
            continue
        if flg == 0:
            if i > cursol:
                flg = 1
                cursol = i
            elif i < cursol:
                flg = -1
                cursol = i
        elif flg == 1:
            if i >= cursol:
                cursol = i
            elif i < cursol:
                cursol = i
                flg = 0
                ans += 1
        else:
            if i <= cursol:
                cursol = i
            elif i > cursol:
                cursol = i
                flg = 0
                ans += 1
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

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

flag = 0
ans = 1
tmp = 0

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

print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
flag = 0
cnt = 1
state = [0,0,0]

for i in range(1,n):

    if a[i] > a[i-1]:
        if flag == -1:
            cnt += 1
            flag = 0
        else:
            flag = 1
        state[0] += 1
    elif a[i] == a[i-1]:
        continue
        state[1] += 1
    elif a[i] < a[i-1]:
        if flag == 1:
            cnt += 1
            flag = 0
        else:
            flag = -1
        state[2] += 1
print(cnt)

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

count = 1
up_flag = None
prev_a = a_list[0]
for a in a_list[1:]:
  if up_flag is None:
    if prev_a == a:
      continue
    up_flag = a > prev_a
  if up_flag and a >= prev_a:
    prev_a = a
    continue
  elif not up_flag and a <= prev_a:
    prev_a = a
    continue
  else:
    up_flag = None
    count += 1
    prev_a = a

print(count)

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

Solution (Python):
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] >= tmp[0] or v <= tmp[-1] <= tmp[-2] <= tmp[0]):
      tmp = []
      cnt += 1
  tmp.append(v)

if tmp:
  cnt+=1
print(cnt)

Solution (Python):
n = int(input())
if n>1: a = list(map(int, input().split()))
else: a = int(input()); print(1); exit()
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)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
x=[]
ans=1

for i in range(n-1):
  if a[i+1]==a[i]:
    continue
  x.append(a[i+1]-a[i])
  
flag=True

for i in range(len(x)-1):
  if flag==False:
    flag=True
    continue
  if x[i]<0 and x[i+1]>0 or x[i]>0 and x[i+1]<0:
    ans+=1
    flag=False
    

print(ans)

Solution (Python):



def solve():
    n = int(input())
    A = [int(i) for i in input().split()]
    ans = 0
    cur = []
    flag = None
    for a in A:
        if not cur:
            cur.append(a)
            continue
        elif cur[-1] == a:
            cur.append(a)
        elif flag is None:
            if cur[-1] > a:
                flag = False
            elif cur[-1] < a:
                flag = True
            cur.append(a)
            continue
        elif flag:  
            if cur[-1] > a:
                ans += 1
                cur = [a]
                flag = None
            else:
                cur.append(a)
        elif not flag:  
            if cur[-1] < a:
                ans += 1
                cur = [a]
                flag = None
            else:
                cur.append(a)
    if cur:
        ans += 1
    print(ans)


solve()

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

cnt = 1
top = True
    
for i in range(1,N):
    if A[i] == A[i-1]:
        continue
    if top == True:
        inc = (A[i]>A[i-1])
        top = False
        continue
    
    flag = (A[i]>A[i-1])
    if inc != flag:
        cnt += 1
        top = True
        
print(cnt)

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

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

Solution (Python):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n = I()
    a = LI()
    tp = 0
    ans = 1
    
    for i in range(1,n):
        before = a[i-1]
        after = a[i]

        if tp == 0:
            if before < after:
                tp = 1
            elif before > after:
                tp = -1
        elif tp == 1:
            if before > after:
                ans += 1
                tp = 0
        elif tp == -1:
            if before < after:
                ans += 1
                tp = 0
        
    print(ans)



main()

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

last = 0
ans = 1

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

print(ans)

Solution (Python):
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

N = k()
A = l()
p = 0
ans = 1
for i in range(1, N):
    if p*(A[i]-A[i-1])<0:
        ans += 1
        p = 0
    elif A[i]-A[i-1] != 0:
        p = A[i]-A[i-1]
print(ans)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
flag=0
answer=1
if N==1:
    print(1)
else:
    if A[0] < A[1]:
        flag = 1
    elif A[0]>A[1]:
        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)

Solution (Python):
import bisect, copy, heapq, math, sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product

def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
def celi(a,b):
    return -(-a//b)
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

ans=0
flag=-1

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

print(ans+1)

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

Solution (Python):
import sys
N = int(input())
A = [int(x) for x in input().split()]
if N == 1:
    print(1)
    sys.exit()
 
inc = 0  
ans = 1
for i in range(N - 1):
    if A[i] < A[i + 1]:
        if inc == -1:
            ans += 1
            inc = 0
        elif inc == 0:
            inc = 1
    elif A[i] > A[i + 1]:
        if inc == 1:
            ans += 1
            inc = 0
        elif inc == 0:
            inc = -1
 
print(ans)

Solution (Python):

def sign(x):
    return (x > 0) - (x < 0)


def main():
    _ = int(input())
    a = map(int, input().split())
    ans = []
    arr = []
    state = 0
    for an in a:
        if len(arr) < 2:
            arr.append(an)
        else:
            state += (arr[-1] - arr[-2])
            if state == 0:
                arr.append(an)
            else:
                current = an - arr[-1]
                if current == 0 or sign(state) == sign(current):
                    arr.append(an)
                else:
                    ans.append(arr)
                    arr = [an]
                    state = 0
    ans.append(arr)

    print(len(ans))


if __name__ == '__main__':
    main()

Solution (Python):
def check(a,b):
  if a<b:
    return 'up'
  if a>b:
    return 'down'
  return ''

def solve():
  ans = 1
  N = int(input())
  A = list(map(int, input().split()))
  ud = ''
  for i in range(N-1):
    a = check(A[i],A[i+1])
    if ud=='' or ud==a:
      ud = a
      continue
    if a=='':
      continue
    ans += 1
    ud = ''
  return ans
print(solve())

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = []
i = 0
while i < N - 1:
    if A[i] != A[i+1]:
        B.append(A[i])
    i += 1
B.append(A[-1])

ans = 1
i = 0
while i < len(B) - 2:
    if (B[i + 1] - B[i]) * (B[i + 2] - B[i + 1]) < 0:
        ans += 1
        i += 2
    else:
        i += 1
print(ans)

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

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
i = 0
ans = 0
while i < N:
    j = i
    while j < N-1 and A[j]<=A[j+1]:
        j += 1
    k = i
    while k < N-1 and A[k]>=A[k+1]:
        k += 1
    ans += 1
    i = max(j, k) + 1
print(ans)

Solution (Python):




import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """6
1 2 3 2 2 1"""
        output = """2"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """9
1 2 1 2 1 2 1 2 1"""
        output = """5"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """7
1 2 3 2 1 999999999 1000000000"""
        output = """3"""
        self.assertIO(input, output)


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

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


if __name__ == "__main__":
    
    resolve()

Solution (Python):
from collections import deque

N = int(input())
A_deque = deque(int(e) for e in input().split())

section = list()
section.append(A_deque.popleft())
ans = 1
mode = 0

while 1:
    if len(A_deque) == 0:
        break
    elif mode == 0:
        next = A_deque.popleft()
        if section[-1] == next:
            section.append(next)
        elif section[-1] > next:
            section.append(next)
            mode = -1
        elif section[-1] < next:
            section.append(next)
            mode = 1
    elif mode == 1:
        next = A_deque.popleft()
        if section[-1] > next:
            section.clear()
            section.append(next)
            ans += 1
            mode = 0
        else:
            section.append(next)
    elif mode == -1:
        next = A_deque.popleft()
        if section[-1] < next:
            section.clear()
            section.append(next)
            ans += 1
            mode = 0
        else:
            section.append(next)

print(ans)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
i=0
a=1
t=0
while i<N-1:
    if A[i]<A[i+1]:
        if t==-1:
            a+=1
            t=0
        elif not t:
            t=1
    elif A[i]>A[i+1]:
        if t==1:
            a+=1
            t=0
        elif not t:
            t=-1
    i+=1
print(a)