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()))
def solve(N,A):
    ans = 1
    lis = []
    lis.append(A[0])
    a = A[0]
    for i in range(1,N):
        if len(lis)==1 or prev==0:
            prev = A[i]-a
        elif (A[i]-a)*prev<0:
            ans += 1
            lis = []
        lis.append(A[i])
        a = A[i]
    return ans
print(solve(N,A))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
def solve(N,A):
    liss = []
    lis = []
    lis.append(A[0])
    a = A[0]
    for i in range(1,N):
        if len(lis)==1 or prev==0:
            prev = A[i]-a
        elif (A[i]-a)*prev<0:
            liss.append(lis)
            lis = []
        lis.append(A[i])
        a = A[i]
    liss.append(lis)
    ans = len(liss)
    return ans
print(solve(N,A))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
def solve(N,A):
    liss = []
    lis = []
    lis.append(A[0])
    a = A[0]
    for i in range(1,N):
        if len(lis)==1 or prev==0:
            if A[i]>a:
                prev = 1
            elif A[i]<a:
                prev = -1
            else:
                prev = 0
        elif (A[i]-a)*prev<0:
            liss.append(lis)
            lis = []
        lis.append(A[i])
        a = A[i]
    liss.append(lis)
    ans = len(liss)
    return ans
print(solve(N,A))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
def solve(N,A):
    liss = []
    lis = []
    lis.append(A[0])
    a = A[0]
    for i in range(1,N):
        if len(lis)==1 or prev==0:
            if A[i]>a:
                prev = 1
            elif A[i]<a:
                prev = -1
            else:
                prev = 0
        elif ((A[i]>a)+(A[i]>=a)-1)*prev<0:
            liss.append(lis)
            lis = []
        lis.append(A[i])
        a = A[i]
    liss.append(lis)
    ans = len(liss)
    return ans
print(solve(N,A))

Submission status: Accepted

Solution code:
si = lambda: input().strip()
ii = lambda: int(input())
mii = lambda: map(int, input().split())
lmii = lambda: list(map(int, input().split()))
lmsi = lambda: list(map(str, input().split()))
smii = lambda: sorted(map(int, input().split()))

# ----------

N = ii()
A = lmii()

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

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
c = 1
x = 0
for i in range(n-1):
    y = a[i+1]-a[i]
    if x*y<0:
        if i<n-2:x = a[i+2]-a[i+1]
        c+=1
    elif y!=0:x=y
print(c)

Submission status: Accepted

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

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

    else:
        if plus:
            if A[i] <= A[i+1]:
                continue
            else:
                ans += 1
                plus = False
        else:
            if A[i] >= A[i+1]:
                continue
            else:
                ans += 1
                minus = False
print(ans+1)

Submission status: Accepted

Solution code:
# map(int, input().split())
# list(map(int, input().split()))
# for _ in range(int(input())):
# print(' '.join([str(x) for x in ans]))

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

inc = -1
num = 1

prev = a[0]
for x in a:
    if x>prev:
        if inc == -1:
            inc = 1
        elif inc == 0:
            num += 1
            inc = -1
    elif x < prev:
        if inc == -1:
            inc = 0
        elif inc == 1:
            num += 1
            inc = -1
    prev = x

print(num)

Submission status: Accepted

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

ans = 0
u_d = -1
for i, a in enumerate(a_list):
    if i == 0:
        tmp = a
        continue
    
    if a-tmp == 0:
        tmp = a
        continue

    if u_d == -1 and a > tmp:
        u_d = 1 # 上の時1
        tmp = a
        continue
    elif u_d == -1 and a < tmp:
        u_d = 0 # 下の時0
        tmp = a
        continue
    #print(a, u_d, ans)
     
    if a > tmp:
        if u_d == 1:
            tmp = a
            #print('############################33')
            continue
        elif u_d == 0:
            ans += 1
            u_d = -1
            tmp = a

    elif a < tmp:
        if u_d == 0:
            tmp = a
            #print('############################33')
            continue
        elif u_d == 1:
            ans += 1
            u_d = -1
            tmp = a

    #print(a, u_d, ans)
    #print('############################33')

print(ans+1)

Submission status: Accepted

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

order = 0
subs = []
tmp = []

prev = 0
d = []
t = [l[0]]
for x in range(len(l)-1):
  a,b = l[x],l[x+1]
  c = a-b
  # print(a,b,c, prev)
  if c == 0:
    t.append(b)
  elif prev == 0:
    t.append(b)
    prev = c
  elif (c < 0  and prev < 0) or (c > 0 and prev > 0):
    t.append(b)
  else:
    prev = 0
    d.append(t)
    t = [b]
d.append(t)

print(len(d))

Submission status: Accepted

Solution code:
# from math import factorial,sqrt,ceil,gcd
# from itertools import permutations as permus
# from collections import deque,Counter
# import re
# from functools import lru_cache # 簡単メモ化 @lru_cache(maxsize=1000)
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np
# import networkx as nx
# from scipy.sparse.csgraph import shortest_path, dijkstra, floyd_warshall, bellman_ford, johnson
# from scipy.sparse import csr_matrix
# from scipy.special import comb

# slist = "abcdefghijklmnopqrstuvwxyz"
N = int(input())
arrA = list(map(int,input().split()))
# arrA = np.array(input().split(),dtype=np.int64)
plusOrMinus = 0
num = arrA[0]
ans = 0
for i in range(N-1):
    if plusOrMinus == "+":
        if arrA[i]>arrA[i+1]:
            ans += 1
            plusOrMinus = 0
    elif plusOrMinus == "-":
        if arrA[i]<arrA[i+1]:
            ans += 1
            plusOrMinus = 0
    else:
        if arrA[i]<arrA[i+1]:
            plusOrMinus = "+"
        elif arrA[i]>arrA[i+1]:
            plusOrMinus = "-"
        else:
            pass

print(ans+1)
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する
# print("{:.10f}".format(ans))
# print("{:0=10d}".format(ans))

Submission status: Accepted

Solution code:
def myAnswer(N:int,A:list) -> int:
   if(N == 1): return 1
   pre = A.pop(0)
   counter = 1
   state = 0 # 0:どちらでもない状態、1:単調非減少、2:単調非増加
   for a in A:
      if(pre == a):
         pre = a
         continue
      if(state == 0):
         if(pre < a):
            state = 1
         else:
            state = 2
      elif(pre < a and state == 2):
         counter += 1
         state = 0
      elif(pre > a and state == 1):
         counter += 1
         state = 0
      pre = a
   return counter

def modelAnswer():
   return
def main():
   N = int(input())
   A = list(map(int,input().split()))
   print(myAnswer(N,A[:]))
if __name__ == '__main__':
   main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
o = b = c = 0;input()
for i, x in enumerate(input().split()):
    x = int(x)
    s = x - b if i else 0
    d = (s>0)-(s<0)
    if abs(d - o) > 1:
        c += 1
        o = 0
    else:
      o = d or o
    b = x
print(c+1)

Submission status: Accepted

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

n = int(input())

nums = [int(i) for i in input().split()]

ans = 1

flag = None
pre = nums[0]
for i in range(n):
    if pre == nums[i]:
        continue
    elif pre < nums[i]:
        if flag == False:
            ans += 1
            flag = None
        else:
            flag = True
    else:
        if flag == True:
            ans += 1
            flag = None
        else:
            flag = False
    pre = nums[i]
print(ans)

Submission status: Accepted

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

status = 0
cnt = 1
for i in range(n - 1):
    #print(a[i], status)
    # neutral&break:0 <:1 >:2 
    if a[i] == a[i + 1]:
        #print('==')
        continue
    elif a[i] < a[i + 1]:
        if status == 0 or status == 1:
            #print('con<')
            status = 1
            continue
        else:
            #print('break&<')
            status = 0
            cnt += 1
    else:
        if status == 0 or status == 2:
            #print('con>')
            status = 2
            continue
        else:
            #print('break&>')
            status = 0
            cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=1
bef=a[0]
flag=None #前との大小関係 true 増加　false 減少
for i in range(1,n):
    #if bef==a[i] イコールのときは問題ないので処理いらない
    if bef<a[i]:
        if flag==False:
            ans+=1
            flag=None
        else:
            flag=True
    elif bef>a[i]: 
        if flag:
            ans+=1
            flag=None
        else:
            flag=False
    bef=a[i]
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
n = int(input())
A = list(map(int, input().split()))

inc = None
c = 0
 
for i in range(n-1):
    if inc == None:
        if A[i] == A[i+1]:
            pass
        elif A[i] < A[i+1]:
            inc = True
        else:
            inc = False
    elif inc:
        if A[i] > A[i+1]:
            c += 1
            inc = None
    else:
        if A[i] < A[i+1]:
            c += 1
            inc = None

print(c+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
import sys
import math

N = int(stdin.readline().rstrip())
A = list(map(int, stdin.readline().rstrip().split()))

ASC = -1
DESC = 1

flag_asc_desc = 0
count = 1
prev = 0
for i in A:
    if prev == 0:
        prev = i
        flag_asc_desc = 0
        continue
    
    if flag_asc_desc == ASC and i >= prev:
        prev = i
        continue
    elif flag_asc_desc == ASC and i < prev:
        prev = i
        flag_asc_desc = 0
        count += 1
    elif flag_asc_desc == DESC and i <= prev:
        prev = i
        continue
    elif flag_asc_desc == DESC and i > prev:
        prev = i
        flag_asc_desc = 0
        count += 1
    elif i == prev:
        prev = i
        continue
    elif i < prev:
        flag_asc_desc = DESC
        prev = i
    elif i > prev:
        flag_asc_desc = ASC
        prev = i
    else:
        print("error")

print(count)

Submission status: Accepted

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

Submission status: Accepted

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

last = alist[0]
count = 1
status = 0  # 0-start, 1-asc, 2-desc
for a in alist[1:]:
    if (status == 1 and last > a) or (status == 2 and last < a):
        count += 1
        status = 0
    elif status == 0 and last > a:
        status = 2
    elif status == 0 and last < a:
        status = 1
    last = a
print(count)

Submission status: Accepted

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

ans = 1
inc = 0

if N == 1:
    print(ans)

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

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
if len(set(l))==1:print(1);exit()
from itertools import groupby as gb
l=[a for a,b in gb(l)]
ans=1
now=0  if l[0]<l[1] else 1
pre=l[0]
n=len(l)
for i in range(n):
    if pre<l[i] and now:
        if i==n-1:print(ans+1);exit()
        else:
            now=0  if l[i]<l[i+1] else 1
            ans+=1
    elif pre>l[i] and 1-now:
        if i==n-1:print(ans+1);exit()
        else:
            now=0  if l[i]<l[i+1] else 1
            ans+=1
    pre=l[i]
print(ans)

Submission status: Accepted

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

f = 0
i = 0
ans = 1
for i in range(n-1):
  if f==0 and A[i+1]-A[i] > 0:
    f = 1
  elif f==0 and A[i+1]-A[i] < 0:
    f = -1
  elif (f < 0 and A[i+1]-A[i] > 0) or (f > 0 and A[i+1]-A[i] < 0):
    f = 0
    ans += 1
    
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
if len(set(l))==1:print(1);exit()
from itertools import groupby as gb
l=[a for a,b in gb(l)]
ans=1
now=0  if l[0]<l[1] else 1
pre=l[0]
n=len(l)
for i in range(n):
    if pre<l[i] and now:
        if i==n-1:print(ans+1);exit()
        else:
            now=0  if l[i]<l[i+1] else 1
            ans+=1
    elif pre>l[i] and 1-now:
        if i==n-1:print(ans+1);exit()
        else:
            now=0  if l[i]<l[i+1] else 1
            ans+=1
    pre=l[i]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
flag = None #True:増加  False:減少
temp = A[0]
result = 1
for a in A:
    if a == temp:
        continue
    if flag == None:
        if a > temp:
            flag = True
        else:
            flag = False
    elif (flag and a < temp) or (not flag and a > temp):
        flag = None
        result += 1
    temp = a
print(result)

Submission status: Accepted

Solution code:
def main():
    import sys
    n=int(input())
    a=list(map(int, input().split()))
    ud=[]
    ans=1
    cnt=[0,0]
    if n==1:
        print(ans)
        sys.exit()
    for i in range(n-1):
        if a[i]==a[i+1]:
            ud.append('eq')
        elif a[i]<a[i+1]:
            ud.append('up')
        elif a[i]>a[i+1]:
            ud.append('down')
    if len(set(ud))==1 or (len(set(ud))==2 and 'eq' in set(ud)):
        print(ans)
        sys.exit()
    if ud[0]=='up':
        cnt[0]=1
    elif ud[0]=='down':
        cnt[1]=1
    for i in range(1,n-1):
        if (cnt[0]==cnt[1]==0 or cnt[0]!=0) and ud[i]=='up':
            cnt[0]+=1
        elif (cnt[0]==cnt[1]==0 or cnt[1]!=0) and ud[i]=='down':
            cnt[1]+=1
        elif cnt[0]!=0 and ud[i]=='down':
            ans+=1
            cnt[0]=0
        elif cnt[1]!=0 and ud[i]=='up':
            ans+=1
            cnt[1]=0
        elif cnt[0]!=0 and cnt[1]==0 and i==n-2:
            ans+=1
        elif cnt[1]!=0 and cnt[0]==0 and i==n-2:
            ans+=1
    print(ans)
    
if __name__=='__main__':
    main()

Submission status: Accepted

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

flag = 'n' #'u',d'
counter = 1
for i,l in enumerate(li):
    if i == len(li)-1:
        break
    if flag == 'n':
        if l > li[i+1]:
            flag = 'd'
        elif l < li[i+1]:
            flag = 'u'
    elif flag == 'u':
        if l > li[i+1]:
            counter += 1
            flag = 'n'
            new_flag = True
    elif flag == 'd':
        if l < li[i+1]:
            counter += 1
            flag = 'n'
            new_flag = True
    #print(i,l,li[i+1],flag)
print(counter)

Submission status: Accepted

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

cur=a_list[0]
pat=0 #0:change,1:asc,2:desc 
ans=0
for a in a_list:
    if a==cur:
        continue
    if pat==0:
        if cur<a:
            pat=1
        else:
            pat=2
        cur=a
        continue
    if pat==1 and cur>a:
        ans+=1
        pat=0
    elif pat==2 and cur<a:
        ans+=1
        pat=0

    cur=a
print(ans+1)

Submission status: Accepted

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

p = 0
cnt = 1

for i in range(N-1):
    if a[i] == a[i+1]:
        continue
    elif p == 0 and a[i] > a[i+1]:
        p = 1
    elif p == 0 and a[i] < a[i+1]:
        p = -1
    elif p == 1 and a[i] < a[i+1]:
        cnt += 1
        p = 0
    elif p == -1 and a[i] >a[i+1]:
        cnt += 1
        p = 0

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
an = list(map(int,input().split(' ')))
cnt = 1
is_increased = True
is_init = True
for i in range(1,N):
    if is_init:
        if an[i] > an[i-1]:
            is_increased = True
        elif an[i] < an[i-1]:
            is_increased = False
        elif an[i] == an[i-1]:
            continue
        is_init = False
    else:
        if is_increased:
            if an[i] >= an[i-1]:
                continue
            else:
                is_init = True
                cnt +=1
        else:
            if an[i] <= an[i-1]:
                continue
            else:
                is_init = True
                cnt +=1
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
line = [int(x) for x in input().split(' ')]
count = 1
prev = line[0]
i = 0
while i < len(line):
  if prev == line[i]:
    i += 1
    continue
  elif prev < line[i]:
    for j in range(i,len(line)):
      if line[j] < prev:
        count+=1
        break
      prev = line[j]
    i = j
  elif prev > line[i]:
    for j in range(i,len(line)):
      if line[j] > prev:
        count += 1
        break
      prev = line[j]
    i = j
  prev = line[i]
  i+=1
print(count)

Submission status: Accepted

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

ans=1
tmp=-1
#単調増加:1 単調非増加:0
chk=-1
flag=True
for i in range(N):
    if i==0:
        tmp=A[0]
        continue
    if flag:
        if A[i]>tmp:
            tmp=A[i]
            chk=1
            flag=False
        elif A[i]<tmp:
            tmp=A[i]
            chk=0
            flag=False
        else:
            tmp=A[i]
        continue
    if chk:
        if tmp<A[i]:
            tmp=A[i]
        elif tmp>A[i]:
            ans+=1
            flag=True
            chk=-1
            tmp=A[i]
    else:
        if tmp<A[i]:
            ans+=1
            flag=True
            chk=-1
            tmp=A[i]
        else:
            tmp=A[i]
        
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=1
flag=0
for i in range(n-1):
  if a[i+1]>a[i]:
    if flag==-1:
      ans+=1
      flag=0
    else:
      flag=1
  if a[i+1]<a[i]:
    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()))

f=''
ans=1
for i in range(1,n):
    if a[i-1]==a[i]:
        continue
    if a[i-1] < a[i]:
        if f=='>':
            ans+=1
            f=''
        else:
            f='<'
    else:
        if f=='<':
            ans+=1
            f=''
        else:
            f='>'
print(ans)

Submission status: Accepted

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

for i in range(n-1):
    if a[i] < a[i+1]:
        frag = True
        break
    elif a[i] > a[i+1]:
        frag = False
        break
skip = False
ans = 1

for i in range(n-1):
    beforefrag = frag
    if a[i] > a[i+1]:
        frag = False
    elif a[i] < a[i+1]:
        frag = True
    else:
        continue

    if skip:
        skip = False
        continue

    if beforefrag != frag:
        skip = True
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
count = 0
up = 0
down = 0
before = A[0]
tmp = 0
for i in range(1,N):
  tmp = A[i]
  if (up == 0) and (down == 0):
    if tmp > before:
      up = 1
      before = tmp
    elif tmp < before:
      down = 1
      before = tmp
    else:
      continue
  elif up == 1:
    if tmp < before:
      count += 1
      up = 0
      before = tmp
    else:
      before = tmp
  elif down == 1:
    if tmp > before:
      count += 1
      down = 0
      before = tmp
    else:
      before = tmp
count += 1
print(count)

Submission status: Accepted

Solution code:
# import string
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():

    N=int(input())
    A=list(map(int,input().split()))
    state=0
    temp=A[0]
    cnt=1
    for i in range(1,N):
        if state==0:
            if A[i]>temp:
                state=1
                temp=A[i]
            elif A[i]<temp:
                state=-1
                temp=A[i]
            else:
                temp=A[i]

        elif state==1:
            if A[i]<temp:
                state=0
                cnt+=1
                temp=A[i]
            else:
                temp=A[i]
        else:
            if A[i] > temp:
                state = 0
                cnt += 1
                temp = A[i]
            else:
                temp = A[i]
    print(cnt)
resolve()

Submission status: Accepted

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

a = A[0]
# equal: 0, inc: 1, dec: -1
state = 0
ans = 1
for i in range(1, N):
    if state == 0:
        if a < A[i]:
            state = 1
        elif a > A[i]:
            state = -1
    elif state == 1:
        if a > A[i]:
            ans += 1
            state = 0
    else:
        if a < A[i]:
            ans += 1
            state = 0
    a = A[i]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 1
up = None

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
incre = None
ans = 1
for i in range(n-1):
    if incre == None:
        if a[i] == a[i+1]:
            continue
        elif a[i] < a[i+1]:
            incre = True
        elif a[i] > a[i+1]:
            incre = False
    elif incre == True and a[i] > a[i+1]:
        incre = None
        ans += 1
    elif incre == False and a[i] < a[i+1]:
        incre = None
        ans += 1
print(ans)

Submission status: Accepted

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

increase=None
ans=0

for i in range(n):
    if i!=n-1:
        if increase==None:
            if a[i]==a[i+1]:
                pass
            elif a[i]<a[i+1]:
                increase=True
            else:
                increase=False
        elif increase==True:
            if a[i]>a[i+1]:
                ans+=1
                increase=None
        else:
            if a[i]<a[i+1]:
                ans+=1
                increase=None
ans+=1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
p = a[0]
ans = 1
stat = 'None'
for i, ai in enumerate(a, 1):
    if stat == 'None':
        if ai < p:
            stat = 'Down'
        elif ai > p:
            stat = 'Up'
    else:
        if ai < p and stat == 'Up':
            ans += 1
            stat = 'None'
        elif ai > p and stat == 'Down':
            ans += 1
            stat = 'None'
    p = ai
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# Problem A - Sorted Arrays

# input
N = int(input())
a_list = list(map(int, input().split()))

# initialization
ans = 0
pos = 0

# count
while pos<N:
    pos_1 = pos
    pos_2 = pos

    # up
    while pos_1<N-1 and a_list[pos_1]<=a_list[pos_1+1]:
        pos_1 += 1

    # down
    while pos_2<N-1 and a_list[pos_2]>=a_list[pos_2+1]:
        pos_2 += 1

    large = max(pos_1, pos_2) + 1
    pos = large
    ans += 1

# output
print(ans)

Submission status: Accepted

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

if N == 1:
    print(1)
elif N == 2:
    print(1)
else:
    A = [B[0]]
    cnt = 0
    for i in range(N - 1):
        if B[i] == B[i + 1]:
            pass
        else:
            A.append(B[i+1])

    i = 1
    ans = 1
    n = len(A)
    while i < n - 1:
        if A[i-1] < A[i] < A[i+1] or A[i-1] > A[i] > A[i+1]:
            pass
        else:
            i += 1
            ans += 1
        i += 1
    print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
count = 0
i = 0
while i<N-1:
    if a[i]<a[i+1]:
        i += 1
        while i<N-1:
            if a[i]<=a[i+1]:
                i+=1
            else:
                count+=1
                i+=1
                break
    elif a[i]>a[i+1]:
        i+=1
        while i<N-1:
            
            if a[i]>=a[i+1]: #
                i+=1
            else:
                count+=1
                i+=1
                break
    else:
        i+=1
        while i<N-1:
            if a[i]==a[i+1]:
                i+=1
            else:
                break
print(count+1)

Submission status: Accepted

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

ans = 0
plus = ""
for i in range(N):
  if plus != "":
    if plus:
      if A[i-1] > A[i]:
        ans += 1
        plus = ""
    else:
      if A[i-1] < A[i]:
        ans += 1
        plus = ""
  if i < N-1 and plus == "":
    if A[i] < A[i+1]:
      plus = True
    elif A[i] > A[i+1]:
      plus = False
print(ans+1)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

change = 0  # 1, 0, -1
ans = 1

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

print(ans)