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):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import groupby, accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
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()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()
A = [k for k, g in groupby(A)]
N = len(A)
i = 1
ans = 0

while i < N-1:
	if A[i-1] < A[i] > A[i+1] or A[i-1] > A[i] < A[i+1]:
		ans += 1
		i += 1
	i += 1

print(ans+1)

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

Solution (Python):
import sys


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


sys.setrecursionlimit(10 ** 9)


def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = [A[0]] + [y for x, y in zip(A, A[1:]) if x != y]
    seq = []
    answer = 1
    for x in B:
        if len(seq) < 2:
            seq.append(x)
            continue
        a, b = seq[-2:]
        if a > b > x or a < b < x:
            seq.append(x)
        else:
            answer += 1
            seq = [x]

    print(answer)


if __name__ == "__main__":
    main()

Solution (Python):
import sys


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


sys.setrecursionlimit(10 ** 9)


def main():
    N = int(input())
    A = list(map(int, input().split()))
    answer = 1
    last = A[0]
    diff = 0
    for i in range(1, N):
        if A[i] - last > 0:
            d = 1
        elif A[i] - last < 0:
            d = -1
        else:
            d = 0
        last = A[i]
        if diff == 0:
            diff = d
        elif d == 0:
            continue
        else:
            if diff == d:
                continue
            else:
                answer += 1
                diff = 0
    print(answer)


if __name__ == "__main__":
    main()

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

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

p = 0
m = 0
cnt = 0
for i in range(N-1):
    if(A[i] < A[i+1]):
        p = 1
    elif(A[i] == A[i+1]):
        continue
    else:
        m = 1
    if(p == m == 1):
        cnt += 1
        p = 0
        m = 0
print(cnt+1)

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

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

judge = 'reset'
cnt = 1


for i in range(N-1):
    if A[i] < A[i+1]:
        if judge == 'reset':
            judge = 'high'
        elif judge == 'low':
            judge = 'reset'
            cnt += 1
    elif A[i] > A[i+1]:
        if judge == 'reset':
            judge = 'low'
        elif judge == 'high':
            judge = 'reset'
            cnt += 1

print(cnt)

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

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

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


A = []
idx = 0
while idx < N:
    A.append(tmp_A[idx])
    while idx + 1 < N and tmp_A[idx] == tmp_A[idx + 1]:
        idx += 1
    idx += 1


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

    ans = 0
    idx = 0
    while idx + 1 < len(diff):
        if diff[idx] != diff[idx + 1]:
            ans += 1
            idx += 1
        idx += 1

    print(ans + 1)

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

judge = 'equal'
cnt = 1
for i in range(n-1):
  if a[i] < a[i+1]:
    if judge == 'equal':
      judge = 'high'
    elif judge == 'low':
      judge = 'equal'
      cnt += 1
  elif a[i] > a[i+1]:
    if judge == 'equal':
      judge = 'low'
    elif judge == 'high':
      judge = 'equal'
      cnt += 1

      
print(cnt)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n = int(readline())
a = list(map(int, readline().split()))
cnt = 1
flag = True
memo = 0
for i in range(1, n):
    if flag:
        if a[i] > a[i - 1]:
            memo = 1
            flag = False
        if a[i] < a[i - 1]:
            flag = False
            memo = 0
        else:
            continue
    else:
        if memo == 1 and a[i] < a[i - 1]:
            cnt += 1
            flag = True
        elif memo == 0 and a[i] > a[i - 1]:
            cnt += 1
            flag = True
print(cnt)

Solution (Python):
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return " ".join(map(str,x))
def max2(x): 
    return max(map(max,x))
def In(x,a): 
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans

"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

n = onem()

a = l()

co = 0

po = a[0]

fl = 0

for i in range(1,n):
    if fl != 0:
        if fl == 1:
            if po > a[i]:
                co += 1
                fl = 0
            po = a[i]
        else:
            if po < a[i]:
                co += 1
                fl = 0
            po = a[i]


    else:
        if po > a[i]:
            fl = -1
        elif po < a[i]:
            fl = 1
        else:
            fl = 0
        po = a[i]

print(co+1)

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

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

print(ans+1)

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

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()
b = 0 
ans = 0
arr = []
for aa in a:
    if len(arr) == 0:
        arr.append(aa)
        ans += 1
        b = 0
        continue
    if b == 0:
        arr.append(aa)
        if arr[-2] < arr[-1]:
            b = 1
        elif arr[-2] > arr[-1]:
            b = 2
    if b == 1:
        if arr[-1] <= aa:
            arr.append(aa)
        else:
            arr = [aa]
            ans += 1
            b = 0
    if b == 2:
        if arr[-1] >= aa:
            arr.append(aa)
        else:
            arr = [aa]
            ans += 1
            b = 0
print(ans)

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

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

B = []
C = []
for i in range(n):
    if len(C) == 0 or len(C) == 1:
        C.append(A[i])
    else:
        if C[-2] <= C[-1] and C[-1] < A[i]:
            C.append(A[i])
        elif C[-2] >= C[-1] and C[-1] > A[i]:
            C.append(A[i])
        elif (C[-2] <= C[-1] and C[-1] > A[i]) or (C[-2] >= C[-1] and C[-1] < A[i]):
            B.append(C)
            C = []
            C.append(A[i])
if len(C) >= 1:
    B.append(C)
print(len(B))

Solution (Python):
from collections import deque

n = int(input())
a = list(map(int, input().split()))
b = []
if n >= 2:
    for i in range(n - 1):
        if a[i] != a[i + 1]:
            b.append(a[i])
    if b[len(b) - 1] != a[n - 1]:
        b.append(a[n - 1])
    l = len(b)
    m = 0
    ans = 1
    for i in range(l - 1):
        if m == 0:
            if b[i] < b[i + 1]:
                m = 1
            else:
                m = -1
        elif m == 1:
            if b[i] > b[i + 1]:
                ans += 1
                m = 0
        else:
            if b[i] < b[i + 1]:
                ans += 1
                m = 0
else:
    ans = 1
print(ans)

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

Solution (Python):
N=int(input())
A=[int(i) for i in input().split()]
ans=1
pal=0
d=0
for i in range(N-1):
    if pal:
        if d==1:
            if not (A[i]<=A[i+1]):
                pal=0
                ans+=1
                d=0
        if d==-1:
            if not (A[i]>=A[i+1]):
                pal=0
                ans+=1
                d=0
    else:
        if A[i]<A[i+1]:
            d=1
            pal=1
        elif A[i]>A[i+1]:
            d=-1
            pal=1
print(ans)

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

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



print(ans)

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

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


print(ans)

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

Solution (Python):
N = int(input())
a = [int(i) for i in input().split()]
result = 1
delta = 0
for i in range(1,N) :
    if a[i-1] < a[i]:
        ndelta = 1
    elif a[i-1] > a[i]:
        ndelta = -1
    else:
        ndelta = 0
        
    if delta and ndelta and delta != ndelta:
        result += 1
        delta = 0
    elif not delta:
        delta = ndelta
    
print(result)

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

Solution (Python):
def main():
    n, *a = map(int, open(0).read().split())
    c = 1
    f = 0
    d = [1,-1]
    for i, j in zip(a, a[1:]):
      if i == j:
        continue
      g = d[i < j]
      if f * g == -1:
        f = 0
        c += 1
      else:
        f = g
    
    print(c)


if __name__ == '__main__':
    main()

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

    c = 1
    f = 0
    d = [-1,1]
    for i, j in zip(a, a[1:]):
      if i == j:
        continue
      else:
        g = d[i < j]
        if f * g == -1:
          f = 0
          c += 1
        else:
          f = g
    
    print(c)


if __name__ == '__main__':
    main()

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

    c = 1
    f = 0
    d = {True: 1, False: -1}
    for i, j in zip(a, a[1:]):
      if i == j:
        continue
      else:
        g = d[i < j]
        if f * g == -1:
          f = 0
          c += 1
        else:
          f = g
    
    print(c)


if __name__ == '__main__':
    main()

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

count = 1
first = True
isIncrease = False
for i in range(1, len(A)):
    if first == True:
        if A[i] > A[i - 1] :
            isIncrease = True
            first = False
        elif A[i] < A[i - 1] : 
            isIncrease = False
            first = False
    else:
        if isIncrease == True and A[i] < A[i - 1]:
            isIncrease = False
            count += 1
            first = True
        elif isIncrease == False and A[i] > A[i - 1]:
            isIncrease = True
            count += 1
            first = True
            
print(count)

Solution (Python):
N = int(input())
arr = list(map(int, input().split()))
if N == 1:
    print(1)
    exit()
start = 0
ans = 0
while start < N - 1:
    up_count = 0
    for i in range(start,N-1):
        if arr[i] <= arr[i+1]:
            up_count += 1
        else:
            break
    down_count = 0
    for i in range(start,N-1):
        if arr[i] >= arr[i+1]:
            down_count += 1
        else:
            break
    start += max(up_count,down_count) + 1
    if start == N - 1:
        ans += 2
        break
    ans += 1
print(ans)

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

count = 1
state = 0  

for i, item in enumerate(a):
    
    if i < len(a) -1:
        if a[i] < a[i+1]:
            if state == 0:
                state = 1
            elif state == 2:
                state = 0
                count += 1
        elif a[i] > a[i+1]:
            if state == 0:
                state = 2
            elif state == 1:
                state = 0
                count += 1

print(count)

Solution (Python):
import sys

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

check = 0
check_tmp = 0
cnt = 1
tmp = 0
for i in range(n):
    if i == 0:
        tmp = a[i]    
    else:
        if tmp < a[i]:
            check = 1
        elif tmp > a[i]:
            check = -1
        else:
            check = 0

        if check_tmp != 0:
            if check == 1 and check_tmp == -1:
                cnt += 1
                check_tmp = 0
            elif check == -1 and check_tmp == 1:
                cnt += 1
                check_tmp = 0
            tmp = a[i]
        else:
            check_tmp = check
            tmp = a[i]
        

print("{}".format(cnt))

Solution (Python):
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') 
def gcd(a,b):return fractions.gcd(a,b) 
def lcm(a,b):return (a*b) // fractions.gcd(a,b) 
def iin(): return int(sys.stdin.readline()) 
def ifn(): return float(sys.stdin.readline()) 
def isn(): return sys.stdin.readline().split() 
def imn(): return map(int, sys.stdin.readline().split()) 
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) 
def fmn(): return map(float, sys.stdin.readline().split()) 
def iln(): return list(map(int, sys.stdin.readline().split())) 
def iln_s(): return sorted(iln()) 
def iln_r(): return sorted(iln(), reverse=True) 
def fln(): return list(map(float, sys.stdin.readline().split())) 
def join(l, s=''): return s.join(l) 
def perm(l, n): return itertools.permutations(l, n) 
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) 
def comb(l, n): return itertools.combinations(l, n) 
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) 
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 
def m_add(a,b): return (a+b) % MOD
def print_list(l): print(*l, sep='\n')

def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime


N = iin()
A = iln()

decreased = False
increased = False
count = 1
for i in range(1, N):
    if not decreased and not increased:
        if A[i-1] > A[i]: decreased = True
        if A[i-1] < A[i]: increased = True
    else: 
        if decreased:
            if A[i-1] < A[i]:
                count += 1
                decreased = False
        else:
            if A[i-1] > A[i]:
                count += 1
                increased = False
print(count)

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

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

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

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

print(ans)

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

prev = As[0]
status = 0 
cnt = 1

for a in As[1:]:
    if status == 0: 
        if prev > a:
            status = -1
        elif prev < a:
            status = 1
    elif status == 1 :  
        if prev > a:
            status = 0
            cnt += 1
    else:               
        if prev < a:
            status = 0
            cnt += 1
    prev = a

print(cnt)

Solution (Python):

import sys

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

def main():
    N = INT()
    A = LI()

    last = A[0]
    is_increase = None
    answer = 0
    for i in range(1,N):
        if last == A[i]:
            continue
        if is_increase == None:
            is_increase = last<=A[i]
            last = A[i]
            continue

        if is_increase and last<=A[i]:
            last = A[i]
            continue
        
        if not is_increase and last>=A[i]:
            last = A[i]
            continue

        if (is_increase and last>A[i]) or (not is_increase and last<A[i]):
            is_increase = None
            last = A[i]
            answer += 1

    answer += 1
    print(answer)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
l = []
for i in range(1,N):
    if (A[i-1] < A[i]):
        l.append("<")
    elif (A[i-1] != A[i]):
        l.append(">")

a = [0]*len(l)

for i in range(len(l)-1):
    if (a[i] == 0):
        if (l[i] != l[i+1]):
            a[i+1] = 1
    
print(sum(a)+1)

Solution (Python):
def main():
    length = int(input())
    sequence = list(map(int, input().split()))
    answer = 0
    index = 0
    while index < length:
        while index + 1 < length and sequence[index] == sequence[index + 1]:
            index += 1
        if index + 1 < length and sequence[index] < sequence[index + 1]:
            while index + 1 < length and sequence[index] <= sequence[index + 1]:
                index += 1
        elif index + 1 < length and sequence[index + 1] < sequence[index]:
            while index + 1 < length and sequence[index + 1] <= sequence[index]:
                index += 1
        answer += 1
        index += 1
    print(answer)


if __name__ == '__main__':
    main()

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

""" 広義 """

cnt = 1
flag = 0 
for i,a in enumerate(A):
    if i == 0:
        prev = a
        continue

    if flag == 0:
        if prev == a: pass
        elif prev > a: flag = -1
        else: flag = 1
    elif flag > 0:
        if prev <= a: pass
        else:
            cnt += 1
            flag = 0
    else:
        if prev >= a: pass
        else:
            cnt += 1
            flag = 0
    prev = a

print(cnt)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
if n < 3:
    print(1)
    exit(0)
cnt = 1

increase = a[1] - a[0];
for i in range(2, len(a)):
    new_inc = a[i] - a[i-1];
    if increase * new_inc < 0:
        cnt += 1
        increase = 0
    elif increase == 0:
        increase = new_inc
print(cnt)

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

ans = 1
prev = A[0]
direction = 0
for a in A[1:]:
    if direction == 1:
        if prev > a:
            ans += 1
            direction = 0
            prev = a
            continue
    if direction == -1:
        if prev < a:
            ans += 1
            direction = 0
            prev = a
            continue
    if direction == 0:
        if prev < a:
            direction = 1
        elif prev > a:
            direction = -1
    prev = a
print(ans)

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

check = ''
ans = 0

for i in range(n-1):
    diff = A[i] - A[i+1]
    if check == '':
        if diff < 0:
            check = '+'
        elif diff > 0:
            check = '-'
    elif check != '':
        if check == '+' and diff > 0:
            ans += 1
            check = ''
        elif check == '-' and diff < 0:
            ans += 1
            check = ''

print(ans+1)

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

for i in range(N-1):
    d = A[i+1] - A[i]
    if direction == 0:
        if d > 0:
            direction = 1
        elif d < 0:
            direction = -1
    else:
        if direction == 1 and d < 0:
            cnt += 1
            direction = 0
        elif direction == -1 and d > 0:
            cnt +=1
            direction = 0
print(cnt)

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

direc = ""
ans = 0
for i in range(N-1):
  d = A[i+1] - A[i]
  if direc == "":
    if d > 0:
      direc = "up"
    elif d < 0:
      direc = "down"
  else:
    if direc == "up" and d < 0:
      ans += 1
      direc = ""
    elif direc == "down" and d > 0:
      ans += 1
      direc = ""
print(ans+1)

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

p = 0
count = 1

def start(L, A):
    global p
    if L[A+1] > L[A]:
        p=1
    elif L[A+1] < L[A]:
        p=2
    else:
        p=0

def upcount(L, A):
    global p, count
    if L[A+1] >= L[A]:
        p=1
    else:
        p=0
        count += 1

def downcount(L, A):
    global p, count
    if L[A+1] <= L[A]:
        p=2
    else:
        p=0
        count += 1

for i in range(n):
    if i == n-1:
        break
    elif p == 0:
        start(l, i)
    elif p == 1:
        upcount(l, i)
    else:
        downcount(l, i)

print(count)

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

if __name__ == "__main__":
    main()

Solution (Python):
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    ans = 0
    i = 0
    while i < N:
        pos1, pos2 = i, i
        while pos1+1 < N and A[pos1] <= A[pos1+1]:
            pos1 += 1
        while pos2+1 < N and A[pos2] >= A[pos2+1]:
            pos2 += 1
        ans += 1
        i = max(pos1, pos2) + 1
    print(ans)


if __name__ == '__main__':
    main()

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
        pos1, pos2 = i, i
        while pos1+1 < N and A[pos1] <= A[pos1+1]:
            pos1 += 1
        while pos2+1 < N and A[pos2] >= A[pos2+1]:
            pos2 += 1
        ans += 1
        i = max(pos1, pos2) + 1
    print(ans)


if __name__ == '__main__':
    main()

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

count=1
flag=True
for i in range(n-1):
    if i==0:
        ans=a[i+1]-a[i]

    if not flag:
        ans=a[i+1]-a[i]
        flag=True

    if (ans<=0 and a[i+1]-a[i]<=0) or (ans>=0 and a[i+1]-a[i]>=0):
        if ans!=0:
            continue
        elif a[i+1]-a[i]!=0:
            ans=a[i+1]-a[i]
        else:
            continue
            


    else:
        count+=1
        flag=False

print(count)

Solution (Python):
n = int(input())
ans = list(map(int,input().split()))
key = ans[0]
up_key = True
down_key = True
count = 0
for i in range(n):
    if key < ans[i]:
        down_key = False
    if key > ans[i]:
        up_key = False
    if up_key == False and down_key == False:
        count += 1
        up_key = True
        down_key = True
    key = ans[i]
print(count+1)

Solution (Python):
N = int(input())
A = list(map(int, input().split(' ')))
count = 0
tmp_arr = []
inc = -1
for i, a in enumerate(A):
    if N-1 == i:
        count += 1
    if len(tmp_arr) == 0:
        tmp_arr.append(a)
        continue
    if inc == 0 and a > tmp_arr[-1]:
        tmp_arr = []
        inc = -1
        count += 1
    elif inc == 1 and a < tmp_arr[-1]:
        tmp_arr = []
        inc = -1
        count += 1
    else:
        if a > tmp_arr[-1]:
            inc = 1
        elif a < tmp_arr[-1]:
            inc = 0
    tmp_arr.append(a)
print(count)

Solution (Python):




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

ans = 1
mm = 0
i = 0
flg = 0
while(i<N-1):
    if A[i]>A[i+1]:
        if mm>0:
            ans += 1
            flg = 1
            
            
        mm = -1
        if flg:
            flg = 0
            mm = 0

    elif A[i]<A[i+1]:
        if mm<0:
            ans += 1
            flg = 1
            
            

        mm = 1
        if flg:
            flg = 0
            mm = 0
    i+=1
    
print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
i = 0
count = 1
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
        if i < N-1:
            count += 1
            i += 1
    
    elif A[i] > A[i+1]:
        while i < N-1 and A[i] >= A[i+1]:
            i += 1
        if i < N-1:
            count += 1
            i += 1
print(count)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
i = 0
ret = 1
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
    if i < N-1:
      ret += 1
      i += 1
  elif A[i] > A[i+1]:
    while i < N-1 and A[i] >= A[i+1]:
      i += 1
    if i < N-1:
      ret += 1
      i += 1
print(ret)

Solution (Python):
N=int(input())
f=0
fnew=0
A=list(map(int,input().split()))
cnt=0
i=0
l=0
r=0
downflag=0
upflag=0
ma=A[i]
mi=A[i]
while i<N:
 if ma>A[i]:
  downflag=1
 elif ma<A[i]:
  upflag=1
 if (upflag==1)and(A[i]>ma):
  ma=A[i]
 if (downflag==1)and(mi>A[i]):
  mi=A[i]
 if (ma+mi)!=(A[l]+A[i]):
  ma=A[i]
  mi=ma
  cnt=cnt+1
  l=i
  i=i-1
  upflag=0
  downflag=0
 i=i+1
print(cnt+1)