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:
n = int(input())
a = list(map(int, input().split()))

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    A = list(map(int, input().split()))
    count = 0
    plus = '-1'
    A = [A[i + 1] - A[i] for i in range(n - 1)]
    for a in A:
        if plus == '-1':
            if a > 0:
                plus = True
            elif a < 0:
                plus = False
            else:
                pass
        elif plus:
            if a < 0:
                plus = '-1'
                count += 1
            else:
                pass
        else:
            if a > 0:
                plus = '-1'
                count += 1
            else:
                pass
    print(count + 1)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N = int(input())
    A = [int(x) for x in input().split()]

    zogen = 0

    ans = 0
    for a, b in zip(A, A[1:]):
        if zogen > 0 and b >= a:
            continue
        if zogen < 0 and b <= a:
            continue
        if zogen == 0:
            zogen = b - a
            continue
        ans += 1
        zogen = 0

    print(ans + 1)



    
    

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())

a = [0] * n

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

last = 0
cnt = 1
increase_flag = None
first_flag = True
for i in range(len(a) - 1):
    if a[i] < a[i + 1]:
        if (increase_flag is None):
            increase_flag = True
        elif not (increase_flag):
            cnt += 1
            increase_flag = None
        else:
            continue
    elif a[i] > a[i + 1]:
        if (increase_flag is None):
            increase_flag = False
        elif not (increase_flag):
            continue
        else:
            cnt += 1
            increase_flag = None

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

increase_flag=False
decrease_flag=False
answer=0
if n!=1:
  for i in range(len(a)-1):
    if increase_flag==False and decrease_flag==False:
      #旗が下がっているときに、二値を見ることで単調増加か、単調減少を見極める。

      if a[i+1]>a[i]:
        increase_flag=True
        answer+=1
      elif a[i+1]<a[i]:
        decrease_flag=True
        answer+=1
      
    else:
      #単調増加と単調減少から変わる時旗を下げるだけです。次の値が単調増加になる可能性があるから。
      if decrease_flag==True and a[i+1]>a[i]:
        decrease_flag=False
      elif increase_flag==True and a[i+1]<a[i]:
        increase_flag=False
  
  #index=-1とindex=-2より、旗を下げた場合、残りの値が一つのグループとなるため+=1。
  if increase_flag==False and decrease_flag==False:
    if a[-1]>a[-2]:
      increase_flag=True
      answer+=1
    elif a[-1]<a[-2]:
      decrease_flag=True
      answer+=1
  print(answer)
else:
  print(1)

Submission status: Accepted

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

increase_flag=False
decrease_flag=False
answer=0
if n!=1:
  for i in range(len(a)-1):
    if increase_flag==False and decrease_flag==False:

      if a[i+1]>a[i]:
        increase_flag=True
        answer+=1
      elif a[i+1]<a[i]:
        decrease_flag=True
        answer+=1
      
    else:
      if decrease_flag==True and a[i+1]>a[i]:
        decrease_flag=False
      elif increase_flag==True and a[i+1]<a[i]:
        increase_flag=False

  if increase_flag==False and decrease_flag==False:
    if a[-1]>a[-2]:
      increase_flag=True
      answer+=1
    elif a[-1]<a[-2]:
      decrease_flag=True
      answer+=1
  print(answer)
else:
  print(1)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    N = int(input())
    A = [int(a) for a in input().strip().split()]
    f, p = 0, 0
    ans = 0
    for a in A:
        if f == 1:
            if a > p:
                f = 2
            elif a < p:
                f = 3
        elif f == 2:
            if a < p:
                ans += 1
                f = 1
        elif f == 3:
            if a > p:
                ans += 1
                f = 1
        elif f == 0:
            f = 1
        p = a
    else:
        ans += 1
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

n = int(input())
a = list(map(int, input().split()))
a_ = []
for i in a:
    if not a_:
        a_.append(i)
    elif i != a_[-1]:
        a_.append(i)
cnt = 0
flag = 0
for i in range(len(a_) - 1):
    if flag == 0:
        if a_[i + 1] - a_[i] > 0:
            flag = 1
        else:
            flag = -1
        continue
    if flag == 1 and a_[i + 1] - a_[i] < 0:
        cnt += 1
        flag = 0
    elif flag == -1 and a_[i + 1] - a_[i] > 0:
        cnt += 1
        flag = 0
print(cnt + 1)

Submission status: Accepted

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

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

cnt = 0
flag = 0

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations # (string,3) 3回
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
A = readInts()
cnt = 0
i = 0
while i < n:
    # まず同じ値を処理する
    if i+1 < n:
        while A[i+1] == A[i]:
            i += 1
    # その次の者が大きい?
    if i+1 < n:
        if A[i] <= A[i+1]:
            while A[i] <= A[i+1]:
                i += 1
                if i+1 < n:
                    pass
                else:
                    break
        else:
            while A[i] >= A[i+1]:
                i += 1
                if i + 1 < n:
                    pass
                else:
                    break
    cnt += 1
    i += 1
print(cnt)

Submission status: Accepted

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

increase = True
equal = True
start = True
count = 1

for i, a in enumerate(A):
    if i > 0:
        if equal:
            if a > A[i-1]:
                increase = True
                equal = False
            elif a < A[i-1]:
                increase = False
                equal = False
        else:
            if increase:
                if a < A[i-1]:
                    count += 1
                    equal = True
                    # print(i)
            else:
                if a > A[i-1]:
                    count += 1
                    equal = True
                    start = True
                    # print(i)

print(count)

Submission status: Accepted

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

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

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

ans += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

flag = [True]*len(d)

ans = 1
for i in range(len(d)-1):
    if flag[i]:
        if d[i] == -d[i+1]:
            ans += 1
            flag[i+1] = False
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
B = []
for a in A:
    if B:
        if B[-1] == a:
            continue
        else:
            B.append(a)
    else:
        B.append(a)

#print(B)
m = len(B)
X = []
for i in range(m):
    if i == 0:
        cur = B[0]
        cnt = 1
        flag = 0
    else:
        if flag == 0:
            if B[i] > cur:
                flag = 1
                cnt += 1
                cur = B[i]
            if B[i] < cur:
                flag = -1
                cnt += 1
                cur = B[i]
        elif flag == 1:
            if B[i] > cur:
                cnt += 1
                cur = B[i]
            else:
                X.append(cnt)
                cur = B[i]
                cnt = 1
                flag = 0
        else:
            if B[i] < cur:
                cnt += 1
                cur = B[i]
            else:
                X.append(cnt)
                cur = B[i]
                cnt = 1
                flag = 0
    #print(i, flag, cnt)
else:
    X.append(cnt)
#print(X)
print(len(X))

Submission status: Accepted

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

cnt = 1
sa = 0
for i in range(1, n):
    new = a[i] - a[i-1]
    if new * sa < 0:
        cnt += 1
        sa = 0
        continue
    if new != 0:
        sa = new
print(cnt)

Submission status: Accepted

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

before = a[0]
ok = "" #増加ならTrue
cnt = 0
for i in range(1,n):
  if ok == "":
    if before < a[i]:
      ok = True
    elif before > a[i]:
      ok = False
  elif ok:
    if before > a[i]:
      cnt += 1
      ok = ""
  elif not ok:
    if before < a[i]:
      cnt += 1
      ok = ""
  before = a[i]
print(cnt+1)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
cur = 0
cnt = 0
while cur<N:
    cnt += 1
    c = 0
    cur1 = cur+1
    for i in range(cur+1,N):
        if A[i]>=A[i-1]:
            cur1 = i+1
        else:
            cur1 = i
            break
    cur2 = cur+1
    for i in range(cur+1,N):
        if A[i]<=A[i-1]:
            cur2 = i+1
        else:
            cur2 = i
            break
    cur = max(cur1,cur2)
print(cnt)

Submission status: Accepted

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

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

print(cnt+1)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
up = dn = False
ans = 1
p = A[0]
for a in A:
    if p < a:
        up = True
    elif p > a:
        dn = True
    if up and dn:
        ans += 1
        up = dn = False
    p = a
print(ans)

Submission status: Accepted

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


def read():
    N = int(input().strip())
    A = list(map(int, input().strip().split()))
    return N, A


def solve(N, A):
    B = [A[0]]
    prev = A[0]
    for a in A:
        if prev != a:
            B.append(a)
            prev = a
    count = 1
    up = False
    down = False
    for i in range(1, len(B)):
        if not up and not down:
            if B[i-1] < B[i]:
                up = True
            else:
                down = True
        elif up and B[i-1] > B[i]:
            up = False
            count += 1
        elif down and B[i-1] < B[i]:
            down = False
            count += 1
    return count


if __name__ == '__main__':
    inputs = read()
    print("%s" % solve(*inputs))

Submission status: Accepted

Solution code:
import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n = int(ipt())
    a = [int(i) for i in ipt().split()]
    ans = 0
    t = 0
    pa = a[0]
    for i in a:
        if pa > i:
            if t > 0:
                t = 0
                ans += 1
            else:
                t = -1
        elif pa < i:
            if t < 0:
                t = 0
                ans += 1
            else:
                t = 1
        pa = i
    print(ans+1)

    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
# import math
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
# sys.setrecursionlimit(int(pow(10, 2)))
#sys.stdin = open("input.txt", "r")
#sys.stdout = open("output.txt", "w")
mod = int(pow(10, 9) + 7)
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]





# @lru_cache(None)

# def seive():
#     prime=[1 for i in range(10**6+1)]
#     prime[0]=0
#     prime[1]=0
#     for i in range(10**6+1):
#         if(prime[i]):
#             for j in range(2*i,10**6+1,i):
#                 prime[j]=0


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
c=1
d=0
for i in range(n-1):
  e=d+A[i+1]-A[i]
  if (d<e and d<0) or (d>e and d>0):
    c+=1
    e=0
  d=e
print(c)

Submission status: Accepted

Solution code:
N = int(input())
ans = 1
A = list(map(int, input().split()))
prev = A[0]
trend = 0
for a in A[1:]:
    if (a - prev) * trend < 0:
        ans += 1
        trend = 0
    elif trend == 0:
        if a - prev > 0:
            trend = 1
        elif a - prev < 0:
            trend = -1
    prev = a
print(ans)

Submission status: Accepted

Solution code:
num = int(input())
seq = list(map(int, input().split()))

prev = None
increase = None
count = 0

prev = seq[0]
for x in seq[1:]:
    if increase is True:
        if x >= prev:
            pass
        else:
            increase = None
            count += 1
    elif increase is False:
        if x <= prev:
            pass
        else:
            increase = None
            count += 1
    elif increase is None:
        if x == prev:
            pass
        elif x > prev:
            increase = True
        else:
            increase = False
    prev = x

count += 1
print(count)

Submission status: Accepted

Solution code:
__DEUBG__ = 0

import math
import sys

num = int(input())
seq = list(map(int, input().split()))

prev = None
tmp = []
increase = None
count = 0

def dump(x):
    from pprint import pprint
    pprint(x)

#breakpoint()

for x in seq:
    if __DEUBG__:
        print(x)
        print(tmp)
    if prev is None:
        tmp = [x]
    elif increase is True:
        if x >= tmp[-1]:
            tmp.append(x)
        else:
            if __DEUBG__:
                dump(tmp)
            tmp = [x]
            increase = None
            count += 1
    elif increase is False:
        if x <= tmp[-1]:
            tmp.append(x)
        else:
            if __DEUBG__:
                dump(tmp)
            tmp = [x]
            increase = None
            count += 1
    elif increase is None:
        if x == tmp[-1]:
            pass
        elif x > tmp[-1]:
            increase = True
        else:
            increase = False
        tmp.append(x)
    prev = x

if __DEUBG__:
    dump(tmp)

count += 1
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
count = 1
direction = 0
for a, b in zip(A, A[1:]):
	dif = b - a
	if direction == 0 and dif != 0:
		direction = dif // abs(dif)
	elif (direction == 1 and dif < 0) or (direction == -1 and dif > 0):
		count += 1
		direction = 0
print(count)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
count = 1
direction = 0
for a, b in zip(A, A[1:]):
	dif = b - a
	if direction == 0:
		if dif > 0:
			direction = 1
		elif dif < 0:
			direction = -1
	elif (direction == 1 and dif < 0) or (direction == -1 and dif > 0):
		count += 1
		direction = 0
print(count)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    res = 1
    plus = 0
    minus = 0
    for i in range(n-1):
        if a[i + 1] - a[i] > 0:
            plus += 1
        elif a[i + 1] - a[i] < 0:
            minus += 1
        if plus and minus:
            res += 1
            plus = 0
            minus = 0
    print(res)
if __name__ == '__main__':
    main()

Submission status: Accepted

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

    res = 1
    inc = 0
    dec = 0

    for i in range(n - 1):
        if A[i] < A[i+1]:
            inc += 1
        elif A[i] > A[i+1]:
            dec += 1
        
        if inc and dec:
            res += 1
            inc, dec = 0, 0
    
    print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def solve():
  n = int(input())
  a = list(map(int, input().split()))
  g, z = 0, 0
  cut = 1
  for i in range(n-1):
    if a[i+1] > a[i]:
      g += 1
    elif a[i+1] < a[i]:
      z += 1
    if g != 0 and z != 0:
      cut += 1
      g, z = 0, 0
  print(cut)
  return 0
 
if __name__ == "__main__":
  solve()

Submission status: Accepted

Solution code:
from itertools import tee


def pairwise(iterable):
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)


N = int(input())
A = list(map(int, input().split()))
cut = 0
previous = None
is_increase = False
is_decrease = False

for a in A:
    if previous is None:
        previous = a
    elif not (is_increase or is_decrease):
        if previous < a:
            is_increase = True
        elif previous > a:
            is_decrease = True
        previous = a
    elif is_increase and a < previous:
        cut += 1
        is_increase = False
    elif is_decrease and a > previous:
        cut += 1
        is_decrease = False

    previous = a

print(cut + 1)

Submission status: Accepted

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
from enum import Enum
#======================================================#
class State(Enum):
    UP = 1
    DOWN = 2
    HORI = 3

def main():
    n = II()
    prev, *aa = MII()
    state = State.HORI
    cnt = 1
    for a in aa:
        if prev < a:
            if state == State.HORI:
                state = State.UP
            elif state == State.DOWN:
                cnt += 1
                state = State.HORI
        elif prev > a:
            if state == State.HORI:
                state = State.DOWN
            elif state == State.UP:
                cnt += 1
                state = State.HORI
        prev = a
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

G = 0
Z = 0
ans = 1
for i in range(N-1):
  if A[i+1] > A[i]:
    G += 1
  elif A[i+1] < A[i]:
    Z += 1
  if G != 0 and Z != 0:
    ans += 1
    G = 0
    Z = 0
print(ans)

Submission status: Accepted

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

if n == 1:
    print(1)
    exit()
    
if al[1] > al[0]:
    now = 'U'
elif al[1] < al[0]:
    now = 'D'
else:
    now = 'E'

cnt = 1
for i in range(2, n):
    if now == 'U':
        if al[i] > al[i-1] or al[i] == 'E':
            now = 'U'
        elif al[i] < al[i-1]:
            cnt += 1
            i += 1
            now = 'E'
    elif now == 'D':
        if al[i] < al[i-1] or al[i] == 'E':
            now = 'D'
        elif al[i] > al[i-1]:
            cnt += 1
            now = 'E'
    elif now == 'E':
        if al[i] == al[i - 1]:
            now = 'E'
        elif al[i] > al[i - 1]:
            now = 'U'
        elif al[i] < al[i - 1]:
            now = 'D'
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
count = 1
increase = False
decrease = False
for i in range(1,N):
    if A[i-1] == A[i]:
        continue
    elif increase and A[i-1] < A[i]:
        continue
    elif decrease and A[i-1] > A[i]:
        continue
    elif not increase and not decrease and A[i-1] < A[i]:
        increase = True
    elif not increase and not decrease and A[i-1] > A[i]:
        decrease = True
    else:
        count += 1
        increase = False
        decrease = False
print(count)

Submission status: Accepted

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

count = 1
flag = 0

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

print(count)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math
    #from math import gcd

    #inf = 10**17
    #mod = 10**9 + 7

    n = int(input())
    a = list(map(int, input().split()))
    b = [a[0]]
    for i in a:
        if b[-1] == i:
            continue
        b.append(i)
    res = 1
    if len(b) <= 2:
        print(res)
    else:
        i = 1
        while i <= len(b)-2:
            if b[i] == max(b[i-1], b[i], b[i+1]) or b[i] == min(b[i-1], b[i], b[i+1]):
                res += 1
                i += 1
            i += 1
        print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

cnt, t = 0, 0
for i in range(1,n):
    if a[i] == a[i - 1] and t == 0:
        continue
    elif a[i] >= a[i - 1] and t >= 0:
        t = 1
    elif a[i] <= a[i - 1] and t <= 0:
        t = -1
    else:
        cnt += 1
        t = 0
else:
    cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
_ = input()
A = map(int, input().split())
prev = next(A)
up = None
count = 1
for i in A:
    if prev < i:
        if up is None:
            up = True
        elif not up:
            count += 1
            up = None
    elif prev > i:
        if up is None:
            up = False
        elif up:
            count += 1
            up = None
    prev = i
print(count)

Submission status: Accepted

Solution code:
N = int(input())
pre = None
is_inc = None
ans = 1
for num in list(map(int, input().split())):
    if pre is None:
        pre = num
        continue

    if is_inc is None and num > pre:
        is_inc = True
    elif is_inc is None and num < pre:
        is_inc = False
    elif (is_inc and num < pre) or (not is_inc and num > pre):
        ans += 1
        is_inc = None

    pre = num

print(ans)

Submission status: Accepted

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

count = 1
prev = A[0]
up = False
down = False
for a in A[1:]:
    if prev < a:
        if up:
            count += 1
            up = False
        elif down:
            pass
        else: # 初回
            down = True
    elif prev > a:
        if up:
           pass
        elif down:
            count += 1
            down = False
        else: # 初回
            up = True
    else:
        pass
    prev = a

print(count)

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline

flagset = ['not increase', 'flat', 'not decrease']

def main():
    N = int(readline())
    A = list(map(int, readline().split()))

    cnt = 0
    A0 = A[0]
    flag = flagset[1]
    for i in range(N-1):
        Ai = A[i]
        Ai1 = A[i+1]
        if flag == 'flat':
            if Ai < Ai1:
                flag = 'not decrease'
            elif Ai > Ai1:
                flag = 'not increase'
            elif Ai == Ai1:
                flag = 'flat'
            else:
                cnt += 1
        elif flag == 'not decrease':
            if Ai <= Ai1:
                continue
            else:
                flag = 'flat'
                cnt += 1
        else:
            if Ai >= Ai1:
                continue
            else:
                flag = 'flat'
                cnt += 1
    cnt += 1
    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

if N == 1:
    print(1)
    exit()

if A[0] > A[1]:
    status = 'dec'

elif A[1] > A[0]:
    status = 'inc'

else:
    status = 'equal'


cnt = 0

for i in range(2,N):
    if status == 'dec' and A[i] > A[i-1]:
        cnt += 1
        status = 'none'
    
    elif status == 'inc' and A[i] < A[i-1]:
        cnt += 1
        status = 'none'
    
    elif status == 'equal' and A[i] > A[i-1]:
        status = 'inc'
    
    elif status == 'equal' and A[i] < A[i-1]:
        status = 'dec'
    
    elif status == 'none' and A[i] < A[i-1]:
        status = 'dec'
    
    elif status == 'none' and A[i] > A[i-1]:
        status = 'inc'
    
    elif status == 'none' and A[i] == A[i-1]:
        status = 'equal'

print(cnt+1)