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()))
c=1
g=0
for i in range(n-1):
  if (g==1 and a[i+1]<a[i]) or (g==-1 and a[i+1]>a[i]) :
    c+=1
    g=0
  elif g==0:
    if a[i+1]>a[i]:
      g=1
    elif a[i+1]<a[i]:
      g=-1
print(c)

Submission status: Accepted

Solution code:
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007

#+++++

def main():
	n = int(input())
	#b , c = tin()
	#s = input()
	al = lin()
	flg=None
	dd = 1
	pv=al[0]
	for v in al[1:]:
		if flg is None:
			if pv == v:
				pass
			elif pv < v:
				flg = 'up'
			else:
				flg = 'down'
		elif flg == 'up':
			if v < pv:
				dd += 1
				flg = None
		else:
			if v > pv:
				dd += 1
				flg = None
		pv = v
	print(dd)
				
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

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

li = []

for i in range(N-1):
    if A[i+1] - A[i] > 0:
       li.append('+')
    elif A[i+1] - A[i] < 0:
        li.append('-')
    else:
        pass
# print(li)
ans = 1
p = 0
m = 0
for i in li:
    if i == '+':
        p = 1
    elif i == '-':
        m = 1
    if p == 1 & m == 1:
        ans += 1
        p = 0
        m = 0
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[1]:


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


# In[3]:


ans = 1
p = 0
m = 0
pre = A[0]
for a in A[1:]:
    if a > pre:
        p = 1
    elif a < pre:
        m = 1
    if p == 1 and m == 1:
        ans += 1
        p = 0
        m = 0
    pre = a
print(ans)


# In[ ]:

Submission status: Accepted

Solution code:
p = -1
xp = -1
ans = 1
input()
for i in input().split(" "):
  i = int(i)
  if i == p: continue
  if p != -1:
    x = p < i
    if xp != -1:
      ans += x != xp
    if xp != -1 and x != xp:
      xp = -1
    else:
      xp = x
  p = i
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
ans = 1
p = m = 0
c = A[0]
for i in A[1:]:
    if i > c:
        p = 1
    elif i < c:
        m = 1
    if p == 1 and m == 1:
        p = m = 0
        ans += 1
    c = i
print(ans)
# maybe pills will work??

Submission status: Accepted

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

now=0
ans=1
for i in range(1,N):
    if i==1:
        if A[i]-A[i-1]==0:
            now=0
        else:
            now=(A[i]-A[i-1])//(abs(A[i]-A[i-1]))
        if now>=1:
            plus=True
        elif now==0:
            plus=-1
        else:
            plus=False
            
    else:
        if A[i]-A[i-1]==0:
            nex=0
        else:
            nex=(A[i]-A[i-1])//(abs(A[i]-A[i-1]))
        if plus==-1:
            if nex>=1:
                plus=True
            elif nex<0:
                plus=False
        elif plus:
            if nex<0:
                plus=-1
                ans+=1
        else:
            if nex>0:
                plus=-1
                ans+=1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
cnt,i = 0,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
    cnt+=1
    i+=1
print(cnt)

Submission status: Accepted

Solution code:
num = int(input())
l = list(map(int, input().split()))
count = 1
var = var2 = 0
c = l[0]
for i in l[1:]:
    if c < i:
        var = 1
    elif c > i:
        var2 = 1
    if var == 1 and var2 == 1:
        count += 1
        var = var2 = 0
    c = i
print(count)

Submission status: Accepted

Solution code:
num = int(input())
a = list(map(int, input().split()))
k = 1
x = y = 0
c = a[0]
for i in a[1:]:
    if c < i:
        x = 1
    elif c > i:
        y = 1
    if x == 1 and y == 1:
        k += 1
        x = y = 0
    c = i
print(k)

Submission status: Accepted

Solution code:
N = int(input())

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

i = 0
res = 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
    res += 1
    i += 1
print(res)

Submission status: Accepted

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

ans = 1
status = 0
prev = a[0]

for i in a:
    diff = i - prev

    if diff == 0:
        pass
    elif diff > 0:
        if status >= 0:
            status = diff
        else:
            ans += 1
            status = 0
    else:
        if status <= 0:
            status = diff
        else:
            ans += 1
            status = 0

    prev = i

print(ans)

Submission status: Accepted

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

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

for i in A:
    check = i - prev

    if check == 0:
        pass
    elif check > 0:
        if mode >= 0:
            mode = check
        else:
            ans += 1
            mode = 0
    else:
        # check < 0
        if mode <= 0:
            mode = check
        else:
            ans += 1
            mode = 0

    prev = i

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:
N = int(input())
A = list(map(int, input().split()))
k = 1
x = y = 0
c = A[0]
for i in A[1:]:
    if c < i:
        x = 1
    elif c > i:
        y = 1
    if x == 1 and y == 1:
        k += 1
        x = y = 0
    c = i
print(k)

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

ans = 1
up = dn = False
pre = A[0]
for a in A[1:]:
    if pre < a:
        up = True
    elif pre > a:
        dn = True
    if up and dn:
        ans += 1
        up = dn = False
    pre = a

print(ans)

Submission status: Accepted

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

i = 0
ans = 0

while i < n:
    i1 = i
    last = a[i]
    while i1 < n:
        if last <= a[i1]:
            last = a[i1]
            i1 += 1
        else: break
    
    i2 = i
    while i2 < n:
        if last >= a[i2]:
            last = a[i2]
            i2 += 1
        else: break
    
    i = max(i1, i2)
    ans += 1

print(ans)

Submission status: Accepted

Solution code:
import sys
N = int(input())
a = list(map(int, input().split()))
num, cut = 0, 0

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

for i in range(N-1):
    if a[i] == a[i+1]:
        continue
    if a[i] < a[i+1] and num == 0:
        num  = 1
        cut += 1
    elif a[i] > a[i+1] and num == 0:
        num = -1
        cut += 1
    elif num == 1 and a[i] > a[i+1]:
        if i == N-2:
            cut += 1
        num = 0
    elif num == -1 and a[i] < a[i+1]:
        if i == N-2:
            cut += 1
        num = 0
print(cut)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

# 減少中、増大中であるようなときの数列の個数のmin
dp = 1, 1
for i in range(1, N):
    if A[i] > A[i-1]:
        dp = min(dp)+1, min(dp[0]+1, dp[1])
    elif A[i] == A[i-1]:
        pass
    else:
        dp = min(dp[0], dp[1]+1), min(dp)+1

print(min(dp))

Submission status: Accepted

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

    idx = 0
    ans = 0
    for i in range(len(A)):
        if idx + 1 > len(A) - 1 :
            break

        m = ""
        if A[idx] < A[idx + 1]:
            m = "+"
        elif A[idx] > A[idx + 1]:
            m = "-"
        else:
            find = False
            for j in range(idx, len(A)-1):
                if A[j] < A[j + 1]:
                    m = "+"
                    find = True
                    idx = j
                    break
                elif A[j] > A[j + 1]:
                    m = "-"
                    find = True
                    idx = j
                    break
            if find == False:
                print(ans + 1)
                exit()

        for j in range(idx, len(A) - 1):
            if m == "+":
                if A[j] <= A[j + 1]:
                    idx = j + 1
                else:
                    idx = j + 1
                    ans += 1
                    break
            elif m == "-":
                if A[j] >= A[j + 1]:
                    idx = j + 1
                else:
                    idx = j + 1
                    ans += 1
                    break

    print(ans + 1)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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


flg=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:
from operator import lt, gt, le, ge
def get_monotonic_section(A, start=0, operator=lt):
    a0 = A[start]
    for i in range(start+1, len(A)):
        a = A[i]
        if not operator(a0, a):
            return i
        a0 = a
    return len(A)

n, *A = map(int, open(0).read().split())
c = t = 0
while t < n:
    t1 = get_monotonic_section(A, start=t, operator=le)
    t2 = get_monotonic_section(A, start=t, operator=ge)
    t = max(t1, t2)
    c += 1
print(c)

Submission status: Accepted

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

up = False
down = False
ans = 1

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

print(ans)

Submission status: Accepted

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

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
S = readInts()
i = 0
cnt = 0
while i < n:
    while i+1 < n:
        if S[i] == S[i+1]:
            i += 1
        else:
            break
    if i + 1 < n:
        if S[i] < S[i+1]:
            while i + 1 < n:
                if S[i] <= S[i+1]:
                    i += 1
                else:
                    break
        else:
            while i + 1 < n:
                if S[i] >= S[i+1]:
                    i += 1
                else:
                    break
    cnt += 1
    i += 1
print(cnt)

Submission status: Accepted

Solution code:
_, *A = map(int, open(0).read().split())
ans = 1
is_add = False
is_minus = False
tmp = A[0]
for a in A[1:]:
  if tmp > a:
    if is_add:
      ans += 1
      is_add = False
    else:
      is_minus = True
  elif tmp < a:
    if is_minus:
      ans += 1
      is_minus = False
    else:
      is_add = True
  tmp = a
print(ans)

Submission status: Accepted

Solution code:
#AGC013-A
n = int(input())
a = list(map(int,input().split()))
cnt = 0
first = True
up = False
down = False
for i in range(n):
    #print(i)
    if first:
        cnt += 1
        now = a[i]
        if i < n-1:
            #upのとき
            if a[i+1] > now:
                #print('up')
                up = True
                down = False
                first = False
            elif a[i+1] < now:
                #print('down')
                down = True
                up = False
                first = False
            else:
                #print("equal")
                up = False
                down = False
                first = True
                cnt -= 1

            
    else:
        now = a[i]
        if i < n-1:
            if up:
                if now > a[i+1]:
                    first = True

            elif down:
                if now < a[i+1]:
                    first = True

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
x = 0 #まだ未定なら0、単調増加なら1、単調減少なら-1にする
i = 0 #index
ans = 0

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

while True:
    if x == 0:
        if li[i] < li[i+1]:
            x = 1
        elif li[i] > li[i+1]:
            x = -1
        else:
            x = 0
    if x == 1:
        if li[i] > li[i+1]:
            ans += 1
            x = 0
    if x == -1:
        if li[i] < li[i+1]:
            ans += 1
            x = 0
    i += 1
    if i == n-1:
        break
print(ans+1)

Submission status: Accepted

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

if N == 1:
    print(1)
else:
    pre = A[0]
    flg = 0
    ans = 1
    for i in range(1, N):
        now = A[i]
        diff = now - pre
        if flg == 1:
            if diff >= 0:
                pass
            else:
                ans += 1
                flg = 0
        elif flg == -1:
            if diff <= 0:
                pass
            else:
                ans += 1
                flg = 0
        else:
            if diff > 0:
                flg = 1
            elif diff < 0:
                flg = -1
            else:
                pass
        pre = now
        # print("i: {}, A[i]: {}, flg: {}, ans:{}".format(i, A[i], flg, ans))
    print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2, log
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
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
from decimal import *

N = INT()
A = LIST()

ans = 1
tmp = 0
for i in range(1, N):
	if A[i] == A[i-1]:
		continue
	elif tmp == 0:
		tmp = (A[i]-A[i-1])//abs(A[i]-A[i-1])
	elif tmp != (A[i]-A[i-1])//abs(A[i]-A[i-1]):
		ans += 1
		tmp = 0

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
now = A[0]
kind = 0
ans = 0
for i in range(1,N):
    if kind == 0:
        if now < A[i]:
            kind = 1
        elif now > A[i]:
            kind = 2
    elif kind == 1: # 増加
        if now > A[i]:
            ans += 1
            kind = 0
    else: # 減少
        if now < A[i]:
            ans += 1
            kind = 0
    now = A[i]
print(ans+1)

Submission status: Accepted

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

cnt = 0
flag = 0

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')
AtoZ = "abcdefghijklmnopqrstuvwxyz"

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############
N = I()
A = IL()

if N < 3:
  print(1)
else:
  status = "Pending"
  prev = A[0]
  count = 1
  for a in A:
    if status == "Pending":
      if a > prev:
        status = "UP"
      if a < prev:
        status = "DOWN"
    elif status == "UP":
      if a < prev:
        status = "Pending"
        count += 1
    else:
      if a > prev:
        status = "Pending"
        count += 1
    prev = a
  print(count)

Submission status: Accepted

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

i = 0
ans = 0
for a in A:
  right, right2 = i, i
  while right+1 < N and A[right] <= A[right+1]: right += 1
  while right2+1 < N and A[right2] >= A[right2+1]: right2 += 1
  i = max(right, right2) + 1
  ans += 1
  if i >= N: break
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# AGC 013 A - Sorted Arrays

n = int(input())
a = list(map(int, input().split()))
up, down = 0, 0
ans = 0

for i in range(1, n):
    if a[i] > a[i-1]:
        up += 1
    if a[i-1] > a[i]:
        down += 1
    if up * down > 0:
        ans += 1
        up = 0
        down = 0
print(ans + 1)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
cnt = 0
f = 0
for i in range(1, n):
    if a[i-1] < a[i]:
        ff = 1
    elif a[i-1] > a[i]:
        ff = -1
    else:
        ff = 0
    if f and ff and f != ff:
        cnt += 1
        f = 0
    elif not f:
        f = ff

print(cnt + 1)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    end = len(A)
    judge = 0
    ans = 0
    i = 0

    if end == 1:
        return 1

    while True:
        if A[i] < A[i+1]:
            ans += 1
            while True:
                i += 1
                if i == end-1:
                    return ans
                elif A[i] <= A[i+1]:
                    continue
                else:
                    i += 1
                    if i == end-1:
                        ans += 1
                        return ans
                    else:
                        break

        elif A[i] > A[i+1]:
            ans += 1
            while True:
                i = i+1
                if i == end-1:
                    return ans
                elif A[i] >= A[i+1]:
                    continue
                else:
                    i += 1
                    if i == end-1:
                        ans += 1
                        return ans
                    else:
                        break

        elif A[i] == A[i+1]:
            i += 1
            if i == end-1:
                ans += 1
                return ans
            else:
                continue

print(main())

Submission status: Accepted

Solution code:
from collections import  deque
n=int(input())
a=list(map(int,input().split()))
cnt=0
judge=[]
for i in range(n-1):
    if a[i]<a[i+1]:
        flag=0
    elif a[i]>a[i+1]:
        flag=1
    elif a[i]==a[i+1]:
        continue
    if judge==[]:
        judge.append(flag)
    else:
        ff=judge.pop()
        if flag==ff:
            judge.append(flag)
        else:
            cnt+=1

print(cnt+1)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=1
if n==1:
    print(ans)
    exit()
c=0#1なら増加傾向、2なら減少傾向
for i in range(n):
    if i!=n-1:
        if c==1:
            if a[i+1]<a[i]:
                ans+=1
                c=0
                continue
        elif c==2:
            if a[i+1]>a[i]:
                ans+=1
                c=0
                continue
        
        if a[i+1]>a[i]:
            c=1
        elif a[i+1]<a[i]:
            c=2
print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


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

    status = "undefined"
    ans = 1
    for i in range(N - 1):
        diff = A[i + 1] - A[i]
        if status == "undefined":
            if diff == 0:
                pass
            elif diff > 0:
                status = "increase"
            else:
                status = "decrease"
        elif status == "increase":
            if diff >= 0:
                pass
            else:
                ans += 1
                status = "undefined"
        else:
            if diff <= 0:
                pass
            else:
                status = "undefined"
                ans += 1

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

ans=0
uf=False
lf=False
pre=A[0]
for i in range(1,N):
  if pre==A[i]:
    continue
  
  if A[i]>pre:
    uf=True
    if lf:
      ans+=1
      uf=False
      lf=False
  else:
    lf=True
    if uf:
      ans+=1
      uf=False
      lf=False
  pre=A[i]

print(ans+1)

Submission status: Accepted

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

    ans = 1
    is_add = False
    is_minus = False
    tmp = A[0]
    for a in A[1:]:
        if tmp > a:
            if is_add:
                ans += 1
                is_add = False
            else:
                is_minus = True
        elif tmp < a:
            if is_minus:
                ans += 1
                is_minus = False
            else:
                is_add = True
        tmp = a
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
*a, = map(int, input().split())
ans = 1
up = False
down = False
prev = a.pop(0)
for i in a:
    if (not up) and (not down):
        if prev < i:
            up = True
        elif prev > i:
            down = True
    elif up:
        if prev > i:
            ans += 1
            up = False
    elif down:
        if prev < i:
            ans += 1
            down = False
    prev = i
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a_li = list(map(int,input().split()))
if n < 3:
    print(1)
    exit()
ans = 1
flag = "flat"
for i in range(1,n):
    if a_li[i-1] > a_li[i]:
        if flag == "up":
            ans += 1
            flag = "flat"
            continue
        flag = "down"
    elif a_li[i-1] < a_li[i]:
        if flag == "down":
            ans += 1
            flag = "flat"
            continue
        flag = "up"
    else:
        if flag == "flat":
            flag = "flat"
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
a=list(map(int,input().split()))

#連続する重複を除外
arr=np.array(a)
diff=np.diff(np.hstack((0,arr)))
aa=arr[np.nonzero(diff)]

aaa=aa.tolist()
point=[]
cnt=1
if len(aaa)<=2:
    print(1)
else:
    for i in range(len(aaa)-2):
        if (aaa[i+1]-aaa[i])*(aaa[i+2]-aaa[i+1])<0:
            cnt+=1
            point.append(i)
    
    if len(point)>1:
        for i in range(1,len(point)):
            if point[i]-1==point[i-1]:
                point[i]-=1
                cnt-=1
    print(cnt)

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
a=list(map(int,input().split()))
b=a.copy()
b.insert(0,0)

#連続する重複を除外
arr=np.array(b)
diff=np.diff(arr)
arr2=np.array(a)
aa=arr2[np.nonzero(diff)]

aaa=aa.tolist()
point=[]
cnt=1
if len(aaa)<=2:
    print(1)
else:
    for i in range(len(aaa)-2):
        if (aaa[i+1]-aaa[i])*(aaa[i+2]-aaa[i+1])<0:
            cnt+=1
            point.append(i)
    
    if len(point)>1:
        for i in range(1,len(point)):
            if point[i]-1==point[i-1]:
                point[i]-=1
                cnt-=1
    print(cnt)