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



Sample input:
6
1 2 3 2 2 1


Sample output:
2

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

n=ii()
arr=limii()
cnt=0
if n==1:
    print(1)
    exit()

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = float('inf')
up = -1
count = 0
for i in range(N-1):
    if A[i] > A[i+1]:
        if up == -1:
            up = 0
        elif up == 1:
            up = -1
            count += 1
    elif A[i] < A[i+1]:
        if up == -1:
            up = 1
        elif up == 0:
            up = -1
            count += 1
print(count+1)

Submission status: Accepted

Solution code:
def examA():
    N = I()
    A = LI()
    ans = 1
    flag = None
    for i in range(N-1):
        cur = A[i+1] - A[i]
        if not flag:
            if cur<0:
                flag = "d"
            elif cur>0:
                flag = "u"
        else:
            if cur<0 and flag=="u":
                flag = None
                ans += 1
            elif cur>0 and flag=="d":
                flag = None
                ans += 1
    print(ans)
    return

def examB():
    ans = 0
    print(ans)
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math,random
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examA()

"""

"""

Submission status: Accepted

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

ans = 0
i = 0
while i < N:
    j = i + 1
    t = 0
    while j < N:
        if A[j - 1] == A[j]:
            j += 1
            continue
        elif t == 0 and A[j - 1] != A[j]:
            t = (A[j] - A[j - 1]) / abs(A[j] - A[j - 1])
        elif t != 0 and t != (A[j] - A[j - 1]) / abs(A[j] - A[j - 1]):
            break
        j += 1
    ans += 1
    i = j

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
#state0は未定、1は広義単調増加、2は広義単調減少
state = 0
m = A[0]
ans = 1

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

print(ans)

Submission status: Accepted

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

ans = 1

#state0は未定、1は広義単調増加、2は広義単調減少
state = 0
m = A[0]

i = 1
while i <= N-1:
    if m < A[i]:
        if state == 0:
            state = 1
        elif state == 2:
            ans += 1
            state = 0
    elif m > A[i]:
        if state == 0:
            state = 2
        elif state == 1:
            ans += 1
            state = 0
    m = A[i]
    i += 1

print(ans)

Submission status: Accepted

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

ans = 1

#state0は未定、1は広義単調増加、2は広義単調減少
state = 0
m = A[0]

i = 1
while i <= N-1:
    if m < A[i]:
        if state == 0:
            state = 1
            m = A[i]
        elif state == 1:
            m = A[i]
        else:
            ans += 1
            state = 0
            m = A[i]
    elif m > A[i]:
        if state == 0:
            state = 2
            m = A[i]
        elif state == 1:
            ans += 1
            state = 0
            m = A[i]
        else:
            m = A[i]
    i += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

up=0
down=0
count = 0
x=a[0]
for i in range(n):
  if x==a[i]:
    continue
  elif x<a[i]:
    if down==0:
      up=1
    else:
      down=0
      up=0
      count+=1
  else:
    if up==0:
      down=1
    else:
      down=0
      up=0
      count+=1
  x=a[i]
print(count+1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


class PriorityQueue:
    def __init__(self, l=[]):
        self.__q = l
        heapq.heapify(self.__q)
        return

    def push(self, n):
        heapq.heappush(self.__q, n)
        return

    def pop(self):
        return heapq.heappop(self.__q)


MOD = int(1e09) + 7
INF = int(1e15)


def main():
    # sys.stdin = open("sample.txt")
    N = Scanner.int()
    A = Scanner.map_int()
    add = 0
    ans = 1
    i = 0
    while i < N-1:
        if add == 0:
            add = 1 if A[i] < A[i + 1] else -1 if A[i] > A[i+1] else 0
        elif add == 1:
            if A[i] > A[i + 1]:
                add = 0
                ans += 1
        else:
            if A[i] < A[i+1]:
                add = 0
                ans += 1
        i += 1
    print(ans)
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

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

flag = 0
cnt = 1

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

print(cnt)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline

def main():
    N = int(input())
    a = list(map(int,input().split()))
    
    b = 0
    count = 1
    for i in range(N-1):
        if b == 0:
            if a[i+1]-a[i] > 0:
                b = 1
            elif a[i+1] == a[i]:
                continue
            else:
                b = -1
        elif b == 1:
            if a[i+1]-a[i] >= 0:
                continue
            else:
                count += 1
                b = 0
        else:
            if a[i+1] <= a[i]:
                continue
            else:
                count += 1
                b = 0
                
    print(count)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
n = int(input())
a_ls = [int(i) for i in sys.stdin.readline().split()]
flg = None #正
cur = a_ls[0]
cnt = 1
for a in a_ls[1:]:
    if a > cur:
        if flg is None:
            flg = True
        if not flg:
            flg = None
            cnt += 1
    if a < cur:
        if flg is None:
            flg = False
        if flg:
            cnt += 1
            flg = None
    cur = a
print(cnt)

Submission status: Accepted

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

ans = 1
trend = 0
last = -1
for a in A:
    if last == -1:
        last = a
        continue
    if a > last:
        if trend < 0:
            ans += 1
            last = -1
            trend = 0
        else:
            trend = 1
    elif a < last:
        if trend > 0:
            ans += 1
            last = -1
            trend = 0
        else:
            trend = -1
    last = a

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split(' ')))
A_short = [A[0]]
tmp = A[0]
ans = 1
cnt = 0
for i in A[1:]:
    if tmp != i:
        A_short.append(i)
    tmp = i
while cnt < len(A_short)-2:
    if A_short[cnt] >= A_short[cnt+1] and A_short[cnt+1] <A_short[cnt+2]:
        cnt += 1
        ans += 1
    elif A_short[cnt] <= A_short[cnt+1] and A_short[cnt+1] > A_short[cnt+2]:
        cnt += 1
        ans += 1
    cnt += 1
print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N, *A = map(int, read().split())

cnt = 1
flag = 'E'
for i, a in enumerate(A):
    if i == N - 1:
        break

    if a < A[i + 1]:
        if flag == 'D':
            cnt += 1
            flag = 'E'
        else:
            flag = 'U'
    elif a > A[i + 1]:
        if flag == 'U':
            cnt += 1
            flag = 'E'
        else:
            flag = 'D'

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = 1
flag = 0
for i in range(N-1):
    if A[i+1] < A[i] and flag == 0:
        flag = 1 #減少
    elif A[i+1] > A[i] and flag ==0:
        flag = 2 #増加
    elif flag == 1 and (A[i+1] > A[i]):
        ans += 1 #リストを区切る
        flag = 0 
    elif flag == 2 and (A[i+1] < A[i]):
        ans += 1
        flag = 0
    #A[i+1] == A[i] のときはリストを区切らず次のiへ
print(ans)

Submission status: Accepted

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

count = 1
state = 0
for i in range(N-1):
    diff = A[i+1] - A[i]
    if state == 0:
        if diff > 0:
            state = 1
            continue
        if diff == 0:
            state = 0
            continue
        if diff < 0:
            state = -1
            continue
    if state == 1:
        if diff > 0:
            state = 1
            continue
        if diff == 0:
            state = 1
            continue
        if diff < 0:
            count += 1
            state = 0
            continue
    if state == -1:
        if diff > 0:
            count += 1
            state = 0
            continue
        if diff == 0:
            state = -1
            continue
        if diff < 0:
            state = -1
            continue

print(count)

Submission status: Accepted

Solution code:
# シミュレーションぽい？iとi+1をみて増加か減少か判断して変わったら終了
n = int(input())
arr = list(map(int, input().split()))
ans = 1
state = 0  # 1..増加 -1..減少 0..未確定 みたいな
for i in range(n - 1):
    if state == 0:
        if arr[i] > arr[i + 1]:
            state = -1
        elif arr[i] < arr[i + 1]:
            state = 1
    elif state == 1:
        if arr[i] < arr[i + 1]:
            continue
        elif arr[i] > arr[i + 1]:
            # リセット
            state = 0
            ans += 1
        else:
            continue
    elif state == -1:
        if arr[i] > arr[i + 1]:
            continue
        elif arr[i] < arr[i + 1]:
            # リセット
            state = 0
            ans += 1
        else:
            continue

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
al = list(map(int,input().split()))

ans = 1
mode = "center"
now = al[0]
for a in al:
    if mode == "center":
        if a > now:
            mode = "up"
            now = a
        elif a < now:
            mode = "down"
            now = a
    elif mode == "up":
        if a > now:
            now = a
        elif a < now:
            now = a
            mode = "center"
            ans += 1
    elif mode == "down":
        if a > now:
            now = a
            mode = "center"
            ans += 1
        elif a < now:
            now = a
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
input_array = list(map(int, input().split()))

upper = False
lower = False
res = 1
for i in range(len(input_array) - 1):
    if input_array[i] == input_array[i + 1]:
        continue
    elif input_array[i] < input_array[i + 1]:
        upper = True
    else:
        lower = True

    if upper is True and lower is True:
        res += 1
        upper = False
        lower = False

print(res)

Submission status: Accepted

Solution code:
N = int(input())
P = list(map(int,input().split()))
cnt = 1
flag = 0 
for i in range(1,N):
    if P[i-1]>P[i] and flag == 0:
        flag = 1
    elif P[i-1]<P[i] and flag == 0:
        flag = 2
    elif flag == 1 and (P[i-1] < P[i]) :
        cnt += 1
        flag = 0
    elif flag == 2 and (P[i-1] > P[i]) :
        cnt += 1
        flag = 0
print(cnt)

Submission status: Accepted

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

answer = 0
up_or_down = 0
previous = None

for i in a:
    if previous is None or previous == i:
        pass
    elif previous < i:
        if up_or_down == -1:
            answer += 1
            up_or_down = 0
        else:
            up_or_down = 1
    elif previous > i:
        if up_or_down == 1:
            answer += 1
            up_or_down = 0
        else:
            up_or_down = -1
    previous = i

print(answer + 1)

Submission status: Accepted

Solution code:
from sys import stdin

n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))+[0]
li = [li[i] for i in range(len(li)-1) if li[i] != li[i+1]]
#長さが2以下の数列に関しては絶対答えは1になる
if n <= 2:
    print(1)
    exit()
#そうでない場合
count = 1
flag = 0
for i in range(1,len(li)-1):
    if flag == 1:
        flag = 0
        continue
    else:
        if li[i-1] > li[i] < li[i+1] or li[i-1] < li[i] > li[i+1]:
            flag = 1
            count += 1
print(count)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
last = a[0]
direction = 0
ans = 1

# 増加か減少かで分ける 切れ目で+1する
for a in a[1:]:
    if direction == 0:
        if last < a:
            direction = 1
        elif last > a:
            direction = -1
        last = a
        continue
    if direction == 1:
        if last <= a:
            last = a
            continue
        else:
            last = a
            direction = 0
            ans += 1
            continue
    if direction == -1:
        if last >= a:
            last = a
            continue
        else:
            last = a
            direction = 0
            ans += 1
            continue
print(ans)

Submission status: Accepted

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

count = 0
multi = 1

while len(A) > 0:
    i = 0
    sign = A[min(1, len(A)-1)] - A[0]
    multi = 0
    if len(A) != 1:
        while multi >= 0:
            if len(A) <= i+2:
                i = len(A)-1
                break
            else:
                i += 1
                multi = (A[i+1] - A[i]) * sign
                if sign == 0:
                    sign = (A[i+1]-A[i])
        del A[0:i+1]
        count += 1
    else:
        del A[0]
        count += 1

print(count)

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int,input().split()))

before = ud = None
cutCnt = 0
i = 0
while i < n:
  if before is None:
    before = arr[i]
    continue
  else:
    if ud is None:
      if before > arr[i]:
        ud = 1
        before = arr[i]
      elif before < arr[i]:
        ud = 2
        before = arr[i]
    elif ud == 1 and before < arr[i]:
      cutCnt+=1
      ud = None
      before = None
      i-=1
    elif ud == 2 and before > arr[i]:
      cutCnt+=1
      ud = None
      before = None
      i-=1
    else:
      before =arr[i]
    i+=1
    
print(cutCnt+1)

Submission status: Accepted

Solution code:
N = int(input())

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

cnt = 0
mode = 0

for i in range(len(A)):
    if i == 0:
        continue
    
    if (A[i] - A[i-1] >= 0 and mode >=0) or (A[i] - A[i-1] <= 0 and mode <= 0):
        if A[i] - A[i-1] > 0:
            mode = 1
        if A[i] - A[i-1] < 0:
            mode = -1
    else:
        mode = 0
        cnt += 1

print(cnt+1)

Submission status: Accepted

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

cond = 0
tmp = A[0]
cnt = 0
for i in range(1,N):
	if cond == 0:
		if A[i] == tmp:
			tmp = A[i]
			continue
		elif A[i] < tmp:
			tmp = A[i]
			cond = -1
			continue
		else:
			tmp = A[i]
			cond = 1
			continue

	elif cond == -1:
		if A[i] > tmp:
			cnt += 1
			cond = 0
			tmp = A[i]
			continue
		else:
			tmp = A[i]
			continue

	else:
		if A[i] < tmp:
			cnt += 1
			cond = 0
			tmp = A[i]
			continue
		else:
			tmp = A[i]
			continue

print(cnt + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
li = list(map(int,input().split()))

x = ""
count = 1

for i in range(N - 1):
  if li[i] == li[i + 1]:
    continue

  if x == "":
    if li[i] < li[i + 1]:
      x = 'UP'
    
    if li[i] > li[i + 1]:
      x = 'DOWN'

  else:
    if x == 'UP' and li[i] > li[i + 1]:
      x = ""
      count += 1
    elif x == 'DOWN' and li[i] < li[i + 1]:
      x = ""
      count += 1

print(count)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
N = int (input())
A = [int(a) for a in input().split()]
d = 0
ans = 1
for i in range(1, N):
    if A[i-1] < A[i]:
        if d < 0:
            ans += 1
            d = 0
        elif d == 0:
            d = 1

    elif A[i-1] > A [i]:
        if d > 0:
            ans +=1
            d = 0
        elif d == 0:
            d = -1

print(ans)

Submission status: Accepted

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


for i in range(N-1):

    if flag==1 and A[i+1]<A[i] or flag==-1 and A[i+1]>A[i] or flag=="st":
        count+=1
        flag=0
    elif flag==0:
        if A[i+1]==A[i]:
            pass
        elif A[i+1]>A[i]:
            flag=1
        else:
            flag=-1


print(count)

Submission status: Accepted

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

ans = 1
x = 0

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    a = LI()
    ans = 1
    t = -1
    for i in range(n - 1):
        if a[i] < a[i + 1]:
            if t == 1:
                continue
            elif t == -1:
                t = 1
            else:
                t = -1
                ans += 1
        elif a[i + 1] < a[i]:
            if t == 0:
                continue
            elif t == -1:
                t = 0
            else:
                t = -1
                ans += 1
    print(ans)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
flg = 0
cnt=0
for i in range(n-1):
    if l[i+1]==l[i]:
        continue
    elif l[i+1]>l[i]:
        if flg==-1:
            cnt+=1
            flg=0
        else:
            flg=1
    else:
        if l[i+1]<l[i]:
            if flg==1:
                cnt+=1
                flg=0
            else:
                flg=-1
print(cnt+1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpl()
u = False
d = False
res = 1
i = 0
while i < n-1:
    if u and a[i+1] < a[i]:
        res += 1
        u = False
        i += 1
    elif d and a[i+1] > a[i]:
        res += 1
        d = False
        i += 1
    if i < n-1:
        if a[i+1] > a[i]:
            u = True
        elif a[i+1] < a[i]:
            d = True
    i += 1
print(res)

Submission status: Accepted

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

cnt=0
bflag=False
sflag=False

for i in range(1,N):
  if A[i]>A[i-1]:
    if sflag==True:
      cnt+=1
      bflag=False
      sflag=False
    else:
      bflag=True
      sflag=False
  elif A[i]<A[i-1]:
    if bflag==True:
      cnt+=1
      bflag=False
      sflag=False
    else:  
      bflag=False
      sflag=True    
  else:
    pass
    
print(cnt+1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
cnt=0
cnt1=0
ans=1
aa=a[0]
for i in range(n):
    #print(ans)
    if aa>a[i] and cnt1==0:
        cnt=1
        aa=a[i]
    elif aa<a[i] and cnt==0:
        cnt1=1
        aa=a[i]
    elif aa>a[i] and cnt1!=0:
        aa=a[i]
        ans+=1
        cnt1=0
    elif aa<a[i] and cnt!=0:
        aa=a[i]
        ans+=1
        cnt=0
    else:
        continue
print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N,*A = map(int,read().split())

# まず、重複を除外
B = [A[0]] + [y for x,y in zip(A,A[1:]) if x != y]

seq = []
answer = 0
for x in B:
    if not seq:
        answer += 1
        seq.append(x)
        continue
    if len(seq) == 1:
        seq.append(x)
        continue
    a,b = seq[-2:]
    if a < b < x or a > b > x:
        seq.append(x)
        continue
    # xを追加すると破綻
    answer += 1
    seq = [x]

print(answer)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
i=0
ans=0
if n<=2:
    print(1)
    exit()
while i<n-1:
    if a[i]<a[i+1]:
        i+=1
        while i<n-1:
            if a[i]<=a[i+1]:
                i+=1
            else:
                if i==n-2:
                    ans+=1
                break
        #print(i)
        i+=1
        #print(a[:i])
        ans+=1
    elif a[i]>a[i+1]:
        i+=1
        while i<n-1:
            if a[i]>=a[i+1]:
                i+=1
            else:
                if i==n-2:
                    ans+=1
                break
        #print(i)
        i+=1
        #print(a[:i])
        ans+=1
    else:
        i+=1
        #print(a[:i])
        if i==n-1:
            ans+=1
print(ans)

Submission status: Accepted

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

up = False
down = False
count = 1
for i in range(N-1):
    if A[i] < A[i+1]:
        up = True
    elif A[i] > A[i+1]:
        down = True
    if up ==True and down == True:
        count += 1
        up = False
        down = False
print(count)

Submission status: Accepted

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

plus = True
minus = True

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

ans += 1

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
a = list(map(int, input().split(' ')))

def dec(i):
    while i+1 < N and a[i] >= a[i+1]:
        i += 1
    return i

def inc(i):
    while i+1 < N and a[i] <= a[i+1]:
        i += 1
    return i


ans = 0
i = 0
while i < N:
    i = max(inc(i), dec(i)) + 1
    ans += 1

print(ans)