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

plus = False
minus = False
ans = 0
for i in range(N-1):
    if not(plus) and not(minus):
        if A[i] < A[i+1]:
            plus = True
        elif A[i] > A[i+1]:
            minus = True

    else:
        if plus:
            if A[i] <= A[i+1]:
                continue
            else:
                ans += 1
                plus = False
        else:
            if A[i] >= A[i+1]:
                continue
            else:
                ans += 1
                minus = False
print(ans+1)

Solution (Python):





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

inc = -1
num = 1

prev = a[0]
for x in a:
    if x>prev:
        if inc == -1:
            inc = 1
        elif inc == 0:
            num += 1
            inc = -1
    elif x < prev:
        if inc == -1:
            inc = 0
        elif inc == 1:
            num += 1
            inc = -1
    prev = x

print(num)

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

ans = 0
u_d = -1
for i, a in enumerate(a_list):
    if i == 0:
        tmp = a
        continue
    
    if a-tmp == 0:
        tmp = a
        continue

    if u_d == -1 and a > tmp:
        u_d = 1 
        tmp = a
        continue
    elif u_d == -1 and a < tmp:
        u_d = 0 
        tmp = a
        continue
    
     
    if a > tmp:
        if u_d == 1:
            tmp = a
            
            continue
        elif u_d == 0:
            ans += 1
            u_d = -1
            tmp = a

    elif a < tmp:
        if u_d == 0:
            tmp = a
            
            continue
        elif u_d == 1:
            ans += 1
            u_d = -1
            tmp = a

    
    

print(ans+1)

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

order = 0
subs = []
tmp = []

prev = 0
d = []
t = [l[0]]
for x in range(len(l)-1):
  a,b = l[x],l[x+1]
  c = a-b
  
  if c == 0:
    t.append(b)
  elif prev == 0:
    t.append(b)
    prev = c
  elif (c < 0  and prev < 0) or (c > 0 and prev > 0):
    t.append(b)
  else:
    prev = 0
    d.append(t)
    t = [b]
d.append(t)

print(len(d))

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

order = 0
subs = []
tmp = []

prev = 0
d = []
t = [l[0]]
for x in range(len(l)-1):
  a,b = l[x],l[x+1]
  c = a-b
  
  if c == 0:
    t.append(b)
  elif prev == 0:
    t.append(b)
    prev = c
  elif (c < 0  and prev < 0) or (c > 0 and prev > 0):
    t.append(b)
  else:
    prev = 0
    d.append(t)
    t = [b]
d.append(t)

print(len(d))

Solution (Python):
















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

plusOrMinus = 0
num = arrA[0]
ans = 0
for i in range(N-1):
    if plusOrMinus == "+":
        if arrA[i]>arrA[i+1]:
            ans += 1
            plusOrMinus = 0
    elif plusOrMinus == "-":
        if arrA[i]<arrA[i+1]:
            ans += 1
            plusOrMinus = 0
    else:
        if arrA[i]<arrA[i+1]:
            plusOrMinus = "+"
        elif arrA[i]>arrA[i+1]:
            plusOrMinus = "-"
        else:
            pass

print(ans+1)

Solution (Python):
def myAnswer(N:int,A:list) -> int:
   if(N == 1): return 1
   pre = A.pop(0)
   counter = 1
   state = 0 
   for a in A:
      if(pre == a):
         pre = a
         continue
      if(state == 0):
         if(pre < a):
            state = 1
         else:
            state = 2
      elif(pre < a and state == 2):
         counter += 1
         state = 0
      elif(pre > a and state == 1):
         counter += 1
         state = 0
      pre = a
   return counter

def modelAnswer():
   return
def main():
   N = int(input())
   A = list(map(int,input().split()))
   print(myAnswer(N,A[:]))
if __name__ == '__main__':
   main()

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
count = 1
status = 0

for i in range(N-1):
  if (A[i+1] - A[i]) == 0:
    continue
  elif (A[i+1] - A[i]) > 0:
    if status == 0 or status == 1:
      status = 1
      continue
    else:
      count += 1
      status = 0
      
  elif (A[i+1] - A[i]) < 0:
    if status == 0 or status == -1:
      status = -1
      continue
    else:
      count += 1
      status = 0
print(count)

Solution (Python):
o = b = c = 0;input()
for i, x in enumerate(input().split()):
    x = int(x)
    s = x - b if i else 0
    d = (s>0)-(s<0)
    if abs(d - o) > 1:
        c += 1
        o = 0
    else:
      o = d or o
    b = x
print(c+1)

Solution (Python):


n = int(input())

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

ans = 1

flag = None
pre = nums[0]
for i in range(n):
    if pre == nums[i]:
        continue
    elif pre < nums[i]:
        if flag == False:
            ans += 1
            flag = None
        else:
            flag = True
    else:
        if flag == True:
            ans += 1
            flag = None
        else:
            flag = False
    pre = nums[i]
print(ans)

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

status = 0
cnt = 1
for i in range(n - 1):
    
    
    if a[i] == a[i + 1]:
        
        continue
    elif a[i] < a[i + 1]:
        if status == 0 or status == 1:
            
            status = 1
            continue
        else:
            
            status = 0
            cnt += 1
    else:
        if status == 0 or status == 2:
            
            status = 2
            continue
        else:
            
            status = 0
            cnt += 1

print(cnt)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
ans=1
bef=a[0]
flag=None 
for i in range(1,n):
    
    if bef<a[i]:
        if flag==False:
            ans+=1
            flag=None
        else:
            flag=True
    elif bef>a[i]: 
        if flag:
            ans+=1
            flag=None
        else:
            flag=False
    bef=a[i]
print(ans)

Solution (Python):


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

inc = None
c = 0
 
for i in range(n-1):
    if inc == None:
        if A[i] == A[i+1]:
            pass
        elif A[i] < A[i+1]:
            inc = True
        else:
            inc = False
    elif inc:
        if A[i] > A[i+1]:
            c += 1
            inc = None
    else:
        if A[i] < A[i+1]:
            c += 1
            inc = None

print(c+1)

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

Solution (Python):
from sys import stdin
import sys
import math

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

ASC = -1
DESC = 1

flag_asc_desc = 0
count = 1
prev = 0
for i in A:
    if prev == 0:
        prev = i
        flag_asc_desc = 0
        continue
    
    if flag_asc_desc == ASC and i >= prev:
        prev = i
        continue
    elif flag_asc_desc == ASC and i < prev:
        prev = i
        flag_asc_desc = 0
        count += 1
    elif flag_asc_desc == DESC and i <= prev:
        prev = i
        continue
    elif flag_asc_desc == DESC and i > prev:
        prev = i
        flag_asc_desc = 0
        count += 1
    elif i == prev:
        prev = i
        continue
    elif i < prev:
        flag_asc_desc = DESC
        prev = i
    elif i > prev:
        flag_asc_desc = ASC
        prev = i
    else:
        print("error")

print(count)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
ans=1
c=0
pas=a[0]
for i in range(1,n):
  now=a[i]
  if c==0:
    if pas<now:
      c=1
    if pas>now:
      c=2
  if c==1:
    if pas>now:
      ans+=1
      c=0
  if c==2:
    if pas<now:
      ans+=1
      c=0
  pas=now
print(ans)

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

last = alist[0]
count = 1
status = 0  
for a in alist[1:]:
    if (status == 1 and last > a) or (status == 2 and last < a):
        count += 1
        status = 0
    elif status == 0 and last > a:
        status = 2
    elif status == 0 and last < a:
        status = 1
    last = a
print(count)

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

ans = 1
inc = 0

if N == 1:
    print(ans)

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

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
if len(set(l))==1:print(1);exit()
from itertools import groupby as gb
l=[a for a,b in gb(l)]
ans=1
now=0  if l[0]<l[1] else 1
pre=l[0]
n=len(l)
for i in range(n):
    if pre<l[i] and now:
        if i==n-1:print(ans+1);exit()
        else:
            now=0  if l[i]<l[i+1] else 1
            ans+=1
    elif pre>l[i] and 1-now:
        if i==n-1:print(ans+1);exit()
        else:
            now=0  if l[i]<l[i+1] else 1
            ans+=1
    pre=l[i]
print(ans)

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

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

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
if len(set(l))==1:print(1);exit()
from itertools import groupby as gb
l=[a for a,b in gb(l)]
ans=1
now=0  if l[0]<l[1] else 1
pre=l[0]
n=len(l)
for i in range(n):
    if pre<l[i] and now:
        if i==n-1:print(ans+1);exit()
        else:
            now=0  if l[i]<l[i+1] else 1
            ans+=1
    elif pre>l[i] and 1-now:
        if i==n-1:print(ans+1);exit()
        else:
            now=0  if l[i]<l[i+1] else 1
            ans+=1
    pre=l[i]
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
flag = None 
temp = A[0]
result = 1
for a in A:
    if a == temp:
        continue
    if flag == None:
        if a > temp:
            flag = True
        else:
            flag = False
    elif (flag and a < temp) or (not flag and a > temp):
        flag = None
        result += 1
    temp = a
print(result)

Solution (Python):
def main():
    import sys
    n=int(input())
    a=list(map(int, input().split()))
    ud=[]
    ans=1
    cnt=[0,0]
    if n==1:
        print(ans)
        sys.exit()
    for i in range(n-1):
        if a[i]==a[i+1]:
            ud.append('eq')
        elif a[i]<a[i+1]:
            ud.append('up')
        elif a[i]>a[i+1]:
            ud.append('down')
    if len(set(ud))==1 or (len(set(ud))==2 and 'eq' in set(ud)):
        print(ans)
        sys.exit()
    if ud[0]=='up':
        cnt[0]=1
    elif ud[0]=='down':
        cnt[1]=1
    for i in range(1,n-1):
        if (cnt[0]==cnt[1]==0 or cnt[0]!=0) and ud[i]=='up':
            cnt[0]+=1
        elif (cnt[0]==cnt[1]==0 or cnt[1]!=0) and ud[i]=='down':
            cnt[1]+=1
        elif cnt[0]!=0 and ud[i]=='down':
            ans+=1
            cnt[0]=0
        elif cnt[1]!=0 and ud[i]=='up':
            ans+=1
            cnt[1]=0
        elif cnt[0]!=0 and cnt[1]==0 and i==n-2:
            ans+=1
        elif cnt[1]!=0 and cnt[0]==0 and i==n-2:
            ans+=1
    print(ans)
    
if __name__=='__main__':
    main()

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

flag = 'n' 
counter = 1
for i,l in enumerate(li):
    if i == len(li)-1:
        break
    if flag == 'n':
        if l > li[i+1]:
            flag = 'd'
        elif l < li[i+1]:
            flag = 'u'
    elif flag == 'u':
        if l > li[i+1]:
            counter += 1
            flag = 'n'
            new_flag = True
    elif flag == 'd':
        if l < li[i+1]:
            counter += 1
            flag = 'n'
            new_flag = True
    
print(counter)

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

cur=a_list[0]
pat=0 
ans=0
for a in a_list:
    if a==cur:
        continue
    if pat==0:
        if cur<a:
            pat=1
        else:
            pat=2
        cur=a
        continue
    if pat==1 and cur>a:
        ans+=1
        pat=0
    elif pat==2 and cur<a:
        ans+=1
        pat=0

    cur=a
print(ans+1)

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

p = 0
cnt = 1

for i in range(N-1):
    if a[i] == a[i+1]:
        continue
    elif p == 0 and a[i] > a[i+1]:
        p = 1
    elif p == 0 and a[i] < a[i+1]:
        p = -1
    elif p == 1 and a[i] < a[i+1]:
        cnt += 1
        p = 0
    elif p == -1 and a[i] >a[i+1]:
        cnt += 1
        p = 0

print(cnt)

Solution (Python):
N = int(input())
an = list(map(int,input().split(' ')))
cnt = 1
is_increased = True
is_init = True
for i in range(1,N):
    if is_init:
        if an[i] > an[i-1]:
            is_increased = True
        elif an[i] < an[i-1]:
            is_increased = False
        elif an[i] == an[i-1]:
            continue
        is_init = False
    else:
        if is_increased:
            if an[i] >= an[i-1]:
                continue
            else:
                is_init = True
                cnt +=1
        else:
            if an[i] <= an[i-1]:
                continue
            else:
                is_init = True
                cnt +=1
print(cnt)

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

Solution (Python):
N = int(input())
line = [int(x) for x in input().split(' ')]
count = 1
prev = line[0]
i = 0
while i < len(line):
  if prev == line[i]:
    i += 1
    continue
  elif prev < line[i]:
    for j in range(i,len(line)):
      if line[j] < prev:
        count+=1
        break
      prev = line[j]
    i = j
  elif prev > line[i]:
    for j in range(i,len(line)):
      if line[j] > prev:
        count += 1
        break
      prev = line[j]
    i = j
  prev = line[i]
  i+=1
print(count)

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

ans=1
tmp=-1

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

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

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

f=''
ans=1
for i in range(1,n):
    if a[i-1]==a[i]:
        continue
    if a[i-1] < a[i]:
        if f=='>':
            ans+=1
            f=''
        else:
            f='<'
    else:
        if f=='<':
            ans+=1
            f=''
        else:
            f='>'
print(ans)

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

for i in range(n-1):
    if a[i] < a[i+1]:
        frag = True
        break
    elif a[i] > a[i+1]:
        frag = False
        break
skip = False
ans = 1

for i in range(n-1):
    beforefrag = frag
    if a[i] > a[i+1]:
        frag = False
    elif a[i] < a[i+1]:
        frag = True
    else:
        continue

    if skip:
        skip = False
        continue

    if beforefrag != frag:
        skip = True
        ans += 1

print(ans)

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

Solution (Python):

import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():

    N=int(input())
    A=list(map(int,input().split()))
    state=0
    temp=A[0]
    cnt=1
    for i in range(1,N):
        if state==0:
            if A[i]>temp:
                state=1
                temp=A[i]
            elif A[i]<temp:
                state=-1
                temp=A[i]
            else:
                temp=A[i]

        elif state==1:
            if A[i]<temp:
                state=0
                cnt+=1
                temp=A[i]
            else:
                temp=A[i]
        else:
            if A[i] > temp:
                state = 0
                cnt += 1
                temp = A[i]
            else:
                temp = A[i]
    print(cnt)
resolve()

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

a = A[0]

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

print(ans)

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

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

ans = 1
up = None

for i in range(n-1):
    if up == None:
        if a[i] == a[i+1]:
            continue
        elif a[i] < a[i+1]:
            up = True
        else:
            up = False
    elif up and a[i] > a[i+1]:
        up = None
        ans += 1
    elif not up and a[i] < a[i+1]:
        up = None
        ans += 1
print(ans)

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

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

increase=None
ans=0

for i in range(n):
    if i!=n-1:
        if increase==None:
            if a[i]==a[i+1]:
                pass
            elif a[i]<a[i+1]:
                increase=True
            else:
                increase=False
        elif increase==True:
            if a[i]>a[i+1]:
                ans+=1
                increase=None
        else:
            if a[i]<a[i+1]:
                ans+=1
                increase=None
ans+=1
print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
p = a[0]
ans = 1
stat = 'None'
for i, ai in enumerate(a, 1):
    if stat == 'None':
        if ai < p:
            stat = 'Down'
        elif ai > p:
            stat = 'Up'
    else:
        if ai < p and stat == 'Up':
            ans += 1
            stat = 'None'
        elif ai > p and stat == 'Down':
            ans += 1
            stat = 'None'
    p = ai
print(ans)

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

Solution (Python):



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


ans = 0
pos = 0


while pos<N:
    pos_1 = pos
    pos_2 = pos

    
    while pos_1<N-1 and a_list[pos_1]<=a_list[pos_1+1]:
        pos_1 += 1

    
    while pos_2<N-1 and a_list[pos_2]>=a_list[pos_2+1]:
        pos_2 += 1

    large = max(pos_1, pos_2) + 1
    pos = large
    ans += 1


print(ans)

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

if N == 1:
    print(1)
elif N == 2:
    print(1)
else:
    A = [B[0]]
    cnt = 0
    for i in range(N - 1):
        if B[i] == B[i + 1]:
            pass
        else:
            A.append(B[i+1])

    i = 1
    ans = 1
    n = len(A)
    while i < n - 1:
        if A[i-1] < A[i] < A[i+1] or A[i-1] > A[i] > A[i+1]:
            pass
        else:
            i += 1
            ans += 1
        i += 1
    print(ans)

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

temp = 0
cnt = 0
for i in range(n-1):
    
    if A[i] < A[i+1]:
        if temp == 0:
            temp = 1
        elif temp == -1:
            temp = 0
            cnt += 1
        else:
            pass
    elif A[i] > A[i+1]:
        if temp == 0:
            temp = -1
        elif temp == 1:
            temp = 0
            cnt += 1
        else:
            pass
    else:
        pass
else:
    cnt += 1
print(cnt)

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

temp = 0
cnt = 0
for i in range(n-1):
    
    if A[i] < A[i+1]:
        if temp == 0:
            temp = 1
        elif temp == -1:
            temp = 0
            cnt += 1
        else:
            pass
    elif A[i] > A[i+1]:
        if temp == 0:
            temp = -1
        elif temp == 1:
            temp = 0
            cnt += 1
        else:
            pass
    else:
        pass
else:
    cnt += 1
print(cnt)

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

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

Solution (Python):
N = int(input())
a = [int(i) for i in input().split()]
count = 0
i = 0
while i<N-1:
    if a[i]<a[i+1]:
        i += 1
        while i<N-1:
            if a[i]<=a[i+1]:
                i+=1
            else:
                count+=1
                i+=1
                break
    elif a[i]>a[i+1]:
        i+=1
        while i<N-1:
            
            if a[i]>=a[i+1]: 
                i+=1
            else:
                count+=1
                i+=1
                break
    else:
        i+=1
        while i<N-1:
            if a[i]==a[i+1]:
                i+=1
            else:
                break
print(count+1)

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

ans = 0
plus = ""
for i in range(N):
  if plus != "":
    if plus:
      if A[i-1] > A[i]:
        ans += 1
        plus = ""
    else:
      if A[i-1] < A[i]:
        ans += 1
        plus = ""
  if i < N-1 and plus == "":
    if A[i] < A[i+1]:
      plus = True
    elif A[i] > A[i+1]:
      plus = False
print(ans+1)

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

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

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

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

change = 0  
ans = 1

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

print(ans)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
debugmode = False

n = INT()
a = LIST()
mode = 0 
ans = 1
f = a[0]
for i in range(1, n):
    if mode == 0 and a[i] == f:
        pass
    elif mode == 0 and a[i] < f:
        mode = 2
        f = a[i]
    elif mode == 0 and a[i] > f:
        mode = 1
        f = a[i]
    elif mode == 1 and a[i] == f:
        pass
    elif mode == 1 and a[i] > f:
        f = a[i]
    elif mode == 1 and a[i] < f:
        ans += 1
        mode = 0
        f = a[i]
    elif mode == 2 and a[i] == f:
        pass
    elif mode == 2 and a[i] > f:
        ans += 1
        mode = 0
        f = a[i]
    elif mode == 2 and a[i] < f:
        f = a[i]
print(ans)

Solution (Python):
def solve():
    n = int(input())
    a = list(map(int,input().split()))
    direction = 0 
    cnt = 1
    for i in range(1,n):
        if (a[i]-a[i-1])*direction<0:
            cnt+=1
            direction = 0
        else:
            if direction==0:
                direction = a[i]-a[i-1]
    print(cnt)
if __name__ == "__main__":
    solve()

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
if n<=2:print(1);exit()
ans=1
chk=0
left=0
while chk<n-2:
  if (l[left]-l[chk+1])*(l[chk+1]-l[chk+2])<0:ans+=1;chk+=2;left=chk
  else:chk+=1
print(ans)

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

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

Solution (Python):
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from queue import Queue


def input(): return sys.stdin.readline().strip()


def INT(): return int(input())


def MAP(): return map(int, input().split())


def LIST(): return list(map(int, input().split()))


sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    n = INT()
    a = LIST()

    ans = 0

    
    
    

    
    
    
    
    
    

    sign = 0

    for i in range(n-1):
        if a[i] < a[i+1] and sign == -1:
            ans += 1
            sign = 0

        elif a[i] > a[i+1] and sign == 1:
            ans += 1
            sign = 0

        elif sign == 0:
            if a[i] < a[i+1]:
                sign = 1
            elif a[i] > a[i+1]:
                sign = -1

    print(ans + 1)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from queue import Queue


def input(): return sys.stdin.readline().strip()


def INT(): return int(input())


def MAP(): return map(int, input().split())


def LIST(): return list(map(int, input().split()))


sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    n = INT()
    a = LIST()

    ans = 0

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

    if a[0] < a[1]:
        sign = 1
    elif a[0] > a[1]:
        sign = -1
    else:
        sign = 0

    for i in range(n-1):
        if a[i] < a[i+1] and sign == -1:
            ans += 1
            sign = 0

        elif a[i] > a[i+1] and sign == 1:
            ans += 1
            sign = 0

        elif sign == 0:
            if a[i] < a[i+1]:
                sign = 1
            elif a[i] > a[i+1]:
                sign = -1

    print(ans + 1)


if __name__ == '__main__':
    main()