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
input = sys.stdin.buffer.readline
N = int(input())
A = list(map(int, 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())
l=list(map(int,input().split()))

if n<=2:
  print(1)
  exit()
  
cnt=0

if l[0]>l[1]:
  ans="low"
elif l[0]<l[1]:
  ans="high"
else:
  ans="even"
  
for i in range(2,n):
  if l[i-1]>l[i] and (ans=="low" or ans=="even"):
    ans="low"
  elif l[i-1]<l[i] and (ans=="high" or ans=="even"):
    ans="high"
  elif l[i-1]==l[i]:
    continue
  else:
    cnt+=1
    ans="even"
    
print(cnt+1)

Submission status: Accepted

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

dir = 0
old = a[0]
ans = 1
for i in range(1, len(a)):
    now = a[i]
    if dir == 0:
        if old < now:
            dir = 1
        elif old > now:
            dir = -1
    elif dir == 1:
        if old > now:
            dir = 0
            ans += 1
    elif dir == -1:
        if old < now:
            dir = 0
            ans += 1
    old = now
print(ans)

Submission status: Accepted

Solution code:
# contest = 'agc013'
# problem = 'a'
# testcase = 3

# import sys
# import os
# current_path = os.getcwd().split('/')
# path = '/workspaces/atcoder_docker_sample/tmp/{}_{}/sample-{}.in'.format(
#     contest,problem,testcase
# )
# f = open(path, 'r')
# sys.stdin = f

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

if(n<3):
    print(1)
    exit()

ans = 1
state = 0
now = a[0]
for ai in a[1:]:
    if(state==0):
        if(now == ai):
            state = 0
        elif(now > ai):
            state = -1
        else:
            state = 1
    elif(state==1):
        if(now > ai):
            state = 0
            ans += 1
    else:
        if(now < ai):
            state = 0
            ans += 1
    now = ai

print(ans)

Submission status: Accepted

Solution code:
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right #func(リスト,値)
from heapq import heapify, heappop, heappush
from math import *

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
        
def sgn(x):
    if x==0:
        return x
    return x//abs(x)

N,A = ilint()
tmp = A[0]
ans = 1
s = 0
for a in A[1:]:
    if s==0:
        s = sgn(a-tmp)
        tmp = a
    elif s*(a-tmp)>=0:
        tmp = a
    else:
        ans += 1
        tmp = a
        s = 0
print(ans)

Submission status: Accepted

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

for i in range(N-1):
    if sw=="1":
        if A[i]>A[i+1]:
            sw="2"
        if A[i]<A[i+1]:
            sw="3"
    elif sw=="2":
        if A[i]<A[i+1]:
            ans+=1
            sw="1"
    elif sw=="3":
        if A[i]>A[i+1]:
            ans+=1
            sw="1"

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int,input().split()))
zoka = 0
before = As[0]
c = 1
for i in range(1,N):
    if before < As[i]:
        if zoka == 0 or zoka == 1:
            zoka = 1
        else:
            c+=1
            zoka = 0
        before = As[i]
    elif before > As[i]:
        if zoka == 0 or zoka == 2:
            zoka = 2
        else:
            c+=1
            zoka = 0
        before = As[i]
print(c)

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int,input().split()))
zoka = 0
before = As[0]
c = 1
for i in range(1,N):
    if before == As[i]:
        continue
    elif before < As[i]:
        if zoka == 0 or zoka == 1:
            before = As[i]
            zoka = 1
        else:
            c+=1
            zoka = 0
            before = As[i]
    else:
        if zoka == 0 or zoka == 2:
            before = As[i]
            zoka = 2
        else:
            c+=1
            zoka = 0
            before = As[i]
print(c)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
if N==1:
    print(1)
    exit()

B=[A[i]-A[i-1] for i in range(1,N)]
ans=0
flag=False
for i in range(N-1):
    if B[i]!=0:
        first=B[i]
        flag=True
    if flag:
        break
for i in range(N-1):
    if first*B[i]>=0:
        pass
    else:
        ans+=1
        j=i+1
        while j<=N-2:
            if B[j]!=0:
                first=B[j]
                break
            else:
                j+=1

print(ans+1)

Submission status: Accepted

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

L = [A[0]]
for i in range(1, N):
  if A[i] != A[i - 1]:
    L.append(A[i])

N = len(L)

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

f = L[1] > L[0]
ans = 1

i = 2
while i < N:
  if (L[i] > L[i - 1]) != f:
    ans += 1
    if i == N - 1:
      break
    i += 1
    f = L[i] > L[i - 1]

  i += 1
 
print(ans)

Submission status: Accepted

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

c = 1; w = 0
for i in range(N-1):
    if w == 0:
        if A[i] < A[i+1]:
            w = 1
        elif A[i] > A[i+1]:
            w = -1
    elif (w > 0 and A[i] > A[i+1]) or (w < 0 and A[i] < A[i+1]):
         w = 0
         c += 1

print(c)

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
count=0
i=0
while i < n:
    j=i
    while j < n-1 and A[j]<=A[j+1]:
        j += 1
    k=i
    while k < n-1 and A[k]>=A[k+1]:
        k += 1
    count += 1
    i = max(j,k)+1
print(count)

Submission status: Accepted

Solution code:
# vim: fileencoding=utf-8


def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = 1
    flg = 0
    cursol = 0
    for i in a:
        if cursol == 0:
            cursol = i
            continue
        if flg == 0:
            if i > cursol:
                flg = 1
                cursol = i
            elif i < cursol:
                flg = -1
                cursol = i
        elif flg == 1:
            if i >= cursol:
                cursol = i
            elif i < cursol:
                cursol = i
                flg = 0
                ans += 1
        else:
            if i <= cursol:
                cursol = i
            elif i > cursol:
                cursol = i
                flg = 0
                ans += 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

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

flag = 0
ans = 1
tmp = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

count = 1
up_flag = None
prev_a = a_list[0]
for a in a_list[1:]:
  if up_flag is None:
    if prev_a == a:
      continue
    up_flag = a > prev_a
  if up_flag and a >= prev_a:
    prev_a = a
    continue
  elif not up_flag and a <= prev_a:
    prev_a = a
    continue
  else:
    up_flag = None
    count += 1
    prev_a = a

print(count)

Submission status: Accepted

Solution code:
from collections import deque
 
n = int(input())
a = list(map(int,input().split()))
d = deque(a)
 
tmp = []
cnt = 0
while d:
  v = d.popleft()
  if len(tmp)>=2:
    if not (v >= tmp[-1] >= tmp[0] or v <= tmp[-1] <= tmp[0]):
      tmp = []
      cnt += 1
  tmp.append(v)

print(cnt+1 if tmp else cnt)

Submission status: Accepted

Solution code:
from collections import deque
 
n = int(input())
a = list(map(int,input().split()))
d = deque(a)
 
tmp = []
cnt = 0
while d:
  v = d.popleft()
  if len(tmp)<=1:
    pass
  else:
    if not (v >= tmp[-1] >= tmp[-2] >= tmp[0] or v <= tmp[-1] <= tmp[-2] <= tmp[0]):
      tmp = []
      cnt += 1
  tmp.append(v)
#  print(d,tmp,cnt)
if tmp:
  cnt+=1
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(n-1):
  if a[i+1]==a[i]:
    continue
  x.append(a[i+1]-a[i])
  
flag=True

for i in range(len(x)-1):
  if flag==False:
    flag=True
    continue
  if x[i]<0 and x[i+1]>0 or x[i]>0 and x[i+1]<0:
    ans+=1
    flag=False
    

print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/agc013/tasks/agc013_a


def solve():
    n = int(input())
    A = [int(i) for i in input().split()]
    ans = 0
    cur = []
    flag = None
    for a in A:
        if not cur:
            cur.append(a)
            continue
        elif cur[-1] == a:
            cur.append(a)
        elif flag is None:
            if cur[-1] > a:
                flag = False
            elif cur[-1] < a:
                flag = True
            cur.append(a)
            continue
        elif flag:  # increasing
            if cur[-1] > a:
                ans += 1
                cur = [a]
                flag = None
            else:
                cur.append(a)
        elif not flag:  # decreasing
            if cur[-1] < a:
                ans += 1
                cur = [a]
                flag = None
            else:
                cur.append(a)
    if cur:
        ans += 1
    print(ans)


solve()

Submission status: Accepted

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

cnt = 1
top = True
    
for i in range(1,N):
    if A[i] == A[i-1]:
        continue
    if top == True:
        inc = (A[i]>A[i-1])
        top = False
        continue
    
    flag = (A[i]>A[i-1])
    if inc != flag:
        cnt += 1
        top = True
        
print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n = I()
    a = LI()
    tp = 0
    ans = 1
    
    for i in range(1,n):
        before = a[i-1]
        after = a[i]

        if tp == 0:
            if before < after:
                tp = 1
            elif before > after:
                tp = -1
        elif tp == 1:
            if before > after:
                ans += 1
                tp = 0
        elif tp == -1:
            if before < after:
                ans += 1
                tp = 0
        
    print(ans)



main()

Submission status: Accepted

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

last = 0
ans = 1

for i in range(N - 1):
    diff = A[i] - A[i + 1]
    if last == 0:
        if diff > 0:
            last = 1
        elif diff < 0:
            last = -1
    elif last * diff < 0:
        ans += 1
        last = 0

print(ans)

Submission status: Accepted

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

N = k()
A = l()
p = 0
ans = 1
for i in range(1, N):
    if p*(A[i]-A[i-1])<0:
        ans += 1
        p = 0
    elif A[i]-A[i-1] != 0:
        p = A[i]-A[i-1]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect, copy, heapq, math, sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product

def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
def celi(a,b):
    return -(-a//b)
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

ans=0
flag=-1

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

print(ans+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
N = int(input())
A = [int(x) for x in input().split()]
if N == 1:
    print(1)
    sys.exit()
 
inc = 0  # 1: 単調非減少,　-1 : 単調非増加, 0: 未確定
ans = 1
for i in range(N - 1):
    if A[i] < A[i + 1]:
        if inc == -1:
            ans += 1
            inc = 0
        elif inc == 0:
            inc = 1
    elif A[i] > A[i + 1]:
        if inc == 1:
            ans += 1
            inc = 0
        elif inc == 0:
            inc = -1
 
print(ans)

Submission status: Accepted

Solution code:

def sign(x):
    return (x > 0) - (x < 0)


def main():
    _ = int(input())
    a = map(int, input().split())
    ans = []
    arr = []
    state = 0
    for an in a:
        if len(arr) < 2:
            arr.append(an)
        else:
            state += (arr[-1] - arr[-2])
            if state == 0:
                arr.append(an)
            else:
                current = an - arr[-1]
                if current == 0 or sign(state) == sign(current):
                    arr.append(an)
                else:
                    ans.append(arr)
                    arr = [an]
                    state = 0
    ans.append(arr)

    print(len(ans))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def check(a,b):
  if a<b:
    return 'up'
  if a>b:
    return 'down'
  return ''

def solve():
  ans = 1
  N = int(input())
  A = list(map(int, input().split()))
  ud = ''
  for i in range(N-1):
    a = check(A[i],A[i+1])
    if ud=='' or ud==a:
      ud = a
      continue
    if a=='':
      continue
    ans += 1
    ud = ''
  return ans
print(solve())

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = []
i = 0
while i < N - 1:
    if A[i] != A[i+1]:
        B.append(A[i])
    i += 1
B.append(A[-1])

ans = 1
i = 0
while i < len(B) - 2:
    if (B[i + 1] - B[i]) * (B[i + 2] - B[i + 1]) < 0:
        ans += 1
        i += 2
    else:
        i += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#
# agc013 a
#

import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """6
1 2 3 2 2 1"""
        output = """2"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """9
1 2 1 2 1 2 1 2 1"""
        output = """5"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """7
1 2 3 2 1 999999999 1000000000"""
        output = """3"""
        self.assertIO(input, output)


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

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


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
from collections import deque

N = int(input())
A_deque = deque(int(e) for e in input().split())

section = list()
section.append(A_deque.popleft())
ans = 1
mode = 0

while 1:
    if len(A_deque) == 0:
        break
    elif mode == 0:
        next = A_deque.popleft()
        if section[-1] == next:
            section.append(next)
        elif section[-1] > next:
            section.append(next)
            mode = -1
        elif section[-1] < next:
            section.append(next)
            mode = 1
    elif mode == 1:
        next = A_deque.popleft()
        if section[-1] > next:
            section.clear()
            section.append(next)
            ans += 1
            mode = 0
        else:
            section.append(next)
    elif mode == -1:
        next = A_deque.popleft()
        if section[-1] < next:
            section.clear()
            section.append(next)
            ans += 1
            mode = 0
        else:
            section.append(next)

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
i=0
a=1
t=0
while i<N-1:
    if A[i]<A[i+1]:
        if t==-1:
            a+=1
            t=0
        elif not t:
            t=1
    elif A[i]>A[i+1]:
        if t==1:
            a+=1
            t=0
        elif not t:
            t=-1
    i+=1
print(a)

Submission status: Accepted

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

	is_increasing = True
	is_first = True
	ans = 1

	for i in range(1, N):
		if is_first == True:
			if A[i - 1] < A[i]:
				is_increasing = True
				is_first = False
			elif A[i - 1] > A[i]:
				is_increasing = False
				is_first = False
		else:
			if is_increasing == True and A[i - 1] > A[i]:
				ans += 1
				is_first = True
			elif is_increasing == False and A[i - 1] < A[i]:
				ans += 1
				is_first = True

	print(ans)



 
if __name__ == "__main__":
  	main()

Submission status: Accepted

Solution code:
import sys


stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


def main():
    n = ni()
    A = nl()
    D = [A[i + 1] - A[i] for i in range(n - 1)]

    for d in D:
        if d != 0:
            if d > 0:
                state = 1
            else:
                state = -1
            break
    else:
        print(1)
        return

    ans = 1
    cnt = 0

    for d in D:
        if d > 0:
            c_state = 1
            cnt += 1
        elif d < 0:
            c_state = -1
            cnt += 1
        else:
            continue
        if c_state != state:
            if cnt != 1:
                ans += 1
                cnt = 0
            state = c_state
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

while i<N-2-sim:
  if list[i] != list[i+1]:
    count += 1
    i+=1
  i+=1

print(count + 1)

Submission status: Accepted

Solution code:
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [input() for _ in range(n)]

inc = False
dec = False
val = 0
ans = 1
n = int(input())
a = list(map(int, input().split()))

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

Submission status: Accepted

Solution code:
def solve(): 

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

    i = 0
    result = 0

    while i < N:
        while i+1 < N and A[i] == A[i+1]:
            i += 1
        if i+1 < N and A[i] < A[i+1]:
            while i+1 < N and A[i] <= A[i+1]:
                i += 1
        elif i+1 < N and A[i] > A[i+1]:
            while i+1 < N and A[i] >= A[i+1]:
                i += 1
        
        result += 1
        i += 1

    print(result)    

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

ans = 0

up = None

for i in range(1, N):
    if up == None:
        if not A[i] == A[i - 1]:
            up = A[i] > A[i - 1]
            
    elif up:
        if A[i] < A[i - 1]:
            ans += 1
            up = None

    else:
        if A[i] > A[i - 1]:
            ans += 1
            up = None

print(ans + 1)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = 1
cur = 'N'
i = 1
while i < N:
    if cur == 'U':
        if A[i] < A[i-1]:
            ans += 1
            cur = 'N'
        i += 1
    elif cur == 'D':
        if A[i] > A[i-1]:
            ans += 1
            cur = 'N'
        i += 1
    elif cur == 'N':
        if A[i] > A[i-1]:
            cur = 'U'
        elif A[i] < A[i-1]:
            cur = 'D'
        i += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
ans = 0
now = 0
m = 0
c = 0
for i in l:
    if now == 0:
        now = i
    else:
        if m == 0:
            if now < i:
                m = 1
            elif now > i:
                m = -1
        elif m == 1:
            if now > i:
                ans += 1
                m = 0
            
        else:
            if now < i:
                ans += 1
                m = 0
        now = i

print(ans+1)

Submission status: Accepted

Solution code:
import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

n = inint()
A = inintl()

prev = A[0]
ans = 1
sign = 0

for i in range(1,n):
    if A[i] == prev:
        continue
    if A[i] > prev:
        if sign == -1:
            ans += 1
            prev = A[i]
            sign = 0
            continue
        else:
            sign = 1
            prev = A[i]
            continue
    if A[i] < prev:
        if sign == 1:
            ans += 1
            prev = A[i]
            sign = 0
            continue
        else:
            sign = -1
            prev = A[i]
            continue

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
b = A[0]
att = ''
cnt = 1
for a in A:
    # print(a,b,att)
    if a != b:
        if att == '':
            if a > b:
                att = '+'
            elif a < b:
                att = '-'
        elif att == '+':
            if a < b:
                cnt += 1
                att = ''
        elif att == '-':
            if a > b:
                cnt += 1
                att = ''
    b = a
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
num = 0
count = 1
mode = 0
while num < n - 1:
    if a[num] == a[num + 1]:
        num += 1
    elif a[num] > a[num + 1]:
        num += 1
        if mode == -1:
            pass
        elif mode == 0:
            mode = -1
        else:
            count += 1
            mode = 0
    else:
        num += 1
        if mode == 1:
            pass
        elif mode == 0:
            mode = 1
        else:
            count += 1
            mode = 0
print(count)

Submission status: Accepted

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

l = []
ans = 1
flag = True
for i in range(1, len(a)):
    num = a[i] - a[i - 1]
    if num > 0:
        num = 1
        l.append(num)
    elif num < 0:
        num = -1
        l.append(num)
    else:
        pass
if len(l) > 1:
    for i in range(len(l) - 1):
        if l[i] != l[i + 1]:
            if flag == True:
                ans += 1
                flag = False
            else:
                flag = True
        else:
            flag = True
print(ans)

Submission status: Accepted

Solution code:
import sys

N=int(input())
A=list(map(int,input().split()))
div_flg=False
inc_flg=False
dec_flg=False
ans=0

if N==1:
  print(1)
  sys.exit()
  
for i in range(N):
  if inc_flg:
    if i==N-1:
      break
    if A[i]<=A[i+1]:
      continue
    else:
      inc_flg=False
      div_flg=False
  elif dec_flg:
    if i==N-1:
      break
    if A[i]>=A[i+1]:
      continue
    else:
      dec_flg=False
      div_flg=False
  else:
    if i==0:
      ans+=1
      div_flg=True
    elif i==N-1:
      if not div_flg:
        ans+=1
      break
    if A[i]==A[i+1]:
      if not div_flg:
        ans+=1
      div_flg=True
      continue
    elif A[i]<A[i+1]:
      if not div_flg:
        ans+=1
      inc_flg=True
      continue
    elif A[i]>A[i+1]:
      if not div_flg:
        ans+=1
      dec_flg=True
      continue
print(ans)

Submission status: Accepted

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

cnt = 1

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

print(cnt)

Submission status: Accepted

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

tmp=[]
for i in range(1,n):
    if a[i]>a[i-1]:
        tmp.append(1)
    elif a[i]==a[i-1]:
        tmp.append(0)
    else:
        tmp.append(-1)

flg=0
swi=0
for j in range(len(tmp)):
    if swi==1:
        flg=tmp[j]
        swi=0
        continue
 
    if flg==0:
        flg=tmp[j]
    elif flg==1:
        if tmp[j]==-1:
            cnt+=1
            flg=-1
            swi=1
    elif flg==-1:
        if tmp[j]==1:
            cnt+=1
            flg=1
            swi=1

print(cnt+1)

Submission status: Accepted

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

  ans = 1
  x=y=0
  for i in range(1, N):

    if A[i - 1] < A[i]:
      x = 1

    if A[i - 1] > A[i]:
      y = 1

    if x and y:
      ans += 1
      x = y = 0

  print(ans)

main()