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()))

cnt = 1
mode = 0

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

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


if __name__ == "__main__":
    resolve()

Solution (Python):

import sys


def solve(N: int, A: "List[int]"):
    ix = 0
    cnt = 0
    while(ix < N):
        nix = ix
        minx = 0
        while(nix+1 < N and A[nix] <= A[nix+1]):
            minx += 1
            nix += 1
        nix = ix
        maxx = 0
        while(nix+1 < N and A[nix] >= A[nix+1]):
            maxx += 1
            nix += 1
        ix = ix + max(minx, maxx) + 1
        
        cnt += 1

    print(cnt)

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(N, A)

if __name__ == '__main__':
    main()

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

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

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()][::-1]
count = 0
f = 0
c = a.pop()
for i in range(1,n):
  c,p = a.pop(),c
  if f == 0:
    if c < p:
      f = 1
    elif c > p:
      f = 2
  if f == 1:
    if c > p:
      count += 1
      f = 0
  if f == 2:
    if c < p:
      count += 1
      f = 0
print(count + 1)

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

ans = 0
l = 0
ud = 0
for a in A:
  if l==0:
    ans += 1
    l+=1
  elif l==1:
    if b<a:
      ud = 1
      l+=1
    elif b>a:
      ud = -1
      l+=1
  else:
    if b<a and ud == -1:
      ans += 1
      l = 1
    elif b>a and ud == 1:
      ans += 1
      l = 1
  b = a
print(ans)

Solution (Python):

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


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

count = 1
mode = None
for i in range(1, N):
    if A[i-1] == A[i]:
        continue

    if mode == None:
        if A[i-1] < A[i]:
            mode = 'inc'
        if A[i-1] > A[i]:
            mode = 'dec'
        continue

    if mode == 'inc' and A[i-1] < A[i]:
        continue

    if mode == 'dec' and A[i-1] > A[i]:
        continue

    count += 1
    mode = None

print(count)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
flag = "a"
count = 0
for i in range(1,N):
    if A[i-1]<A[i] and flag == "a":
        flag = "up"
    elif A[i-1]>A[i] and flag == "a":
        flag = "down"
    elif A[i-1]==A[i] and flag == "a":
        continue
    if A[i-1]<=A[i] and flag == "up":
        continue
    elif A[i-1]>=A[i] and flag == "down":
        continue
    elif A[i-1]<A[i] and flag == "down":
        count += 1
        flag = "a"
    elif A[i-1]>A[i] and flag == "up":
        count += 1
        flag = "a"

print(count+1)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
flag = None
ans = 0
for i in range(N-1):
    if flag is None:
        if A[i] < A[i+1]:
            flag = True
        elif A[i+1] < A[i]:
            flag = False
        continue
    if flag:
        if A[i+1] < A[i]:
            ans += 1
            flag = None
    elif not flag:
        if A[i] < A[i+1]:
            ans += 1
            flag = None
ans += 1
print(ans)

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

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    n = I()
    a = LI()
    u = [0]*n
    r = 1
    for l in range(n):
        if l == r:
            r += 1
        while r < n and a[r-1] <= a[r]:
            r += 1
        u[l] = r
    d = [0]*n
    r = 1
    for l in range(n):
        if l == r:
            r += 1
        while r < n and a[r-1] >= a[r]:
            r += 1
        d[l] = r
    i = 0
    for t in range(n):
        i = max(u[i],d[i])
        if i == n:
            print(t+1)
            return
    return

def B():

    return


def C():

    return


def D():

    return


def E():

    return


def F():

    return


if __name__ == "__main__":
    A()

Solution (Python):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    if N <= 2: return 1

    i = 0
    count = 0
    while i < N:
        ref = A[i]
        count += 1
        i += 1
        while i < N and A[i] == ref:
            i += 1
        if i < N:
            diff = A[i] - ref
            i += 1
            if diff > 0:
                while i < N and A[i-1] <= A[i]:
                    i += 1
            else:
                while i < N and A[i-1] >= A[i]:
                    i += 1
    return count

if __name__ == '__main__':
    print(main())

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

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

inc, dec = False, False
cnt = 1

for i in range(N-1):
  if A[i+1] > A[i]:
    inc = True
  elif A[i+1] < A[i]:
    dec = True
  
  if inc and dec:
    cnt += 1
    inc, dec = False, False

print(cnt)

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

if __name__ == '__main__':
    main()

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):
'''
左端から条件を満たすようにしていく
Image :
[success] + [Failure]
[success] + [success] + [failure]
'''
N = int(input())
para = [int(i) for i in input().split()]

log = [para[0]]
p = 1 
flag_a = False
flag_b = False 
cnt = 1
while p <= N -1:
    if not flag_a and not flag_b :
        if para[p] > log[0]: 
            flag_a = True
            log.append(para[p])
            p += 1
        elif para[p] < log[0] : 
            flag_b = True
            log.append(para[p]) 
            p += 1
        else :
            log.append(para[p])
            p += 1
            continue
        
    else :
        if flag_a:
            if para[p] >= log[-1] :
                log.append(para[p])
                p += 1
            else :
                log = [para[p]]
                flag_a = False
                cnt += 1 
        if flag_b :
            if para[p] <= log[-1] :
                log.append(para[p])
                p += 1
            else :
                log = [para[p]]
                flag_b = False
                cnt += 1
print(cnt)

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

l = len(a)
prev = None
c = 1
inc = None

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

Solution (Python):

import sys, math, fractions, itertools, collections, copy, heapq


def solve(n: int, a: "List[int]"):
    i = 0
    res = []
    tmp = [a[0]]
    flag = 0  
    while i < len(a)-1:
        if a[i+1] == a[i]:
            tmp.append(a[i+1])
        elif a[i+1] > a[i]:
            if flag != -1:
                tmp.append(a[i+1])
                flag = 1
            else:
                res.append(tmp)
                tmp = []
                tmp.append(a[i+1])
                flag = 0
        elif a[i+1] < a[i]:
            if flag != 1:
                tmp.append(a[i+1])
                flag = -1
            else:
                res.append(tmp)
                tmp = []
                tmp.append(a[i+1])
                flag = 0
        i += 1
    res.append(tmp)
    
    print(len(res))
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [ int(next(tokens)) for _ in range(N) ]  
    solve(N, A)

if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
a=l[0]
l.remove(a)
f='flat'
f2='first'
c=1
for i in l:
  if f2=='first':
    if a==i:
      pass
    elif a<i:
      f='up'
      f2='not first'
      a=i
    else:
      f='down'
      f2='not first'
      a=i
  else:
    if a==i:
      pass
    elif a<i:
      if f=='up':
        pass
        a=i
      else:
        f=='flat'
        f2='first'
        c+=1
        a=i
    else:
      if f=='down':
        pass
        a=i
      else:
        f=='flat'
        f2='first'
        c+=1
        a=i
print(c)

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

def main():
    N = int(input())
    a = list(map(int, input().split()))

    state = 0
    pre = a[0]
    count = 0
    i = 1
    for i in range(1, N):
        tmp = a[i] - pre
        if state == 0: 
            if tmp > 0:
                state = 1
            elif tmp < 0:
                state = -1
        elif state == 1: 
            if tmp < 0:
                state = 0
                count += 1
        else:   
            if tmp > 0:
                state = 0
                count += 1
        pre = a[i]
    count += 1
    print(count)

if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
Flag = None
pre = A[0]
ans = 1
for i in range(1, N):
    a = A[i]
    if a < pre:
        if Flag == "P":
            ans += 1
            Flag = None
        else:
            Flag = "M"
    elif a > pre:
        if Flag == "M":
            ans += 1
            Flag = None
        else:
            Flag = "P"
    pre = a

print(ans)

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

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

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

if __name__ == '__main__':
    main()

Solution (Python):

N = int(input())
A = list(map(int, input().split()))
count = 1
dec = False
inc = False
if N <= 2:
    print('1')
    exit()
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:
        count += 1
        dec = False
        inc = False
print(str(count))

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

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

Solution (Python):
n = int(input())
num_list = [ int(v) for v in input().split() ]
cut_list = []
status = 0
for i in range(n-1):
    if status == 0:
        if num_list[i+1] > num_list[i]:
            status = 1
        elif num_list[i+1] < num_list[i]:
            status = -1
    elif status == 1:
        if num_list[i+1] < num_list[i]:
            cut_list.append(i)
            status = 0
    elif status == -1:
        if num_list[i+1] > num_list[i]:
            cut_list.append(i)
            status = 0

print(len(cut_list)+1)

Solution (Python):
from collections import deque
n=int(input())
D=deque(list(map(int,input().split())))
pop=D.popleft
A=pop()
memo=0
ans=0
for _ in range(n-1):
    a=pop()
    if memo==0:
        if A>a:
            memo=-1
        elif A<a:
            memo=1
        else:
            pass
    elif memo==1:
        if A>a:
            ans+=1
            memo=0
        else:
            pass
    else:
        if A<a:
            ans+=1
            memo=0
        else:
            pass
    A=a
print(ans+1)

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


flag = None
count = 0
for i in range(1, N):
    if A[i] - A[i - 1] == 0:
        pass
    if A[i] - A[i - 1] > 0: 
        if flag == True: 
            pass
        elif flag == False: 
            flag = None
        elif flag == None: 
            
            count += 1
            flag = True
    if A[i] - A[i - 1] < 0:
        if flag == False:
            pass
        elif flag == True:
            flag = None
        elif flag == None:
            
            count += 1
            flag = False
    
if flag == None:
    count += 1
print (count)

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

Solution (Python):
n,*l=map(int,open(0).read().split())
a=b=0
c=0
l+=[0]
while True:
	while l[a]<=l[a+1] and a!=n-1:
		a+=1
	while l[b]>=l[b+1] and b!=n-1:
		b+=1
	a=b=max(a,b)+1
	c+=1
	if a==n-1:
		c+=1
		break
	elif a==n:
		break
print(c)

Solution (Python):




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

cnt = 0
reset = True
flag = True
tmp = A[0]
for i in range(1,N):
    if reset:
        if A[i] > tmp:
            flag = True
            reset = False
        elif A[i] < tmp:
            flag = False
            reset = False
    else:
        if flag and A[i] < tmp:
            cnt += 1
            reset = True
        elif not(flag) and A[i] > tmp:
            cnt += 1
            reset = True
    tmp = A[i]
        
    
    
  
print(cnt+1)

Solution (Python):
N = int(input())
if N == 1:
  print(1)
  exit()
A = [int(i) for i in input().split()]
ans = 1
if A[1] - A[0] > 0:
  pre = 1
elif A[1] - A[0] == 0:
  pre = 0
else:
  pre = -1

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

Solution (Python):
a = int(input())
l = list(map(int,input().split()))
s = 1
t = 0
for i,j in zip(l,l[1:]):
    if i < j and t == -1:
        t=0
        s+=1
    elif i > j and t == 1:
        t = 0
        s+=1
    elif t ==0:
        if i < j:
            t = 1
        elif i > j:
            t = -1
        

    
print(s)

Solution (Python):
N = int(input())
aLst = list(map(int, input().split()))
zougen = ''

cnt = 0
for i in range(N - 1):
    if zougen == 'U':
        if aLst[i] > aLst[i + 1]:
            cnt += 1
            zougen = ''
    elif zougen == 'D':
        if aLst[i] < aLst[i + 1]:
            cnt += 1
            zougen = ''
    else:
        if aLst[i] > aLst[i + 1]:
            zougen= 'D'
        elif aLst[i] < aLst[i + 1]:
            zougen = 'U'
print(cnt + 1)

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

ans = 1
cnt = 0

for i in range(1, N):
    if A[i-1] < A[i]:
        if cnt == 0:
            f = True
            cnt = 1
        if f == False:
            f = True
            cnt = 0
            ans += 1
    elif A[i-1] > A[i]:
        if cnt == 0:
            f = False
            cnt = 1
        if f == True:
            f = False
            cnt = 0
            ans += 1

print(ans)

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

ans = 1
up = down = False
for a,b in zip(A,A[1:]):
    if a<b:
        if down:
            ans += 1
            up = down = False
        else:
            up = True
            down = False
    elif a>b:
        if up:
            ans += 1
            up = down = False
        else:
            up = False
            down = True
print(ans)

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

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

for i in range(N):
    if i == 0:
        ans += 1
        check = 0
        l.append(A[i])
    else:
        if len(l) == 0:
            ans += 1
            check = 0
            l.append(A[i])
        elif len(l) == 1:
            l.append(A[i])
            if A[i] - A[i-1] > 0:
                check = 1
            elif A[i] - A[i-1] < 0:
                check = -1
            else :
                check = 0
        else:
            if A[i] - A[i-1] == 0:
                l.append(A[i])
                pass
            elif A[i] - A[i-1] > 0 and check >= 0:
                check = 1
                l.append(A[i])
            elif A[i] - A[i-1] > 0 and check < 0:
                check = 1
                l = [A[i]]
                ans += 1
            elif A[i] - A[i-1] < 0 and check <= 0:
                check = -1
                l.append(A[i])
            elif A[i] - A[i-1] < 0 and check > 0:
                check = -1
                l = [A[i]]
                ans += 1






print(ans)

Solution (Python):
import sys, math, itertools, bisect, copy, re, heapq
from collections import Counter, deque, defaultdict
from itertools import accumulate, permutations, combinations, takewhile, compress, cycle






INF = float('inf')
MOD = 10 ** 9 + 7
EPS = 10 ** -7
sys.setrecursionlimit(1000000)








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


mode = 0
cnt = 0

last = A[0]
for a in A:
    
    if mode == 0:
        if last < a:
            mode = +1
        elif last > a:
            mode = -1
        else:
            pass
    elif mode == +1:
        if last <= a:
            pass
        else:
            cnt += 1
            mode = 0
    else:
        if last >= a:
            pass
        else:
            cnt += 1
            mode = 0
    last = a

print(cnt+1)

Solution (Python):
n = int(input())
A = tuple(map(int, input().split()))
direction = 0  
temp = A[0]
count = 1

for i in A:
    if direction == 0:
        if i > temp:
            temp = i
            direction = 1
        elif i < temp:
            temp = i
            direction = 2
        else:
            temp = i

    elif direction == 1:
        if i >= temp:
            temp = i
        else:
            temp = i
            count += 1
            direction = 0

    elif direction == 2:
        if i <= temp:
            temp = i
        else:
            temp = i
            count += 1
            direction = 0

print(count)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
sums = 0
b = 0
count = 1
for i in range(len(a)-1):
  sums += a[i+1] - a[i]
  
  if sums >= 0 and sums < b or b > 0 and sums < 0:
    count += 1
    sums = 0
  elif sums <= 0 and sums > b or b < 0 and sums > 0:
    count += 1
    sums = 0
  b = sums
  
print(count)

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

ap = a[0]
count = 0
l = 0
for i in range(n):
    l += 1
    if mt + lt == 0:
        if a[i]-ap > 0:
            mt = True
        elif a[i]-ap < 0:
            lt = True
    if mt:
        if a[i]-ap <0:
            lt = True
            mt = False
            if l > 1:
                count += 1
                l = 0
                lt = False
            else:
                l = 1
    if lt:
        if a[i]-ap > 0:
            mt = True
            lt = False
            if l > 1:
                mt = False
                count += 1
                l = 0
            else:
                l = 1
    ap = a[i]
print(count+1)

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

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

last = A[0]
direction = 0

answer = 1
for a in A[1:]:
    if direction == 0:
        if last < a:
            direction = 1
        elif last > a:
            direction = -1
        last = a
        continue
    if direction == 1:
        if last <= a:
            last = a
            continue
        else:
            last = a
            direction = 0
            answer += 1
            continue
    if direction == -1:
        if last >= a:
            last = a
            continue
        else:
            last = a
            direction = 0
            answer += 1
            continue

print(answer)

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

if n == 1:
    print("1")
else:
    if a[0] > a[1]:
        now = -1
    elif a[0] < a[1]:
        now = 1
    else:
        now = 0
        
    for i in range(1,n-1):
        if now == -1:
            if a[i] < a[i+1]:
                counter += 1
                now = 0
        elif now == 1:
            if a[i] > a[i+1]:
                counter += 1
                now = 0
        else:
            if a[i] > a[i+1]:
                now = -1
            elif a[i] < a[i+1]:
                now = 1
    print(counter)

Solution (Python):



import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7


n = int(sys.stdin.readline())
a = list(map(int,sys.stdin.readline().split()))
ans = 1
x = a[0]
flg = 0
for i in range(1,n):
    if flg == 0:
        if x < a[i]:
            flg = 1
        elif x > a[i]:
            flg = -1
    elif flg == 1:
        if x > a[i]:
            flg = 0
            ans += 1
    elif flg == -1:
        if x < a[i]:
            flg = 0
            ans += 1
    x = a[i]
print(ans)

Solution (Python):



import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7


n = int(sys.stdin.readline())
a = list(map(int,sys.stdin.readline().split()))
ans = 1
x = a[0]
flg = 0
for i in range(1,n):
    if flg == 0:
        if x < a[i]:
            flg = 1
        elif x > a[i]:
            flg = -1
    if flg == 1:
        if x > a[i]:
            flg = 0
            ans += 1
    elif flg == -1:
        if x < a[i]:
            flg = 0
            ans += 1
    x = a[i]
print(ans)

Solution (Python):
def array_split(array):
    array_len=len(array)
    res=[];ans=[]
    s=0 
    roop=0
    while roop!=array_len:
        if ans==[]:
            ans.append(array[roop])
            roop+=1
        elif s==0 and ans[-1]>array[roop]:
            ans.append(array[roop])
            s=2
            roop+=1
        elif s==0 and ans[-1]<array[roop]:
            ans.append(array[roop])
            s=1
            roop+=1
        elif s==0 and ans[-1]==array[roop]:
            ans.append(array[roop])
            roop+=1
        elif s==1:
            if ans[-1]<=array[roop]:
                ans.append(array[roop])
                roop+=1
            else:
                res.append(ans)
                s=0
                ans=[]
        elif s==2:
            if ans[-1]>=array[roop]:
                ans.append(array[roop])
                roop+=1
            else:
                res.append(ans)
                s=0
                ans=[]
    res.append(ans)
    return res
n=int(input())
a=list(map(int,input().split()))
print(len(array_split(a)))

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

if N ==1 or N ==2:
    print(1)
    exit()
t = 0
c =1
for i in range(1,N):

    if A[i]-A[i-1]>0:
        if t == -1:
            c +=1
            t = 0
        else:
            t = 1
    elif A[i]-A[i-1]<0:
        if t == 1:
            c +=1
            t = 0
        else:
            t = -1    
print(c)

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

Solution (Python):
def main():
    n = int(input())
    ls = [int(x) for x in input().split()]
    if n==1:
        print(1)
    else:
        right,left = 0,1
        cnt = 0
        flg = 0
        while 1:
            now = ls[left]
            nex = ls[right]   
            if now<nex:
                if flg ==-1:
                    cnt +=1
                    flg = 0
                else:
                    flg = 1
                left +=1
                right +=1
            elif nex==now:
                left +=1
                right +=1         

            else:
                if flg ==1:
                    cnt += 1
                    flg = 0
                else:
                    flg = -1
                left +=1
                right +=1

            if right==n-1:
                cnt +=1
                break
        print(cnt)

if __name__ == '__main__':
    main()

Solution (Python):

N = int(input())
A = list(map(int, input().split()))
f = 0
c = 1
i = 0
while i < N-1:
    if f == 0:
        if A[i] < A[i+1]:
            f = 1
        elif A[i] > A[i+1]:
            f = -1
    elif f == 1:
        if A[i] > A[i+1]:
            c += 1
            f = 0
    elif f == -1:
        if A[i] < A[i+1]:
            c += 1
            f = 0
    i += 1
print(c)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=[]
c=[]
if n==1:
        print(1)

else:
    for i in range(n-1):
            if a[i]!=a[i+1]:
                    b.append(a[i])

    b.append(a[n-1])
    ans=1

    for i in range(len(b)-1):
            c.append(b[i+1]-b[i])

    d=c[0]
    for i in range(1,len(b)-1):
            if d*c[i]>=0:
                    d=c[i]
            else:
                    ans+=1
                    d=0
    print(ans)

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
in_cnt = 0
de_cnt = 0
now = -1
for ind,a in enumerate(A[:-1]):
    if(A[ind+1]>a):
        if(now==-1):
            in_cnt+=1
            now = 1
        elif(now==0):
            now = -1
    elif(A[ind+1]<a):
        if(now==-1):
            de_cnt+=1
            now = 0
        elif(now==1):
            now = -1
print(in_cnt+de_cnt+(1 if now==-1 else 0))

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

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