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 = tuple(map(int, input().split()))
    ans, cur = 0, A[0]
    flg = 0  
    for i in A:
        if (flg == 1 and i < cur) or (flg == 2 and i > cur):  
            ans += 1
            flg = 0
        elif not flg and cur < i:  
            flg = 1
        elif not flg and cur > i:  
            flg = 2
        cur = i
    print(ans + 1)


if __name__ == "__main__":
    main()

Solution (Python):

def main():
    n = int(input())
    A = tuple(map(int, input().rstrip().split()))

    
    B, cur = [], 0
    for i in A:
        if i != cur:
            B += [i]
            cur = i
    B += [0]

    
    ans, cur = 0, B[0]
    flg = B[0] < B[1]  
    for i, j in enumerate(B[1:-1], start=1):
        if (flg and j < cur) or (not flg and j > cur):
            ans += 1
            flg = B[i] < B[i + 1]
        cur = j
    print(ans + 1)


if __name__ == "__main__":
    main()

Solution (Python):

def main():
    n = int(input())
    a = tuple(map(int, input().rstrip().split()))
    
    A, x = [a[0]], a[0]
    A_app = A.append
    for i in a[1:]:
        if i != x:
            A_app(i)
            x = i
    A_app(0)
    
    ans, cur = 0, A[0]
    flg = A[0] <= A[1]  
    for i, j in enumerate(A[1:-1], start=1):
        if flg:  
            if j < cur:
                ans += 1
                flg = A[i] <= A[i + 1]
        else:  
            if j > cur:
                ans += 1
                flg = A[i] <= A[i + 1]
        cur = j
    print(ans + 1)


if __name__ == "__main__":
    main()

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

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

dif = [0] * (N-1)
for i in range(1,N): dif[i-1] = A[i] - A[i-1]

ans = 1
t = 0
i = 0
for d in dif:
    if t == 0:
        t = d
    elif d*t < 0:
        ans += 1
        t = 0
print(ans)

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

ans = 1
tmp = aa[0]
flg_asc = flg_desc = True

for a in aa:
  if tmp > a:
    flg_asc = False
  if tmp < a:
    flg_desc = False
  
  if not flg_asc and not flg_desc:
    ans += 1
    flg_asc = flg_desc = True
    
  tmp = a
  
print(ans)

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

flg_inc = False
flg_dec = False

count = 1
for i in range(1, len(a)):
    if a[i] < a[i - 1]:
        flg_dec = True
        if flg_inc:
            count += 1
            flg_inc = False
            flg_dec = False
    elif a[i] > a[i - 1]:
        flg_inc = True
        if flg_dec:
            count += 1
            flg_inc = False
            flg_dec = False

print(count)

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

    
print(ans)

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

c = 0
up = '?'
pre = A[0]
for a in A:
    if a > pre:
        if up is False:
            c += 1
            up = '?'
        else:
            up = True
    elif a < pre:
        if up is True:
            c += 1
            up = '?'
        else:
            up = False
    pre = a

c += 1

print(c)

Solution (Python):
from subprocess import*
with open('Main.java','w')as f:f.write(r"""
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.next());
		int ans = 0;
		int max = Integer.parseInt(sc.next());
		int min = max;
		boolean up = true;
		boolean down = true;
		for (int i = 1; i < n; i++) {
			int a = Integer.parseInt(sc.next());
			if (up) {
				if (a >= max)
					max = a;
				else
					up = false;
			}
			if (down) {
				if (a <= min)
					min = a;
				else
					down = false;
			}
			if (!up && !down) {
				ans++;
				max = a;
				min = a;
				up = true;
				down = true;
			}
		}
		ans++;
		System.out.println(ans);
	}
}
""")
call(('javac','Main.java'))
call(('java','Main'))

Solution (Python):
from subprocess import*
with open('Main.java','w')as f:f.write(r"""
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.next());
		int ans = 0;
		int max = Integer.parseInt(sc.next());
		int min = max;
		boolean up = true;
		boolean down = true;
		for (int i = 1; i < n; i++) {
			int a = Integer.parseInt(sc.next());
			if (up) {
				if (a >= max)
					max = a;
				else
					up = false;
			}
			if (down) {
				if (a <= min)
					min = a;
				else
					down = false;
			}
			if (!up && !down) {
				ans++;
				max = a;
				min = a;
				up = true;
				down = true;
			}
		}
		ans++;
		System.out.println(ans);
	}
}
""")
call(('javac','Main.java'))
call(('java','Main'))

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

B = []
tmp = -1
for a in A:
    if a != tmp:
        B.append(a)
        tmp = a

if len(B) == 1:
    print(1)
    exit()

def f(A):
    ans = 1
    n = len(A)
    if A[0] < A[1]:
        ff = 1
    else:
        ff = -1
    for i in range(1, n - 1):
        if ff == 0:
            if A[i] < A[i + 1]:
                ff = 1
            else:
                ff = -1
        elif ff == 1:
            if A[i] > A[i + 1]:
                ans += 1
                ff = 0
        else:
            if A[i] < A[i + 1]:
                ans += 1
                ff = 0
    return ans

print(f(B))

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

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

ans=0
flg ='0'
for i in range(1,N):

    if flg=='0' and A[i-1]<A[i]:
        flg='+'
    elif flg=='0' and A[i-1]>A[i]:
        flg='-'

    if flg=='+' and A[i-1]>A[i]:
        ans+=1
        flg='0'
    elif flg=='-' and A[i-1]<A[i]:
        ans+=1
        flg='0'
    
    

print(ans+1)

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

Solution (Python):
from sys import stdin
N = int(stdin.readline().rstrip())
data = [int(x) for x in stdin.readline().rstrip().split()]

flag_increase = 0
flag_decrease = 0
ans = 0
tmp = data[0]

for i in data:
    if tmp > i:
        flag_decrease = 1
        if flag_increase == 1:
            ans += 1
            flag_increase = 0
            flag_decrease = 0
        tmp = i
        
    if tmp < i:
        flag_increase = 1
        if flag_decrease == 1:
            ans += 1
            flag_decrease = 0
            flag_increase = 0
        tmp = i
        
    
print(ans+1)

Solution (Python):


import sys

sys.setrecursionlimit(10**7)


def read_h(typ=int):
    return list(map(typ, input().split()))


def read_v(n, m=1, typ=int):
    return [read_h() if m > 1 else typ(input()) for _ in range(n)]


def main():
    N, = read_h()
    A = read_h()

    if len(A) == 1:
        print(1)
        return

    cnt = 1
    inc = None

    for i in range(len(A) - 1):
        

        if inc is None and A[i] != A[i + 1]:
            inc = A[i] < A[i + 1]
            continue

        if A[i] == A[i + 1] or (inc and A[i] < A[i + 1]) or (not inc and A[i] > A[i + 1]):
            continue

        
        cnt += 1
        inc = None

    print(cnt)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    ans = 1
    mov = 0
    prev = A[0]
    for a in A[1:]:
        if mov == 0:
            if prev < a:
                mov = 1
            elif prev > a:
                mov = -1
        elif (mov == 1 and prev > a) or (mov == -1 and prev < a):
            ans += 1
            mov = 0
        prev = a
    print(ans)


if __name__ == "__main__":
    main()

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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
if N == 1:
    print(1)
else:
    if A[0] == A[1]:
        pre = 0
    elif A[0] > A[1]:
        pre = -1
    else:
        pre = 1
    prevalue = A[1]
    ans = 0
    for a in A[2:]:
        if a > prevalue:
            if pre == -1:
                ans += 1
                pre = 0
            else:
                pre = 1
        elif a < prevalue:
            if pre == 1:
                ans += 1
                pre = 0
            else:
                pre = -1
        prevalue = a
    print(ans+1)

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

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
cnt=1
condition=''
for i in range(N-1):
  if A[i]>A[i+1] and condition=='plus':
    cnt+=1
    condition=''
  elif A[i]<A[i+1] and condition=='minus':
    cnt+=1
    condition=''
  elif A[i]>A[i+1]:
    condition='minus'
  elif A[i]<A[i+1]:
    condition='plus'

print(cnt)

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

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

count, state = 0, 0
for n in range(1, N):
	d = A[n] - A[n - 1]
	
	if d == 0:
		continue
	if d < 0 and state <= 0:
		state = -1
		continue
	if 0 < d and 0 <= state:
		state = 1
		continue
	count += 1
	state = 0
print(count + 1)

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

ans = 0

flg = 0
t = A[0]

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

print(ans+1)

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
if n <= 2:
    print(1)
    quit()
h = 0
ans = 1
for i in range(n - 1):
    if (l[i + 1] - l[i]) * h < 0:
        ans += 1
        h = 0
        continue
    if l[i + 1] - l[i] < 0:
        h = -1
    elif l[i + 1] - l[i] > 0:
        h = 1
print(ans)

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 solve(n, a):
    ans = 1
    b = [a[i+1] - a[i] for i in range(n-1)]
    c = list(filter(lambda _: _ != 0, b))
    i = 0
    m = len(c)
    while i < m-1:
        if c[i] * c[i+1] < 0:
            ans += 1
            i += 1
        i += 1
    return ans

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

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
if(n<=2):
    print(1)
    quit()
h=0
ans=1
for i in range(n-1):
    if((l[i+1]-l[i])*h<0):
        ans+=1
        h=0
        continue
    if(l[i+1]-l[i]<0):
        h=-1
    elif(l[i+1]-l[i]>0):
        h=1
print(ans)

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

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

result = 0
increase = None

for i in range(1, n):
    if a[i] < a[i - 1]:
        if increase is None:
            increase = False
        elif increase:
            result += 1
            increase = None
    elif a[i] > a[i - 1]:
        if increase is None:
            increase = True
        elif not increase:
            result += 1
            increase = None
result += 1
print(result)

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

ans = 1
updown = False
up = False

for i in range(N-1):
    if updown:
        if (up and A[i] > A[i+1]) or (not up and A[i] < A[i+1]):
            ans += 1
            updown = False
            up = False
    else:
        if A[i] < A[i+1]:
            updown = True
            up = True
        elif A[i] > A[i+1]:
            updown = True

print(ans)

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


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

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

Solution (Python):
n = input();
n = int(n);
a = input().rstrip('\n')
a = a.split(" ");
a.append(a[n-1]);
state = int(a[1]) - int(a[0]);
ans = 1;
for i in range(2,n) :
	if int(a[i]) - int(a[i-1]) > 0 and state < 0 :
		ans = ans + 1;
		state = int(a[i+1]) - int(a[i]);
	elif int(a[i]) - int(a[i-1]) < 0 and state > 0 :
		ans = ans + 1;
		state = int(a[i+1]) - int(a[i]);
	elif state == 0 :
		state = int(a[i+1]) - int(a[i]);
print(ans);

Solution (Python):
seq_len = int(input())
seq_a = [int(x) for x in input().split()]
if seq_len == 1:
    print(1)
    exit()
b = 0; c = 0; ans = 1
for i in range(seq_len-1):
    if seq_a[i] < seq_a[i+1]:
        b = 1
    if seq_a[i] > seq_a[i+1]:
        c = 1
    if b == 1 and c == 1:
        ans += 1
        b = 0 ; c = 0
print(ans)

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

ans = 1

flag = 0

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

print(ans)

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

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

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

cnt = 1
flag = 0

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

print(cnt)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
inc = False
dec = False
ans = 1

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

print(ans)

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

incFrag = False
decFrag = False

ans = 1
for i in range(0, n - 1):
    if aArray[i + 1] > aArray[i]:
        if decFrag:
            ans += 1
            decFrag = False
        else:
            incFrag = True
    elif aArray[i + 1] < aArray[i]:
        if incFrag:
            ans += 1
            incFrag = False
        else:
            decFrag = True
print(ans)

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

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

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

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

conti_flg = 0  
fst = A[0]
for i in A:
    snd = i
    if fst > snd:
        conti_flg = -1 if conti_flg <= 0 else 2
    elif fst < snd:
        conti_flg = 1 if conti_flg >= 0 else 2

    if conti_flg == 2:
        count += 1
        conti_flg = 0
    fst = snd

print(count)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
cnt,st,d = 0,-1,0
for i in range(n):
  if st < 0:
    st = i
    continue
  elif d == 0:
    if a[i]-a[st] != 0:
      r = i
      d = a[r]-a[st]
    continue
  else:
    if (a[i]-a[i-1])*d < 0:
      cnt += 1
      st,d = i,0
if st != -1:
  cnt += 1
print(cnt)

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

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
if(n<=2):
    print(1)
    quit()
h=0
ans=1
for i in range(n-1):
    if((l[i+1]-l[i])*h<0):
        ans+=1
        h=0
        continue
    if(l[i+1]-l[i]<0):
        h=-1
    elif(l[i+1]-l[i]>0):
        h=1
print(ans)

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

Solution (Python):
from numpy import sign

n = int(input())
a = list(map(int,input().split()))
b = [sign(a[i+1]-a[i]) for i in range(n-1)]

c = 1
f = 0
for t in b:
  if f == 0:
    f = t
  elif f*t == -1:
    c += 1
    f = 0
print(c)

Solution (Python):
from numpy import sign

n = int(input())
a = list(map(int,input().split()))
b = [sign(a[i+1]-a[i]) for i in range(n-1)]

c = 1
f = 0
for t in b:
  if f == 0:
    f = t
  elif f == 1 and t == -1:
    c += 1
    f = 0
  elif f == -1 and t == 1:
    c += 1
    f = 0
print(c)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
cnt=1
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]:
            cnt+=1
            flag=0
    else:
        if a[i]<a[i+1]:
            cnt+=1
            flag=0
print(cnt)

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

    flag = 'flat'

    counter = 1

    for i in range(1, N):
        old = A[i-1]
        new = A[i]

        if flag == 'inc':
            if old <= new:
                continue
            else:
                counter += 1
                flag = 'flat'

        elif flag == 'dec':
            if old >= new:
                continue
            else:
                counter += 1
                flag = 'flat'
        else:
            if old < new:
                flag = 'inc'
            elif old > new:
                flag = 'dec'
            else:
                continue

    print(counter)

Solution (Python):
_ = int(input())
A = [int(a) for a in input().split()]
selector = 0
count = 1
pre_a = A[0]
for a in A[1:]:
    if selector == 0:
        selector = a - pre_a
    elif selector * (a - pre_a) < 0:
        count += 1
        selector = 0
    pre_a = a
print(count)

Solution (Python):

import string
import sys
from itertools import chain, dropwhile, takewhile


def read(
    *shape, f=int, it=chain.from_iterable(sys.stdin), whitespaces=set(string.whitespace)
):
    def read_word():
        w = lambda c: c in whitespaces
        nw = lambda c: c not in whitespaces
        return f("".join(takewhile(nw, dropwhile(w, it))))

    if not shape:
        return read_word()
    elif len(shape) == 1:
        return [read_word() for _ in range(shape[0])]
    elif len(shape) == 2:
        return [[read_word() for _ in range(shape[1])] for _ in range(shape[0])]


def readi(*shape):
    return read(*shape)


def readi1(*shape):
    return [i - 1 for i in read(*shape)]


def readf(*shape):
    return read(*shape, f=float)


def reads(*shape):
    return read(*shape, f=str)


def arr(*shape, fill_value=0):
    if len(shape) == 1:
        return [fill_value] * shape[fill_value]
    elif len(shape) == 2:
        return [[fill_value] * shape[1] for _ in range(shape[0])]


def dbg(**kwargs):
    print(
        ", ".join("{} = {}".format(k, repr(v)) for k, v in kwargs.items()),
        file=sys.stderr,
    )


def main():
    N = readi()
    a = readi(N)
    ans = 1
    c = a[0]
    g, z = False, False
    for i in range(1, N):
        if c < a[i] and g:
            ans += 1
            g, z, = False, False
        elif c < a[i] and not g:
            z = True
        elif c > a[i] and z:
            ans += 1
            g, z, = False, False
        elif c > a[i] and not z:
            g = True
        c = a[i]

    print(ans)


if __name__ == "__main__":
    main()

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

Solution (Python):

"""
A - Sorted Arrays
https://atcoder.jp/contests/agc013/tasks/agc013_a

"""
import sys


def solve(N, array):
    ans = 1
    prev = None
    direction = None

    for a in array:
        if prev == None:
            pass
        elif a > prev:
            if direction == 'd':
                ans += 1
                direction = None
            else:
                if a > prev:
                    direction = 'u'
        elif a < prev:
            if direction == 'u':
                ans += 1
                direction = None
            else:
                if a < prev:
                    direction = 'd'
        prev = a
    return ans


def main(args):
    N = int(input())
    array = [int(a) for a in input().split()]
    ans = solve(N, array)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):
n=int(input())
a=[int(x) for x in input().split()]
if n==1:
    print(1)
    exit()
b=0;c=0;ans=1
for i in range(n-1):
    if a[i]<a[i+1]:
        b=1
    if a[i]>a[i+1]:
        c=1
    if b and c:
        ans+=1
        b=0;c=0
print(ans)

Solution (Python):
N = int(input())
As = list(map(int, input().split()))
now = As[0]
shape = 0
ans = 1
for i in As:
    if shape == 0:
        if i  > now:
            shape = 1
            now = i
        elif i < now:
            shape = 2
            now = i
    elif shape == 1:
        if i < now:
            shape = 0
            now = i
            ans += 1
        else:
            now = i
    else:
        if i > now:
            shape = 0
            now = i
            ans += 1
        else:
            now = i
print(ans)

Solution (Python):
n=int(input())
pre=0
ans=1
t=0
for i in input().split():
    a=int(i)
    if pre==0:
        pre=a
    else:
        if pre>a:
            if -t<0 and t**2==1:
                ans+=1
                t=-2
            else:
                t=-1
        elif pre<a:
            if t<0 and t**2==1:
                ans+=1
                t=2
            else:
                t=1
        else:
            continue
        pre=a
print(ans)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
before=0
state='minus'
ans=0

for i in range(N):
  if A[i]==before:
    continue
  elif A[i]-before>0:
    if state=='minus':
      ans+=1
      state='None'
    elif state=='None':
      state='plus'
  else:
    if state=='plus':
      state='None'
      ans+=1
    elif state=='None':
      state='minus'
  before=A[i]
print(ans)

Solution (Python):
N = int(input())
sub = 0
count = 1
A = [int(i) for i in input().split()]
if N > 1:
	pre_num = A[1]
	pre_sub = A[1] - A[0]
for num in A[2:]:
    if not pre_num == num:
        if pre_sub * (num - pre_num) < 0:
            count += 1
            pre_sub = 0
        else:
          	pre_sub = num - pre_num
        pre_num = num
print(count)

Solution (Python):
N = int(input())
selector = 0
count = 1
A = [int(i) for i in input().split()]
pre_num = A[0]
for num in A[1:]:
    if selector == 0:
        selector = num - pre_num
    if selector > 0:
        if num < pre_num:
            selector = 0
            count += 1
    if selector < 0:
        if num > pre_num:
            selector = 0
            count += 1
    pre_num = num
print(count)

Solution (Python):
from collections import deque
N = int(input())
A = deque([int(i) for i in input().split()])
dec = False
inc = False
cnt = 1
antenum = A.popleft()

for _ in range(N - 1):
    if not inc and not dec:
        if A[0] < antenum:
            dec = True
        elif A[0] > antenum:
            inc = True
    elif inc:
        if A[0] < antenum:
            cnt += 1
            inc = False
    elif dec:
        if A[0] > antenum:
            cnt += 1
            dec = False
    antenum = A.popleft()

print(cnt)

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

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

Solution (Python):
def calc(a_cur, a_prev, vec, ans):
    if a_cur == a_prev:
        pass
    elif a_cur > a_prev:
        if vec == -1:
            ans += 1
            vec = 0
        else:
            vec = 1

    elif a_cur < a_prev:
        if vec == 1:
            ans += 1
            vec = 0
        else:
            vec = -1

    return a_cur, vec, ans

def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    ans = 0
    vec = 0
    a_prev = a[0]
    for i in range(1, n):
        a_cur = a[i]
        a_prev, vec, ans = calc(a_cur, a_prev, vec, ans)

    print(ans+1)

main()

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
n=1
mode=0
for i in range(1,N):
	if mode==0:
		mode = A[i]-A[i-1]
	else:
		if mode * (A[i]-A[i-1]) >=0:
			continue
		else:
			n+=1
			mode=0
print(n)

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

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

D = [A[i + 1] - A[i] for i in range(N - 1)]

pre = 0
cnt = 1
for da in D:
    if pre == 0:
        pre = da
    elif pre * da < 0:
        pre = 0
        cnt += 1

print(cnt)

Solution (Python):
N=int(input())
s=list(map(int,input().split(" ")))
r=1
before=None
if N<=2:
  pass
else:
  flag=None
  for i,z in enumerate(s):
    if flag==None:
      if before == None:
        before=z
      if before<z:
        flag=True
        before=z
      elif before>z:
        flag=False
        before=z
    elif flag==True:
      if z<before:
        flag=None
        before=z
        r+=1
      else:
        before=z
    else:
      if z>before:
        flag=None
        before=z
        r+=1
      else:
        before=z

print(r)