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, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
debugmode = False

n = INT()
a = LIST()
mode = 0 #0未決定 1増加 2減少
ans = 1
f = a[0]
for i in range(1, n):
    if mode == 0 and a[i] == f:
        pass
    elif mode == 0 and a[i] < f:
        mode = 2
        f = a[i]
    elif mode == 0 and a[i] > f:
        mode = 1
        f = a[i]
    elif mode == 1 and a[i] == f:
        pass
    elif mode == 1 and a[i] > f:
        f = a[i]
    elif mode == 1 and a[i] < f:
        ans += 1
        mode = 0
        f = a[i]
    elif mode == 2 and a[i] == f:
        pass
    elif mode == 2 and a[i] > f:
        ans += 1
        mode = 0
        f = a[i]
    elif mode == 2 and a[i] < f:
        f = a[i]
print(ans)

Submission status: Accepted

Solution code:
def solve():
    n = int(input())
    a = list(map(int,input().split()))
    direction = 0 # 0なら未定　－１なら減少，１なら増加
    cnt = 1
    for i in range(1,n):
        if (a[i]-a[i-1])*direction<0:
            cnt+=1
            direction = 0
        else:
            if direction==0:
                direction = a[i]-a[i-1]
    print(cnt)
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
if n<=2:print(1);exit()
ans=1
chk=0
left=0
while chk<n-2:
  if (l[left]-l[chk+1])*(l[chk+1]-l[chk+2])<0:ans+=1;chk+=2;left=chk
  else:chk+=1
print(ans)

Submission status: Accepted

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

ans = 1
up = dn = False
for a,b in zip(A,A[1:]):
    if a < b:
        if up: continue
        up = True
        if dn:
            ans += 1
            up = dn = False
    elif a > b:
        if dn: continue
        dn = True
        if up:
            ans += 1
            up = dn = False
print(ans)

Submission status: Accepted

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from queue import Queue


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()))


sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    n = INT()
    a = LIST()

    ans = 0

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

    # if a[0] < a[1]:
    #     sign = 1
    # elif a[0] > a[1]:
    #     sign = -1
    # else:
    #     sign = 0

    sign = 0

    for i in range(n-1):
        if a[i] < a[i+1] and sign == -1:
            ans += 1
            sign = 0

        elif a[i] > a[i+1] and sign == 1:
            ans += 1
            sign = 0

        elif sign == 0:
            if a[i] < a[i+1]:
                sign = 1
            elif a[i] > a[i+1]:
                sign = -1

    print(ans + 1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from queue import Queue


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()))


sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    n = INT()
    a = LIST()

    ans = 0

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

    if a[0] < a[1]:
        sign = 1
    elif a[0] > a[1]:
        sign = -1
    else:
        sign = 0

    for i in range(n-1):
        if a[i] < a[i+1] and sign == -1:
            ans += 1
            sign = 0

        elif a[i] > a[i+1] and sign == 1:
            ans += 1
            sign = 0

        elif sign == 0:
            if a[i] < a[i+1]:
                sign = 1
            elif a[i] > a[i+1]:
                sign = -1

    print(ans + 1)


if __name__ == '__main__':
    main()

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 groupby, accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

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

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

print(ans+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys


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


sys.setrecursionlimit(10 ** 9)


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

    print(answer)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys


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


sys.setrecursionlimit(10 ** 9)


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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

judge = 'reset'
cnt = 1

# judgeがresetに替わった際にcnt++
for i in range(N-1):
    if A[i] < A[i+1]:
        if judge == 'reset':
            judge = 'high'
        elif judge == 'low':
            judge = 'reset'
            cnt += 1
    elif A[i] > A[i+1]:
        if judge == 'reset':
            judge = 'low'
        elif judge == 'high':
            judge = 'reset'
            cnt += 1

print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

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

# 重複要素の削除
A = []
idx = 0
while idx < N:
    A.append(tmp_A[idx])
    while idx + 1 < N and tmp_A[idx] == tmp_A[idx + 1]:
        idx += 1
    idx += 1


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

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

    print(ans + 1)

Submission status: Accepted

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

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

      
print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

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

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

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

n = onem()

a = l()

co = 0

po = a[0]

fl = 0

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


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

print(co+1)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=0
flug=0 #1:<= 2:>=

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

print(ans+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
debugmode = False

n = INT()
a = LIST()
b = 0 #0: 未定 1: 非減少 2: 非増加
ans = 0
arr = []
for aa in a:
    if len(arr) == 0:
        arr.append(aa)
        ans += 1
        b = 0
        continue
    if b == 0:
        arr.append(aa)
        if arr[-2] < arr[-1]:
            b = 1
        elif arr[-2] > arr[-1]:
            b = 2
    if b == 1:
        if arr[-1] <= aa:
            arr.append(aa)
        else:
            arr = [aa]
            ans += 1
            b = 0
    if b == 2:
        if arr[-1] >= aa:
            arr.append(aa)
        else:
            arr = [aa]
            ans += 1
            b = 0
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

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

Submission status: Accepted

Solution code:
from collections import deque

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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



print(ans)

Submission status: Accepted

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

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


print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

count = 1
state = 0  # 0 : 列の先頭、 1: 非減少, 2: 非増加

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

print(count)

Submission status: Accepted

Solution code:
import sys

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

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

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

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

Submission status: Accepted

Solution code:
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') #無限大
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(sys.stdin.readline()) #整数読み込み
def ifn(): return float(sys.stdin.readline()) #浮動小数点読み込み
def isn(): return sys.stdin.readline().split() #文字列読み込み
def imn(): return map(int, sys.stdin.readline().split()) #整数map取得
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) #整数-1map取得
def fmn(): return map(float, sys.stdin.readline().split()) #浮動小数点map取得
def iln(): return list(map(int, sys.stdin.readline().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def fln(): return list(map(float, sys.stdin.readline().split())) # 浮動小数点リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離
def m_add(a,b): return (a+b) % MOD
def print_list(l): print(*l, sep='\n')

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


N = iin()
A = iln()

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

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 flag == 0:
        if a[i] < a[i + 1]:
            flag = 1
        elif a[i] > a[i + 1]:
            flag = -1
    elif flag == 1:
        if a[i] < a[i + 1]:
            flag = 1
        elif a[i] > a[i + 1]:
            flag = 0
            cnt += 1
    elif flag == -1:
        if a[i] > a[i + 1]:
            flag = -1
        if a[i] < a[i + 1]:
            flag = 0
            cnt += 1
print(cnt + 1)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

prev = As[0]
status = 0 #-1 減少　1増加
cnt = 1

for a in As[1:]:
    if status == 0: #未確定
        if prev > a:
            status = -1
        elif prev < a:
            status = 1
    elif status == 1 :  #単調非減少
        if prev > a:
            status = 0
            cnt += 1
    else:               #単調非増加
        if prev < a:
            status = 0
            cnt += 1
    prev = a

print(cnt)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
# input = sys.stdin.r/eadline
def INT(): return int(input())
def MAP(): return map(int,input().split())
def LI(): return list(map(int,input().split()))

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

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

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

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

    answer += 1
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

a = [0]*len(l)

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

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

""" 広義 """

cnt = 1
flag = 0 # 0:未定、1:増加、-1:減少
for i,a in enumerate(A):
    if i == 0:
        prev = a
        continue

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

print(cnt)

Submission status: Accepted

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

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