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):
def main():
	N = int(input())
	A = list(map(int, input().split()))

	is_increasing = True
	is_first = True
	ans = 1

	for i in range(1, N):
		if is_first == True:
			if A[i - 1] < A[i]:
				is_increasing = True
				is_first = False
			elif A[i - 1] > A[i]:
				is_increasing = False
				is_first = False
		else:
			if is_increasing == True and A[i - 1] > A[i]:
				ans += 1
				is_first = True
			elif is_increasing == False and A[i - 1] < A[i]:
				ans += 1
				is_first = True

	print(ans)



 
if __name__ == "__main__":
  	main()

Solution (Python):
import sys


stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


def main():
    n = ni()
    A = nl()
    D = [A[i + 1] - A[i] for i in range(n - 1)]

    for d in D:
        if d != 0:
            if d > 0:
                state = 1
            else:
                state = -1
            break
    else:
        print(1)
        return

    ans = 1
    cnt = 0

    for d in D:
        if d > 0:
            c_state = 1
            cnt += 1
        elif d < 0:
            c_state = -1
            cnt += 1
        else:
            continue
        if c_state != state:
            if cnt != 1:
                ans += 1
                cnt = 0
            state = c_state
    print(ans)


if __name__ == '__main__':
    main()

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

list = []
sim=0
for i in range(N-1):
  if A[i]<A[i+1]:
    list.append(1)
  if A[i]>A[i+1]:
    list.append(0)
  if A[i]==A[i+1]:
    sim+=1
    
i = 0
count = 0

while i<N-2-sim:
  if list[i] != list[i+1]:
    count += 1
    i+=1
  i+=1

print(count + 1)

Solution (Python):








inc = False
dec = False
val = 0
ans = 1
n = int(input())
a = list(map(int, input().split()))

for i in range(1, n):
    if inc:
        if a[i-1] > a[i]:
            ans += 1
            inc = False
    elif dec:
        if a[i-1] < a[i]:
            ans+=1
            dec =False
    else:
        if a[i-1] < a[i]:
            inc = True
        elif a[i-1] > a[i]:
            dec = True
print(ans)

Solution (Python):
def solve(): 

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

    i = 0
    result = 0

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

    print(result)    

if __name__ == "__main__":
    solve()

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

ans = 0

up = None

for i in range(1, N):
    if up == None:
        if not A[i] == A[i - 1]:
            up = A[i] > A[i - 1]
            
    elif up:
        if A[i] < A[i - 1]:
            ans += 1
            up = None

    else:
        if A[i] > A[i - 1]:
            ans += 1
            up = None

print(ans + 1)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
ans = 1
cur = 'N'
i = 1
while i < N:
    if cur == 'U':
        if A[i] < A[i-1]:
            ans += 1
            cur = 'N'
        i += 1
    elif cur == 'D':
        if A[i] > A[i-1]:
            ans += 1
            cur = 'N'
        i += 1
    elif cur == 'N':
        if A[i] > A[i-1]:
            cur = 'U'
        elif A[i] < A[i-1]:
            cur = 'D'
        i += 1
print(ans)

Solution (Python):
n = int(input())
l = list(map(int,input().split()))
ans = 0
now = 0
m = 0
c = 0
for i in l:
    if now == 0:
        now = i
    else:
        if m == 0:
            if now < i:
                m = 1
            elif now > i:
                m = -1
        elif m == 1:
            if now > i:
                ans += 1
                m = 0
            
        else:
            if now < i:
                ans += 1
                m = 0
        now = i

print(ans+1)

Solution (Python):
import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

n = inint()
A = inintl()

prev = A[0]
ans = 1
sign = 0

for i in range(1,n):
    if A[i] == prev:
        continue
    if A[i] > prev:
        if sign == -1:
            ans += 1
            prev = A[i]
            sign = 0
            continue
        else:
            sign = 1
            prev = A[i]
            continue
    if A[i] < prev:
        if sign == 1:
            ans += 1
            prev = A[i]
            sign = 0
            continue
        else:
            sign = -1
            prev = A[i]
            continue

print(ans)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
b = A[0]
att = ''
cnt = 1
for a in A:
    
    if a != b:
        if att == '':
            if a > b:
                att = '+'
            elif a < b:
                att = '-'
        elif att == '+':
            if a < b:
                cnt += 1
                att = ''
        elif att == '-':
            if a > b:
                cnt += 1
                att = ''
    b = a
print(cnt)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
num = 0
count = 1
mode = 0
while num < n - 1:
    if a[num] == a[num + 1]:
        num += 1
    elif a[num] > a[num + 1]:
        num += 1
        if mode == -1:
            pass
        elif mode == 0:
            mode = -1
        else:
            count += 1
            mode = 0
    else:
        num += 1
        if mode == 1:
            pass
        elif mode == 0:
            mode = 1
        else:
            count += 1
            mode = 0
print(count)

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

l = []
ans = 1
flag = True
for i in range(1, len(a)):
    num = a[i] - a[i - 1]
    if num > 0:
        num = 1
        l.append(num)
    elif num < 0:
        num = -1
        l.append(num)
    else:
        pass
if len(l) > 1:
    for i in range(len(l) - 1):
        if l[i] != l[i + 1]:
            if flag == True:
                ans += 1
                flag = False
            else:
                flag = True
        else:
            flag = True
print(ans)

Solution (Python):
import sys

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

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

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

cnt = 1

flag = 0
for i in range(n-1):
    if lis[i+1] - lis[i] >0:
        if flag == 1:
            pass
        elif flag == -1:
            flag = 0
            cnt += 1
        elif flag == 0:
            flag = 1
    elif lis[i+1] - lis[i] < 0:
        if flag == 1:
            flag = 0
            cnt += 1
        elif flag == -1:
            pass
        elif flag == 0:
            flag = -1
    elif lis[i+1] - lis[i] == 0:
        pass
    

print(cnt)

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

tmp=[]
for i in range(1,n):
    if a[i]>a[i-1]:
        tmp.append(1)
    elif a[i]==a[i-1]:
        tmp.append(0)
    else:
        tmp.append(-1)

flg=0
swi=0
for j in range(len(tmp)):
    if swi==1:
        flg=tmp[j]
        swi=0
        continue
 
    if flg==0:
        flg=tmp[j]
    elif flg==1:
        if tmp[j]==-1:
            cnt+=1
            flg=-1
            swi=1
    elif flg==-1:
        if tmp[j]==1:
            cnt+=1
            flg=1
            swi=1

print(cnt+1)

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

  ans = 1
  x=y=0
  for i in range(1, N):

    if A[i - 1] < A[i]:
      x = 1

    if A[i - 1] > A[i]:
      y = 1

    if x and y:
      ans += 1
      x = y = 0

  print(ans)

main()

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

ans=1
x=y=0
c=A[0]

for i in range(1,N):
    if c < A[i]:
        x=1
    elif c>A[i]:
        y=1
    
    if x==1 and y==1:
        ans+=1
        x=y=0
    
    c=A[i]

print(ans)

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

cnt = 1
first = True
increasing = True

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

print(cnt)

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

uf,df = True,True
x = a[0]
for ai in a:
    
    if x > ai:
        uf = False
    if x < ai:
        df = False
    if (uf or df) is False:
        ans += 1
        uf,df, = True,True
    x = ai
print(ans+1)

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

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()))
is_up=False
is_even=True
pre_a=a[0]
cnt=1
for aa in a:
  if is_even and pre_a != aa:
    is_even=False
    is_up=pre_a < aa
  if not is_even:
    if (is_up and pre_a > aa) or (not is_up and pre_a < aa):
      is_even=True
      cnt+=1
  pre_a=aa
print(cnt)

Solution (Python):
N,*A = map(int, open(0).read().split())
ans = 1
if N <= 2:
    print(1)
else:
    if A[1] > A[0]:
        dif = 1
    elif A[1] < A[0]:
        dif = -1
    else:
        dif = 0
    for i in range(2,N):
        if A[i] > A[i-1]:
            if dif == -1:
                ans += 1
                dif = 0
            elif dif == 0:
                dif = 1
        elif A[i] < A[i-1]:
            if dif == 1:
                ans += 1
                dif = 0
            elif dif == 0:
                dif = -1
    print(ans)

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


print(counter)

Solution (Python):
import sys
sys.setrecursionlimit(1000000)
N = int(input())
A = list(map(int, input().split()))

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

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

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
nb = 0
da = 0
for i in range(len(A)-1):
    if A[i] > A[i+1] and da > 0:
        nb += 1
        da = 0
    elif A[i] < A[i+1] and da < 0:
        nb += 1
        da = 0
    else:
        if A[i+1] - A[i] != 0:
            da = A[i+1] - A[i]
print(nb + 1)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = []
flg = 'hi'
for i in range(n-1):
    if a[i] > a[i+1]:
        if flg != 'pl':
            flg = 'mi'
        else:
            b.append(i)
            flg = 'hi'
    elif a[i] < a[i+1]:
        if flg != 'mi':
            flg = 'pl'
        else:
            b.append(i)
            flg = 'hi'
print(len(b)+1)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
zougen = 2
ans = 0

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

if n == 1:
    ans = 1

print(ans)

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

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


dp = (1, 1)
for i in range(1, N):
    if A[i] > A[i-1]:
        dp = min(dp)+1, min(dp[0]+1, dp[1])
    elif A[i] == A[i-1]:
        pass
    else:
        dp = min(dp[0], dp[1]+1), min(dp)+1
    
print(min(dp))

Solution (Python):


n = int(input())
a = list(map(int, input().split()))
d = [0 for _ in range(n-1)]
for i in range(n-1):
    d[i] = a[i+1]-a[i]


ans = 1 
state = 'flat'
for i in range(n-1):
    if i == 0:
        if d[i] > 0:
            state = 'up'    
        elif d[i] < 0:
            state = 'down'

    else:
        if d[i] > 0:
            if state == 'flat':
                state = 'up'
            elif state == 'down':
                state = 'flat'
                ans += 1

        elif d[i] < 0:
            if state == 'flat':
                state = 'down'
            elif state == 'up':
                state = 'flat'
                ans += 1

print(ans)

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

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

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

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

print(cnt)

Solution (Python):


n = int(input())
a = list(map(int, input().split()))
d = [0 for _ in range(n-1)]
for i in range(n-1):
    d[i] = a[i+1]-a[i]

ans = 1 
flg = [False, False]

for i in range(n-1):
    if d[i] > 0:
        flg[0] = True
    elif d[i] < 0:
        flg[1] = True

    if flg[0] == flg[1] == True:
        ans += 1
        flg = [False, False]

print(ans)

Solution (Python):

import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
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 S(): return input()
def LS(): return input().split()


INF = float('inf')

n = I()
a = LI()
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):
N = int(input())
L = list(map(int, input().split()))
if N == 1 or N == 2:
  print(1)
else:
  pre = L[0]
  greater = False
  less = False
  ans = 1
  for l in L[1:]:
    if greater:
      if pre > l:
        ans += 1
        greater = False
    elif less:
      if pre < l:
        ans += 1
        less = False
    else:
      if pre < l:
        greater = True
      elif pre > l:
        less = True
    pre = l
    
  print(ans)

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

if N<=2:
    ans=1
else:
    ans=1
    count=(A[0]<A[1])-(A[0]>A[1])
    i=2
    while i<N:
        if count*((A[i-1]<A[i])-(A[i-1]>A[i]))<0:
            ans+=1
            count=0
        else:
            if count==0:
                count=(A[i-1]<A[i])-(A[i-1]>A[i])
        i+=1

print(ans)

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

def main():
    n = int(input())
    a = list(map(int, 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, *a = map(int, open(0).read().split())
c = 1
d = [1] * n
a = [a[0]] + [a[i] for i in range(1, n) if a[i - 1] != a[i]]
for i in range(2, len(a)):
    if (a[i] - a[i - 1]) * (a[i - 1] - a[i - 2]) < 0 and d[i - 1]:
        c += 1
        d[i] = 0
print(c)

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

ans = 1
flg = -1
for i in range(1, n):
    if a[i-1] == a[i]:
        continue
    
    elif a[i-1] < a[i]:
        if flg == 1 or flg == -1:
            flg = 1
            continue
        else:
            ans += 1
            flg = -1

    else:
        if flg == 0 or flg == -1:
            flg = 0
            continue
        else:
            ans += 1
            flg = -1

print(ans)

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


ans = 1


flags = [False, False]

for i in range(N - 1):
    da = a[i + 1] - a[i]
    if da > 0:
        flags[0] = True
    elif da < 0:
        flags[1] = True

    if flags[0] == True and flags[1] == True:
        ans += 1
        
        flags = [False, False]

print(ans)

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

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

    cnt=1
    reset_flag=False
    for i in range(n-1):
        if reset_flag:
            if a[i+1]-a[i]>0:
                status="U"
                reset_flag=False
            elif a[i+1]-a[i]<0:
                status="D"
                reset_flag=False
            else:
                continue
        elif status=="D" and a[i+1]-a[i]>0:
            cnt+=1
            reset_flag=True
        elif status=="U" and a[i+1]-a[i]<0:
            cnt+=1
            reset_flag=True
    print(cnt)

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

now = False
count = 1
for i in range(N-1):
    if A[i] < A[i+1]:
        if now == 1:
            pass
        elif now == -1:
            count += 1
            now = False
        else:
            now = 1
    elif A[i] > A[i+1]:
        if now == 1:
            count += 1
            now = False
        elif now == -1:
            pass
        else:
            now = -1

print(count)

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

ans = 1
flags=[False,False]

for i in range(N-1):
  da = a[i+1] - a[i]
  if da > 0:
    flags[0] = True
  elif da < 0:
    flags[1] = True
    
  if all(flags):
    ans += 1
    flags = [False,False]
print(ans)

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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
a = 0
S = 0
num = 1
for i in range(1,N):
    if A[i] - A[i - 1] == 0:
        a = 0
    elif A[i] - A[i - 1] > 0:
        a = 1
    else:
        a = 2
    if S == 0:
        S = a
    elif (S != a) and (a != 0):
        num += 1
        S = 0
print(num)

Solution (Python):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from collections import deque
def main():
    n, *a = map(int, read().split())
    a = deque(a)
    r = 1
    flag = False
    muki = 0
    n1 = a.popleft()
    while a:
        n2 = a.popleft()
        if flag:
            if n1 < n2 and muki:
                n1 = n2
            elif n1 > n2 and not muki:
                n1 = n2
            elif n1 == n2:
                pass
            else:
                r += 1
                flag = False
                n1 = n2
        else:
            if n1 < n2:
                flag = True
                muki = 1
                n1 = n2
            elif n1 > n2:
                flag = True
                muki = 0
                n1 = n2
    print(r)

if __name__ == '__main__':
    main()

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

F=0
K=1
b=A[0]

for a in A:
    if F==0:
        if b<a:
            F=1
            b=a
        elif b>a:
            F=-1
            b=a
    elif F==1 and b>a:
        K+=1
        F=0
    elif F==-1 and b<a:
        K+=1
        F=0
    b=a
print(K)

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

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

i = 0
c = 0
while i < n:
    if i == n - 1:
        c += 1
        break
    if a[i] == a[i+1]:
        i += 1
    elif a[i] < a[i+1]:
        while i < n - 1 and a[i] <= a[i+1]:
            i += 1
        c += 1
        i += 1
    elif a[i] > a[i+1]:
        while i < n - 1 and a[i] >= a[i+1]:
            i += 1
        c += 1
        i += 1
print(c)

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

print(ans)

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = int(readline())
    a = list(map(int, readline().split()))

    i = 0
    ans = 0

    while i < n:
        s = a[i]
        j = i
        k = i
        while j < n and s <= a[j]:
            s = a[j]
            j += 1
        while k < n and s >= a[k]:
            s = a[k]
            k += 1
        i = max(j, k)
        ans += 1

    print(ans)


if __name__ == '__main__':
    main()

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

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

def linput(ty=int, cvt=list):
	return cvt(map(ty,input().split()))

def vinput(rep=1, ty=int, cvt=list):
	return cvt(ty(input().rstrip()) for _ in "*"*rep)

def gcd(a: int, b: int):
	while b: a, b = b, a%b
	return a

def lcm(a: int, b: int):
	return a * b // gcd(a, b)

def main():
	N = int(input())
	vA = linput()
	
	
			
	vC =[a-pp for a,pp in zip(vA[1:],vA)
		 if a!=pp]
		
	res = 0
	pp = 0

	for c in vC:
		if pp==0:
			pp = 1 if c>0 else -1
			res += 1
		if c*pp<0:
			pp = 0
	
	if pp==0:
		res += 1
	
	
	print(res)

if __name__ == "__main__":
	main()

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

def linput(ty=int, cvt=list):
	return cvt(map(ty,input().split()))

def vinput(rep=1, ty=int, cvt=list):
	return cvt(ty(input().rstrip()) for _ in "*"*rep)

def gcd(a: int, b: int):
	while b: a, b = b, a%b
	return a

def lcm(a: int, b: int):
	return a * b // gcd(a, b)

def main():
	N = int(input())
	vA = linput()
	
	
	
	if N==1:
		print(1)
		return
		
	vC =[a-pp for a,pp in zip(vA[1:],vA)]
		
	res = 0
	pp = 0
	cnt = 0
	for c in vC:
		if c==0: continue
		if pp==0:
			pp = 1 if c>0 else -1
			cnt = 1
			res += 1
			continue
		if c>0 and pp<0:
			cnt = 1
			pp = 0
			continue
		if c<0 and pp>0:
			cnt = 1
			pp = 0
			continue
		cnt += 1
	
	if pp==0:
		res += 1
	
	
	print(res)

if __name__ == "__main__":
	main()

Solution (Python):

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


res = 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

    res+=1
    i+=1
   


print(res)

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

N = int(input())
A = list(map(int,input().split()))
B = []
for i in range(1,N):
    if A[i] - A[i-1] != 0:
        B.append(A[i] - A[i-1])
cnt = 0
for i in range(1,len(B)):
    if i >1:
        if B[i-1]*B[i-2] < 0 :
            continue
    if B[i]*B[i-1]<0:
        cnt += 1
        B[i]=0
        
print(cnt+1)

Solution (Python):

 

 
import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
import copy
 

 










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

ans = 1
state = "normal"

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


print(ans)

Solution (Python):

import sys
from collections import deque, defaultdict
from math import sqrt, factorial





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

    ans = 0
    i = 0
    while i < n:
        ans += 1
        if i + 1 == n: break
        else:
            if a[i] < a[i+1]:
                while i + 1 < n and a[i] < a[i+1]:
                    i += 1
                i += 1
            else:
                while i + 1 < n and a[i] > a[i+1]:
                    i += 1
                i += 1
    print(ans)



    

t = 1

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


"""



"""

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

i = 1
ans = 1
flg = 0
for i, j in zip(A[:-1],A[1:]):
    if i < j:
        if flg == 0:
            flg = 1
        elif flg == 2:
            ans += 1
            flg = 0
    if i > j:
        if flg == 0:
            flg = 2
        elif flg == 1:
            ans += 1
            flg = 0
print(ans)

Solution (Python):


from itertools import groupby
N = int(input())
A = list(map(int, input().split()))
B = []
for key, v in groupby(A):
    B.append(key)


diff = []
for i in range(len(B) - 1):
    diff.append(1 if B[i] < B[i + 1] else -1)


nums = []
for d, v in groupby(diff):
    nums.append(len(list(v)))


is_can_del = 0
ans = 0
for n in nums:
    if n == 1 and is_can_del == 1:
        is_can_del = 0
    else:
        ans += 1
        is_can_del = 1
if is_can_del == 0:
    ans += 1
print(ans)

Solution (Python):

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

ans = 1
f = 0

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

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

ans = 1
flg = 0
rev = 0 
for c,n in zip(A, A[1:]):
    
    if not flg:
        if c < n:
            
            flg = 1
        elif c > n:
            
            flg = 1
            rev = 1
    else:
        if rev:
            c = -c
            n = -n

        if c > n:
            ans += 1
            flg = 0
            rev = 0
            
print(ans)

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


flag = None
up = False
down = False
ans = 1
base = A[0]

for i in range(1, n):
    if base < A[i]:
        up = True
        base = A[i]
    elif base > A[i]:
        down = True
    if (up == True) and (down == True):
        up = False
        down = False
        ans += 1
    base = A[i]
    
print(ans)