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 math
n=input()
a=map(int, raw_input().split())
a=[a[i]-a[i-1] for i in range(1, len(a))]
a=[value/abs(value) for value in a if value]
cur=0
result=0
for value in a:
	if cur and cur!=value:
		cur=0
		result+=1
	else:
		cur=value
print result+1

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

ass = 0
k = a[0]
w = 0 
for i in range(n):
    if w == -1:
        if a[i] > a[i-1]:
            ass += 1
            w = 0
        
    elif w == 1:
        if a[i] < a[i-1]:
            ass += 1
            w = 0
    if w == 0:
        if i != n-1:
            if a[i] > a[i+1]:
                w = -1
            elif a[i] < a[i+1]:
                w = 1
print(ass+1)

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

Solution (Python):


def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    ans = 1
    flag = 0
    for i in range(1, n):
        if a_list[i] - a_list[i-1] > 0 and flag == 0:
            flag = 1
        elif a_list[i] - a_list[i-1] < 0 and flag == 0:
            flag = 2
        if a_list[i] - a_list[i-1] > 0 and flag == 2:
            ans += 1
            flag = 0
        elif a_list[i] - a_list[i-1] < 0 and flag == 1:
            ans += 1
            flag = 0
    print(ans)
    
if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
list_a = list(map(int, input().split()))
state = 0
count = 1
for i in range(n-1):
    if state == 0:
        if list_a[i] == list_a[i+1]:
            continue
        elif list_a[i] < list_a[i+1]:
            state = "plus"
        else:
            state = "minus"
    if state == "plus":
        if list_a[i] > list_a[i+1]:
            count += 1
            state = 0
        else:
            continue
    if state == "minus":
        if list_a[i] < list_a[i+1]:
            count += 1
            state = 0

print(count)

Solution (Python):
n = int(input())
lis = list(map(int,input().split()))
cou = 0
ans = 1
for i in range(n-1):
  r = lis[i+1]-lis[i]
  if r * cou < 0:
    ans += 1
    cou = 0
  elif r != 0:
    cou = r
print(ans)

Solution (Python):

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

result = 1
d = 0
for i in range(1, n):
    diff = a[i] - a[i - 1]
    if d == 0:
        d = diff
    elif d * diff < 0:
        result += 1
        d = 0

print(result)

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

res = 1
flag = 0
prev = a[0]
for i in a:
    
    if prev==i:
        tmp = 0
    
    elif prev<i:
        tmp = 1
    
    else:
        tmp = -1

    if tmp==1:
        if flag==-1:
            flag = 0
            res += 1
        else:
            flag = tmp
    elif tmp==-1:
        if flag==1:
            flag = 0
            res += 1
        else:
            flag = tmp

    
    prev = i

print(res)

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

def li(): return [int(x) for x in stdin.readline().split()]
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return [float(x) for x in stdin.readline().split()]
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(ns())
def nf(): return float(ns())

n = ni()
a = li()

a_diff = []
ans = 1
for i in range(n-1):
    a_diff.append(a[i+1] - a[i])
    
cur = 0
for i in range(n-1):
    if cur == 0:
        if a_diff[i] == 0:
            continue
        elif a_diff[i] > 0:
            cur = +1
        else:
            cur = -1
            
    elif cur == +1:
        if a_diff[i] >= 0:
            continue
        else:
            cur = 0
            ans += 1
            
    elif cur == -1:
        if a_diff[i] <= 0:
            continue
        else:
            cur = 0
            ans += 1
            
    else:
        print("バグってます／(^o^)＼")
            
        
print(ans)

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

Solution (Python):
n=int(input())
*m,=map(int,input().split())
l=[]
l=[m[0]]+[m[i] for i in range(1,n) if m[i]!=m[i-1]]

c=1
i=2

while i<len(l):
  if (l[i-2]<l[i-1] and l[i-1]>l[i]) or (l[i-2]>l[i-1] and l[i-1]<l[i]):
    c+=1
    i+=1
  i+=1

print(c)

Solution (Python):
n = int(input())
b = list(map(int, input().split()))
a = [b[0]]
for i in range(1, n):
    if a[-1] != b[i]:
        a.append(b[i])

ans = 1
inc = a[0] < a[min(1, n-1)]

n = len(a)
i = 1
while i < n:
    if (a[i-1] < a[i]) ^ inc:
        ans += 1
        inc = a[i] < a[min(i+1, n-1)]
        i += 2
    else:
        i += 1
print(ans)

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

t = a[0]
ge, le = False, False
i = 0
ans = 1
while i < n:
    if a[i] == t:
        pass
    if a[i] > t:
        if ge:
            pass
        else:
            if le:
                le = False
                ans += 1
            else:
                ge = True
    if a[i] < t:
        if le:
            pass
        else:
            if ge:
                ge = False
                ans += 1
            else:
                le = True

    t = a[i]
    i += 1
print(ans)

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

st = "non"
ans = 1
for i in range(1, N):
    if st == "non":
        if a[i-1] < a[i]:
            st = "inc"
        if a[i-1] > a[i]:
            st = "dec"
    elif st == "inc":
        if a[i-1] > a[i]:
            st = "non"
            ans += 1
    elif st == "dec":
        if a[i-1] < a[i]:
            st = "non"
            ans += 1
print(ans)

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

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

b = 0
ans = 1

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

Solution (Python):
import numpy as np

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

cnt = 1
direct = 0
temp = a[0]
for i in range(1,n):
    dif = a[i] - temp
    if dif != 0:
        if direct == 0:
            direct = np.sign(dif)
            
        elif np.sign(a[i] - temp) != direct:
            cnt+=1
            direct = 0
    temp = a[i]
print(cnt)

Solution (Python):
import numpy as np

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

count = 1
direction = 0
temp = A[0]
for i in range(1, N):
    diff = A[i] - temp
    if diff != 0:
        if direction == 0:
            direction = np.sign(A[i] - temp)
        else:
            if np.sign(A[i] - temp) != direction:
                count += 1
                direction = 0
    temp = A[i]

print(count)

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

def solve(N, A):
    s = 0
    result = 1
    for i in range(N-1):
        r = A[i+1] - A[i]
        if r * s < 0:
            result += 1
            s = 0
        elif r != 0:
            s = r
    return result

print(solve(N, A))

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

def solve(N, A):
    if N <= 2:
        return 1
    s = 0
    result = 1
    i = 0
    while i < N-1:
        r = A[i+1] - A[i]
        if (r > 0 and s < 0) or (r < 0 and s > 0):
            result += 1
            s = 0
        elif r != 0:
            s = r
        i += 1
    return result

print(solve(N, A))

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

flg=0
ans=1

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

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

	ans = 1

	def upper(x):
		if x[0] < x[1]:
			return True
		else:
			return False

	array = [b[0], b[1]]

	i = 2
	while i < len(b):
		u = upper(array)
		if (u and b[i] > array[-1]) or (not u and b[i] < array[-1]):
			array.append(b[i])
			i += 1
		else:
			ans += 1
			if i == len(b) - 1:
				break
			array = [b[i], b[i+1]]
			i += 2

	print(ans)

Solution (Python):
import sys

n = int(input())
array = [int(x) for x in input().split()]

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

tmp = array.pop(0)
tmp2 = array.pop(0)
mode = "plus" if tmp2 > tmp else "minus" if tmp2 != tmp else "equal"
tmp = tmp2

for i in array:
    if tmp < i:
        if mode == "minus":
            cnt += 1
            mode = "equal"
        elif mode == "equal":
            mode = "plus"
    elif tmp > i:
        if mode == "plus":
            cnt += 1
            mode = "equal"
        elif mode == "equal":
            mode = "minus"
    tmp = i
print(cnt)

Solution (Python):
def sign(x):
    if x == 0:
        return 0
    return x // abs(x)


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

    if n == 1:
        print(1)

    else:
        cnt = 1
        d = sign(a[1]-a[0])
        for i in range(n-1):
            dd = sign(a[i+1]-a[i])
            if d == 0:
                d = dd

            if d != 0 and dd != 0 and d != dd:
                cnt += 1
                if i < n-2:
                    d = sign(a[i+2]-a[i+1])

        print(cnt)


if __name__ == '__main__':
    main()

Solution (Python):

import sys

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

if N <= 2:
    print(1)
    sys.exit(0)

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

print(count+1)

Solution (Python):
def a_sorted_arrays(N, A):
    ans = 0  
    change = 0  
    for k in range(1, N):
        if change == 0:
            
            if A[k] > A[k - 1]:
                change = 1
            elif A[k] < A[k - 1]:
                change = -1
        elif change == 1:
            if A[k] < A[k - 1]:
                
                change = 0
                ans += 1
        elif change == -1:
            if A[k] > A[k - 1]:
                
                change = 0
                ans += 1
    ans += 1  
    return ans

N = int(input())
A = [int(i) for i in input().split()]
print(a_sorted_arrays(N, A))

Solution (Python):
N=int(input())
A=list((input().split()))
direction=0 
number=int(A[0])
count=0

for i in range (1, N):
    if direction==0:
        if int(A[i])>number:
            direction=1
        elif  int(A[i])<number:
            direction=-1
        else:
            direction=0
    elif direction==1:
        if int(A[i])<number:
            direction=0
            count+=1
    elif direction==-1:
        if int(A[i])>number:
            direction=0
            count+=1
    number=int(A[i])

count+=1
print(count)

Solution (Python):
n=int(input())
du=""
f=True
ans=1
for j in input().split():
  i=int(j)
  if f==True:
    i0=i
    f=False
  if (du=="u" and i<i0) or (du=="d" and i>i0):
    ans+=1
    du=""
  elif i > i0:
    du="u"
  elif i < i0:
    du="d"
  i0=i
print(ans)

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

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

ans = 1
dec = False
start = True
now = A[0]
for nex in A[1:]:
    if nex-now>0:
        if start:
            start=False
            dec=False
        elif dec:
            start=True
            ans += 1
    elif nex-now<0:
        if start:
            start=False
            dec=True
        elif not dec:
            start=True
            ans += 1
    now = nex

print(ans)

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

i = int(0)
ans = int(0)
f = True


while i+1 < n:

    if a[i] < a[i+1] :
        while a[i] <= a[i+1] :
            if i+1 >= n-1:
                f=False
                break
            i += 1
        
        if f :
            ans += 1

    elif a[i] == a[i+1] :
        if i+1 >= n-1:
            break

    else:
        while a[i] >= a[i+1] :
            if i+1 >= n-1:
                f = False
                break
            i+=1
        
        if f :
            ans += 1
    
    i += 1

    
print(ans+1)

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

while i < n:
    j, k = i, i
    while j < n - 1 and nums[j] <= nums[j+1]:
        j += 1
    while k < n - 1 and nums[k] >= nums[k+1]:
        k += 1
    i = max(j , k) + 1
    ans += 1
print(ans)

Solution (Python):


import sys, math, copy




HUGE = 2147483647
HUGEL = 9223372036854775807
ABC = "abcdefghijklmnopqrstuvwxyz"

def main():
    n = int(input())
    a = list(map(int, input().split()))
    assert len(a) == n
    div = 1
    flag = "free"
    for i in range(1, n):
        if flag == "free":
            if a[i] > a[i - 1]:
                flag = "inc"
            elif a[i] < a[i - 1]:
                flag = "dec"
        elif flag == "inc":
            if a[i] < a[i - 1]:
                div += 1
                flag = "free"
        else:
            if a[i] > a[i - 1]:
                div += 1
                flag = "free"
    print(div)

main()

Solution (Python):
N=int(input())
A=[int(i) for i in input().split()]
if N==1:
    print(1)
    exit()
T=[[A[0],A[1]]]
k=0
for i in range(2,N):
    if len(T[k])<=1:
        T[k].append(A[i])
        continue
    if not((T[k][0]>=A[i-2]>=A[i-1]>=A[i]) or (T[k][0]<=A[i-2]<=A[i-1]<=A[i])):
        k+=1
        T.append([A[i]])
    else:
        T[k].append(A[i])
print(len(T))

Solution (Python):
import sys
n=int(input())
a=list(map(int,input().split()))
x=0
if(n==1):
  print(1)
  sys.exit()
for i in range(n-1):
  if(a[i+1-x]==a[i-x]):
    a.pop(i-x)
    n-=1
    x+=1
    if(n==3):break
if(a[0]==a[1]):
  a.pop(0)
  n-=1
if(n==2):
  print(1)
  sys.exit()
til=a[1]-a[0]
x=0
for i in range(n-1):
  tilt=a[i+1]-a[i]
  if(til*tilt<0):
    x+=1
    if(i+2>=n):
      break
    til=a[i+2]-a[i+1]
print(x+1)

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

ans = 1
status = "Unset"
for i in range(1, N):
    if status == "Unset":
        if A[i-1] < A[i]:
            status = "UP"
        elif A[i-1] > A[i]:
            status = "DOWN"

    else:
        if status == "UP" and A[i-1] > A[i]:
            ans += 1
            status = "Unset"

        if status == "DOWN" and A[i-1] < A[i]:
            ans += 1
            status = "Unset"

print(ans)

Solution (Python):
import numpy as np
n = int(input())
a = [int(i) for i in input().split()]
answer = 1
if n!=1: flag = np.sign(a[1]-a[0])
for i in range(1,n-1):
	num = np.sign(a[i+1]-a[i])
	if flag==0:
		flag = num
	elif num==-flag:
		answer += 1
		flag = 0
print(answer)

Solution (Python):
import numpy as np

N = int(input())
A = list(map(int, input().split()))
count = 1
upd = 0
for i in range(N-1):
  s0 = np.sign(A[i+1] - A[i])
  if upd == 0:
    upd = s0
  elif s0 != 0 and s0 != upd:
    upd = 0
    count += 1
print(count)

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

count = 0
ans = 1
bef = 0
buff = True
for i in A:
    if count == 0:
        count = 1
    elif count == 1:
        if bef < i:
            count += 1
            buff = True
        elif bef > i:
            count += 1
            buff = False
        else:
            pass
    else:
        if buff:
            if bef <= i:
                pass
            else:
                ans += 1
                count = 1
        else:
            if bef >= i:
                pass
            else:
                ans += 1
                count = 1
    bef = i
print(ans)

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

First = True
ans = 1
status = None
for i in range(1, N):
    if First:
        if A[i-1] < A[i]:
            status = "up"
            First = False
            continue
        elif A[i-1] > A[i]:
            status = "down"
            First = False
            continue

    if (status == "up") & (A[i-1] > A[i]):
        ans += 1
        First = True
        continue

    if (status == "down") & (A[i-1] < A[i]):
        ans += 1
        First = True
        continue

print(ans)

Solution (Python):
from itertools import groupby


N = int(input())
A = list(map(int, input().split()))
groups = groupby(A)
A_uniq = [g[0] for g in groups]

N_uniq = len(A_uniq)

ptr = 1
ans = 1
flg = None
while ptr < N_uniq:
    if A_uniq[ptr] > A_uniq[ptr - 1]:
        if flg is None:
            flg = True
        else:
            if flg:
                ptr += 1
                continue
            else:
                ans += 1
                flg = None
    else:
        if flg is None:
            flg = False
        else:
            if flg:
                ans += 1
                flg = None
            else:
                ptr += 1
                continue
    ptr += 1
print(ans)

Solution (Python):
def minimumSubSeq(a):
    n = len(a)
    f = [[0 for i in xrange(2)] for j in xrange(n)]
    f[0][0] = 1
    f[0][1] = 1
    for i in xrange(1, n):
        for j in xrange(2):
            f[i][j] = f[i-1][0] + 1
            f[i][j] = min(f[i][j], f[i-1][1] + 1)
            if (j==0 and a[i-1]>=a[i]) or (j==1 and a[i-1]<=a[i]):
                f[i][j] = min(f[i][j], f[i-1][j])
    return min(f[n-1][0], f[n-1][1])

def main():
    n = int(raw_input().strip())
    a = [int(i) for i in raw_input().strip().split()]
    print minimumSubSeq(a)

if __name__ == '__main__':
    main()

Solution (Python):
N = input()
state = 0
max = 0
min = 0
prev = 0
len = 0
ANS = 1
S = input()
A = list(map(int,S.split()))
 
for i in range(0,int(N)):
  len = len + 1
  if state in {0}:	
      prev = A[i]
      state = 1
  elif state in {1}: 
    if A[i] > prev:
      state = 2
    elif A[i] < prev:
      state = 3
  elif state in {2}: 
    if A[i] < prev:
      state = 1
      ANS = ANS + 1
      len = 0
  elif state in {3}: 
    if A[i] > prev:
      state = 1
      ANS = ANS + 1
      len = 0
  prev = A[i]

print(ANS)

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

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

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

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

Solution (Python):
N = int(input())
src = list(map(int,input().split()))
asc = None
ans = 1
for i in range(N-1):
    d = src[i+1] - src[i]
    if d == 0: continue
    if asc is None:
        asc = d > 0
    elif (asc and d < 0) or (not asc and d > 0):
        ans += 1
        asc = None
print(ans)

Solution (Python):
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
INF = float("inf")


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

    ans = 0
    state = None
    for i in range(N - 1):
        if a_list[i] == a_list[i + 1]:
            continue

        now = a_list[i] < a_list[i + 1]
        if state is None:
            state = now
            continue
        if state != now:
            ans += 1
            state = None

    print(ans + 1)


if __name__ == '__main__':
    main()

Solution (Python):

n = int(input())
a = list(map(int, 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):
def sorted_arrays():
	n = int(raw_input())
	a = map(int, raw_input().split(" "))
	if n==1:
		print 1
		return
	ans = 1
	tag=0
	for i in range(1, n):
		if a[i-1]<a[i]:
			if tag==0:
				tag=1
			elif tag==-1:
				ans+=1
				
				
				tag=0
		if a[i-1]>a[i]:
			if tag==0:
				tag=-1
			elif tag==1:
				ans+=1
				tag=0
	print ans

if __name__ == "__main__":
	sorted_arrays()

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








A = A + [A[-1]]

d = 0
i = 0

inc = None
while i < N:
    if inc is None:
        if A[i] < A[i + 1]:
            inc = True
            d += 1
        elif A[i] > A[i + 1]:
            inc = False
            d += 1
        else:
            inc = None
    else:
        if inc:
            if A[i] > A[i + 1]:
                inc = None
        else:
            if A[i] < A[i + 1]:
                inc = None
    i += 1

if inc is None:
    d += 1
print(d)

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

count = 1
state = 'none'
b = [a[0]]

for i in range(1,n):
    if state=='none':
        before=b.pop()
        now = a[i]
        if before<now:
            b.append(now)
            state='incr'
        elif before>now:
            b.append(now)
            state='decr'
        else:
            b.append(now)
    elif state=='incr':
        before=b.pop()
        now = a[i]
        if before<=now:
            b.append(now)
        else:
            b=[now]
            state='none'
            count+=1
    elif state=='decr':
        before=b.pop()
        now = a[i]
        if before>=now:
            b.append(now)
        else:
            b=[now]
            state='none'
            count+=1
print(count)

Solution (Python):
from sys import stdin

def main():
	N = int(stdin.readline().strip())
	array = [int(x) for x in stdin.readline().strip().split()]
	ans,i= 0,0
	while i+1 < N:
		if array[i] < array[i+1]:
			ans+=1
			while i+1 < N and array[i] <= array[i+1]:
				i+=1
			i+=1
		elif array[i] > array[i+1]:
			ans+=1
			while i+1 < N and array[i] >= array[i+1]:
				i+=1
			i+=1
		else:
			i+=1
	if i+1 == N or ans == 0:
		ans+=1
	print(ans)
main()

Solution (Python):
import numpy as np

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

ret = 0
i = 0
while i < len(a):
    j = i
    while j < n - 1 and a[j] <= a[j+1]:
        j += 1
    h = i
    while h < n - 1 and a[h] >= a[h+1]:
        h += 1
    i = max(h, j) + 1
    ret += 1

print(ret)

Solution (Python):

import math
import sys
import itertools
import numpy as np
import functools
import collections
mo = 1000000007
r = range

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

ret = 0
i = 0
while i < len(a):
    j = i
    while j < n - 1 and a[j] <= a[j+1]:
        j += 1
    h = i
    while h < n - 1 and a[h] >= a[h+1]:
        h += 1

    i = max(h, j) + 1
    ret += 1

print(ret)

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

count=1
z1=0
for i in range(N-1):
    if A[i+1]-A[i]>0:
        z2 = 1
    elif A[i+1]-A[i]<0:
        z2 = -1
    elif A[i+1]-A[i]==0:
        z2 = 0
    if z2*z1<0:
        count+=1
        z1=0
    elif z2!=0:
        z1=z2

print str(count)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

def main():
    n = I()
    a = LI()
    t = 0
    c = a[0]
    r = 1
    for b in a[1:]:
        if c == b:
            continue
        if t==0:
            t = c-b
        elif t * (c-b) < 0:
            r += 1
            t = 0

        c = b

    return r


print(main())

Solution (Python):

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

p=1
flag=0 

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

Solution (Python):
import sys

def solve():
    n = int(input())
    a = [int(i) for i in input().split()]

    ans = 0

    mode = 0

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

    ans += 1

    print(ans)

if __name__ == '__main__':
    solve()

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

count=0
p=0

def cal():
	global p
	global count
	global a,n
	

	
	while True:
		while p + 1 < len(a) and a[p + 1] == a[p]:
			p += 1
		if p + 1 >= len(a):
			return count + 1
		
		if a[p+1]>a[p]:
			while p+1<len(a) and a[p+1]>=a[p]:
				p+=1
			if p+1>=len(a):
				return count+1
		else:
			while p+1<len(a) and a[p+1]<=a[p]:
				p+=1
			if p+1>=len(a):
				return count+1
		count+=1
		p+=1

print cal()

Solution (Python):



import sys

input_line = sys.stdin.readline()
n = int(input_line)
input_line = sys.stdin.readline()
a = [int(v) for v in input_line.split()]

x = 1
bd = 0
skip = False
UP = 1
DOWN = -1
stat = 0
for i in range(1, n):
	diff = a[i] - a[i - 1]
	if diff == 0:
		continue

	if stat == 0:
		if diff > 0:
			stat = UP
		else :
			stat = DOWN

		continue

	if stat == UP and diff < 0:
		x += 1
		stat = 0
		continue

	if stat == DOWN and diff > 0:
		x += 1
		stat = 0

print(str(x))

Solution (Python):
def cutter(nums, idx):
    if len(nums) - idx <= 2:
        return len(nums)
    
    for i in xrange(idx+1, len(nums)):
        if int(nums[i]) != int(nums[i-1]):
            pos = i
            flag = int(nums[i]) > int(nums[i-1])  
            
            break
    else:
        return len(nums)

    for i in xrange(pos, len(nums)):

        if flag:
            if int(nums[i]) < int(nums[i-1]):
                return i
        else:
            if int(nums[i]) > int(nums[i-1]):
                return i
    return len(nums)


n = int(raw_input())
idx = 0
arr = raw_input().split()
if n < 2:
    print n
else:
    count = 0
    while idx < n:
        
        count += 1
        idx = cutter(arr, idx)

    print count

Solution (Python):
def cutter(nums, idx):
    if len(nums) - idx <= 2:
        return len(nums)
    
    for i in xrange(idx+1, len(nums)):
        if int(nums[i]) != int(nums[i-1]):
            pos = i
            flag = int(nums[i]) > int(nums[i-1])  
            
            break
    else:
        return len(nums)

    for i in xrange(pos, len(nums)):

        if flag:
            if int(nums[i]) < int(nums[i-1]):
                return i
        else:
            if int(nums[i]) > int(nums[i-1]):
                return i
    return len(nums)


n = int(raw_input())
idx = 0
arr = raw_input().split()
if n < 2:
    print n
else:
    count = 0
    while idx < n:
        
        count += 1
        idx = cutter(arr, idx)

    print count

Solution (Python):
def cutter(nums, idx):
    if len(nums) - idx <= 2:
        return len(nums)
    
    for i in xrange(idx+1, len(nums)):
        if int(nums[i]) != int(nums[i-1]):
            pos = i
            flag = int(nums[i]) > int(nums[i-1])  
            
            break
    else:
        return len(nums)

    for i in xrange(pos, len(nums)):

        if flag:
            if int(nums[i]) < int(nums[i-1]):
                return i
        else:
            if int(nums[i]) > int(nums[i-1]):
                return i
    return len(nums)


n = int(raw_input())
idx = 0
arr = raw_input().split()
if n < 2:
    print n
else:
    count = 0
    while idx < n:
        
        count += 1
        idx = cutter(arr, idx)

    print count

Solution (Python):



n = input()
if n <= 2:
    print 1
    exit()
    
a = map(int, raw_input().split())

c = 1

status = "n"
i = 1
while i < n:
    if status == "n":
        if a[i-1] < a[i]:
            status = "i"
        elif a[i-1] > a[i]:
            status = "d"
        i += 1
        
    elif status == "i":
        if a[i-1] <= a[i]:
            i += 1
        elif a[i-1] > a[i]:
            status = "n"
            c += 1
            i += 1
        
    elif status == "d":
        if a[i-1] < a[i]:
            status = "n"
            c += 1
            i += 1
        elif a[i-1] >= a[i]:
            i += 1
print c

Solution (Python):
from operator import le, ge

n = int(input())

a_list = list(map(int, input().split()))
prev_a = a_list[0]
cmp = None
cmps = [le, ge]
ans = 1
for a in a_list[1:]:
    if cmp is None:
        if prev_a < a:
            cmp = cmps[0]
        elif prev_a > a:
            cmp = cmps[1]
    else:
        if not cmp(prev_a, a):
            ans += 1
            cmp = None
    prev_a = a

print(ans)

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

UP = 1
DOWN = -1

state = 0
cnt = 1
last = None
for x in A:
	if last == None:
		last = x
	elif state == 0:
		if x - last > 0:
			state = UP
		elif x - last < 0:
			state = DOWN
		last = x
	else:
		if (x-last) * state >= 0:
			last = x
		else:
			last = x
			state = 0
			cnt += 1

print(cnt)