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:
#! /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
N = int(input())
A = list(map(int, input().split()))
count = 1
dec = False
inc = False
if N <= 2:
    print('1')
    exit()
for i in range(N - 1):
    if (A[i + 1] - A[i]) < 0:
        dec = True
    elif (A[i + 1] - A[i]) > 0:
        inc = True
    if dec and inc:
        count += 1
        dec = False
        inc = False
print(str(count))

Submission status: Accepted

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

count=0
zougen=""
for i in range(n-1):
    if zougen=="":
        if lista[i]<lista[i+1]:
            zougen="up"
        if lista[i]>lista[i+1]:
            zougen="down"
    elif  zougen=="up":
        if lista[i]>lista[i+1]:
            count+=1
            zougen=""
    elif zougen=="down":
        if lista[i]<lista[i+1]:
            count+=1
            zougen=""
print(count+1)

Submission status: Accepted

Solution code:
n = int(input())
num_list = [ int(v) for v in input().split() ]
cut_list = []
status = 0
for i in range(n-1):
    if status == 0:
        if num_list[i+1] > num_list[i]:
            status = 1
        elif num_list[i+1] < num_list[i]:
            status = -1
    elif status == 1:
        if num_list[i+1] < num_list[i]:
            cut_list.append(i)
            status = 0
    elif status == -1:
        if num_list[i+1] > num_list[i]:
            cut_list.append(i)
            status = 0

print(len(cut_list)+1)

Submission status: Accepted

Solution code:
from collections import deque
n=int(input())
D=deque(list(map(int,input().split())))
pop=D.popleft
A=pop()
memo=0
ans=0
for _ in range(n-1):
    a=pop()
    if memo==0:
        if A>a:
            memo=-1
        elif A<a:
            memo=1
        else:
            pass
    elif memo==1:
        if A>a:
            ans+=1
            memo=0
        else:
            pass
    else:
        if A<a:
            ans+=1
            memo=0
        else:
            pass
    A=a
print(ans+1)

Submission status: Accepted

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


flag = None
count = 0
for i in range(1, N):
    if A[i] - A[i - 1] == 0:#同じとき-->なかったとして考える
        pass
    if A[i] - A[i - 1] > 0: #増加しているとき
        if flag == True: #増加中のとき-->何もしない
            pass
        elif flag == False: #減少中のとき-->フラットに戻す
            flag = None
        elif flag == None: #フラットのとき-->カウントが増えて、上昇中になる
            # print (A[i])
            count += 1
            flag = True
    if A[i] - A[i - 1] < 0:
        if flag == False:
            pass
        elif flag == True:
            flag = None
        elif flag == None:
            # print (A[i])
            count += 1
            flag = False
    # print (A[i], flag)
if flag == None:
    count += 1
print (count)

Submission status: Accepted

Solution code:
n,*l=map(int,open(0).read().split())
a=b=c=0;f=1;l+=[0,1]
while f:
	while l[a]<=l[a+1]:a+=1
	while l[b]>=l[b+1]:b+=1
	a=b=max(a,b)+1
	c+=1;f=a<n
print(c)

Submission status: Accepted

Solution code:
n,*l=map(int,open(0).read().split())
a=b=0
c=0
l+=[0]
while True:
	while l[a]<=l[a+1] and a!=n-1:
		a+=1
	while l[b]>=l[b+1] and b!=n-1:
		b+=1
	a=b=max(a,b)+1
	c+=1
	if a==n-1:
		c+=1
		break
	elif a==n:
		break
print(c)

Submission status: Accepted

Solution code:




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

cnt = 0
reset = True
flag = True
tmp = A[0]
for i in range(1,N):
    if reset:
        if A[i] > tmp:
            flag = True
            reset = False
        elif A[i] < tmp:
            flag = False
            reset = False
    else:
        if flag and A[i] < tmp:
            cnt += 1
            reset = True
        elif not(flag) and A[i] > tmp:
            cnt += 1
            reset = True
    tmp = A[i]
        
    
    
  
print(cnt+1)

Submission status: Accepted

Solution code:
N = int(input())
if N == 1:
  print(1)
  exit()
A = [int(i) for i in input().split()]
ans = 1
if A[1] - A[0] > 0:
  pre = 1
elif A[1] - A[0] == 0:
  pre = 0
else:
  pre = -1

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

Submission status: Accepted

Solution code:
a = int(input())
l = list(map(int,input().split()))
s = 1
t = 0
for i,j in zip(l,l[1:]):
    if i < j and t == -1:
        t=0
        s+=1
    elif i > j and t == 1:
        t = 0
        s+=1
    elif t ==0:
        if i < j:
            t = 1
        elif i > j:
            t = -1
        

    # print(i,j,s,t)
print(s)

Submission status: Accepted

Solution code:
N = int(input())
aLst = list(map(int, input().split()))
zougen = ''

cnt = 0
for i in range(N - 1):
    if zougen == 'U':
        if aLst[i] > aLst[i + 1]:
            cnt += 1
            zougen = ''
    elif zougen == 'D':
        if aLst[i] < aLst[i + 1]:
            cnt += 1
            zougen = ''
    else:
        if aLst[i] > aLst[i + 1]:
            zougen= 'D'
        elif aLst[i] < aLst[i + 1]:
            zougen = 'U'
print(cnt + 1)

Submission status: Accepted

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

ans = 1
cnt = 0

for i in range(1, N):
    if A[i-1] < A[i]:
        if cnt == 0:
            f = True
            cnt = 1
        if f == False:
            f = True
            cnt = 0
            ans += 1
    elif A[i-1] > A[i]:
        if cnt == 0:
            f = False
            cnt = 1
        if f == True:
            f = False
            cnt = 0
            ans += 1

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 0
check = 0
l = []

for i in range(N):
    if i == 0:
        ans += 1
        check = 0
        l.append(A[i])
    else:
        if len(l) == 0:
            ans += 1
            check = 0
            l.append(A[i])
        elif len(l) == 1:
            l.append(A[i])
            if A[i] - A[i-1] > 0:
                check = 1
            elif A[i] - A[i-1] < 0:
                check = -1
            else :
                check = 0
        else:
            if A[i] - A[i-1] == 0:
                l.append(A[i])
                pass
            elif A[i] - A[i-1] > 0 and check >= 0:
                check = 1
                l.append(A[i])
            elif A[i] - A[i-1] > 0 and check < 0:
                check = 1
                l = [A[i]]
                ans += 1
            elif A[i] - A[i-1] < 0 and check <= 0:
                check = -1
                l.append(A[i])
            elif A[i] - A[i-1] < 0 and check > 0:
                check = -1
                l = [A[i]]
                ans += 1
#    print(i)
#    print(l)
#    print(check)
#    print(ans)
#    print('--------------')

print(ans)

Submission status: Accepted

Solution code:
import sys, math, itertools, bisect, copy, re, heapq
from collections import Counter, deque, defaultdict
from itertools import accumulate, permutations, combinations, takewhile, compress, cycle

# from functools import reduce
# from math import ceil, floor, log10, log2, factorial
# from pprint import pprint


INF = float('inf')
MOD = 10 ** 9 + 7
EPS = 10 ** -7
sys.setrecursionlimit(1000000)

# N = int(input())
# N,M = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]
# A = [int(input()) for _ in range(N)]
# DP = [[0] * 100 for _ in range(100)]
# DP = defaultdict(lambda: float('inf'))

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

# -1:減少、0:維持、+1：増加
mode = 0
cnt = 0

last = A[0]
for a in A:
    # print("{} -> {} : mode:{}".format(last, a, mode))
    if mode == 0:
        if last < a:
            mode = +1
        elif last > a:
            mode = -1
        else:
            pass
    elif mode == +1:
        if last <= a:
            pass
        else:
            cnt += 1
            mode = 0
    else:
        if last >= a:
            pass
        else:
            cnt += 1
            mode = 0
    last = a

print(cnt+1)

Submission status: Accepted

Solution code:
n = int(input())
A = tuple(map(int, input().split()))
direction = 0  # 0=slide, 1=up, 2=down
temp = A[0]
count = 1

for i in A:
    if direction == 0:
        if i > temp:
            temp = i
            direction = 1
        elif i < temp:
            temp = i
            direction = 2
        else:
            temp = i

    elif direction == 1:
        if i >= temp:
            temp = i
        else:
            temp = i
            count += 1
            direction = 0

    elif direction == 2:
        if i <= temp:
            temp = i
        else:
            temp = i
            count += 1
            direction = 0

print(count)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
sums = 0
b = 0
count = 1
for i in range(len(a)-1):
  sums += a[i+1] - a[i]
  #print(i,sums,b)
  if sums >= 0 and sums < b or b > 0 and sums < 0:
    count += 1
    sums = 0
  elif sums <= 0 and sums > b or b < 0 and sums > 0:
    count += 1
    sums = 0
  b = sums
  
print(count)

Submission status: Accepted

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

ap = a[0]
count = 0
l = 0
for i in range(n):
    l += 1
    if mt + lt == 0:
        if a[i]-ap > 0:
            mt = True
        elif a[i]-ap < 0:
            lt = True
    if mt:
        if a[i]-ap <0:
            lt = True
            mt = False
            if l > 1:
                count += 1
                l = 0
                lt = False
            else:
                l = 1
    if lt:
        if a[i]-ap > 0:
            mt = True
            lt = False
            if l > 1:
                mt = False
                count += 1
                l = 0
            else:
                l = 1
    ap = a[i]
print(count+1)

Submission status: Accepted

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

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

last = A[0]
direction = 0

answer = 1
for a in A[1:]:
    if direction == 0:
        if last < a:
            direction = 1
        elif last > a:
            direction = -1
        last = a
        continue
    if direction == 1:
        if last <= a:
            last = a
            continue
        else:
            last = a
            direction = 0
            answer += 1
            continue
    if direction == -1:
        if last >= a:
            last = a
            continue
        else:
            last = a
            direction = 0
            answer += 1
            continue

print(answer)

Submission status: Accepted

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

if n == 1:
    print("1")
else:
    if a[0] > a[1]:
        now = -1
    elif a[0] < a[1]:
        now = 1
    else:
        now = 0
        
    for i in range(1,n-1):
        if now == -1:
            if a[i] < a[i+1]:
                counter += 1
                now = 0
        elif now == 1:
            if a[i] > a[i+1]:
                counter += 1
                now = 0
        else:
            if a[i] > a[i+1]:
                now = -1
            elif a[i] < a[i+1]:
                now = 1
    print(counter)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#AGC13 A

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7


n = int(sys.stdin.readline())
a = list(map(int,sys.stdin.readline().split()))
ans = 1
x = a[0]
flg = 0
for i in range(1,n):
    if flg == 0:
        if x < a[i]:
            flg = 1
        elif x > a[i]:
            flg = -1
    elif flg == 1:
        if x > a[i]:
            flg = 0
            ans += 1
    elif flg == -1:
        if x < a[i]:
            flg = 0
            ans += 1
    x = a[i]
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#AGC13 A

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7


n = int(sys.stdin.readline())
a = list(map(int,sys.stdin.readline().split()))
ans = 1
x = a[0]
flg = 0
for i in range(1,n):
    if flg == 0:
        if x < a[i]:
            flg = 1
        elif x > a[i]:
            flg = -1
    if flg == 1:
        if x > a[i]:
            flg = 0
            ans += 1
    elif flg == -1:
        if x < a[i]:
            flg = 0
            ans += 1
    x = a[i]
print(ans)

Submission status: Accepted

Solution code:
def array_split(array):
    array_len=len(array)
    res=[];ans=[]
    s=0 # 0:傾き不明 1:単調非減少 2:単調非増加
    roop=0
    while roop!=array_len:
        if ans==[]:
            ans.append(array[roop])
            roop+=1
        elif s==0 and ans[-1]>array[roop]:
            ans.append(array[roop])
            s=2
            roop+=1
        elif s==0 and ans[-1]<array[roop]:
            ans.append(array[roop])
            s=1
            roop+=1
        elif s==0 and ans[-1]==array[roop]:
            ans.append(array[roop])
            roop+=1
        elif s==1:
            if ans[-1]<=array[roop]:
                ans.append(array[roop])
                roop+=1
            else:
                res.append(ans)
                s=0
                ans=[]
        elif s==2:
            if ans[-1]>=array[roop]:
                ans.append(array[roop])
                roop+=1
            else:
                res.append(ans)
                s=0
                ans=[]
    res.append(ans)
    return res
n=int(input())
a=list(map(int,input().split()))
print(len(array_split(a)))

Submission status: Accepted

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

if N ==1 or N ==2:
    print(1)
    exit()
t = 0
c =1
for i in range(1,N):

    if A[i]-A[i-1]>0:
        if t == -1:
            c +=1
            t = 0
        else:
            t = 1
    elif A[i]-A[i-1]<0:
        if t == 1:
            c +=1
            t = 0
        else:
            t = -1    
print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    ls = [int(x) for x in input().split()]
    if n==1:
        print(1)
    else:
        right,left = 0,1
        cnt = 0
        flg = 0
        while 1:
            now = ls[left]
            nex = ls[right]   
            if now<nex:
                if flg ==-1:
                    cnt +=1
                    flg = 0
                else:
                    flg = 1
                left +=1
                right +=1
            elif nex==now:
                left +=1
                right +=1         

            else:
                if flg ==1:
                    cnt += 1
                    flg = 0
                else:
                    flg = -1
                left +=1
                right +=1

            if right==n-1:
                cnt +=1
                break
        print(cnt)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#参考(傾きの捉え方)
N = int(input())
A = list(map(int, input().split()))
f = 0
c = 1
i = 0
while i < N-1:
    if f == 0:
        if A[i] < A[i+1]:
            f = 1
        elif A[i] > A[i+1]:
            f = -1
    elif f == 1:
        if A[i] > A[i+1]:
            c += 1
            f = 0
    elif f == -1:
        if A[i] < A[i+1]:
            c += 1
            f = 0
    i += 1
print(c)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=[]
c=[]
if n==1:
        print(1)

else:
    for i in range(n-1):
            if a[i]!=a[i+1]:
                    b.append(a[i])

    b.append(a[n-1])
    ans=1

    for i in range(len(b)-1):
            c.append(b[i+1]-b[i])

    d=c[0]
    for i in range(1,len(b)-1):
            if d*c[i]>=0:
                    d=c[i]
            else:
                    ans+=1
                    d=0
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
in_cnt = 0
de_cnt = 0
now = -1
for ind,a in enumerate(A[:-1]):
    if(A[ind+1]>a):
        if(now==-1):
            in_cnt+=1
            now = 1
        elif(now==0):
            now = -1
    elif(A[ind+1]<a):
        if(now==-1):
            de_cnt+=1
            now = 0
        elif(now==1):
            now = -1
print(in_cnt+de_cnt+(1 if now==-1 else 0))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# AGC013A - Sorted Arrays
def main():
    n = int(input())
    A = tuple(map(int, input().split()))
    ans, cur = 0, A[0]
    flg = 0  # unknown (0), increasing (1), decreasing (2)
    for i in A:
        if (flg == 1 and i < cur) or (flg == 2 and i > cur):  # end of trend
            ans += 1
            flg = 0
        elif not flg and cur < i:  # new increasing trend
            flg = 1
        elif not flg and cur > i:  # new decreasing trend
            flg = 2
        cur = i
    print(ans + 1)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# AGC013A - Sorted Arrays
def main():
    n = int(input())
    A = tuple(map(int, input().rstrip().split()))

    # compress continuous parts of same numbers (e.g. 111 -> 1)
    B, cur = [], 0
    for i in A:
        if i != cur:
            B += [i]
            cur = i
    B += [0]

    # check the modified sequence
    ans, cur = 0, B[0]
    flg = B[0] < B[1]  # increasing (1) / decreasing (0)
    for i, j in enumerate(B[1:-1], start=1):
        if (flg and j < cur) or (not flg and j > cur):
            ans += 1
            flg = B[i] < B[i + 1]
        cur = j
    print(ans + 1)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# AGC013A - Sorted Arrays
def main():
    n = int(input())
    a = tuple(map(int, input().rstrip().split()))
    
    A, x = [a[0]], a[0]
    A_app = A.append
    for i in a[1:]:
        if i != x:
            A_app(i)
            x = i
    A_app(0)
    
    ans, cur = 0, A[0]
    flg = A[0] <= A[1]  # flg: increasing (1)
    for i, j in enumerate(A[1:-1], start=1):
        if flg:  # increasing
            if j < cur:
                ans += 1
                flg = A[i] <= A[i + 1]
        else:  # decreasing
            if j > cur:
                ans += 1
                flg = A[i] <= A[i + 1]
        cur = j
    print(ans + 1)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

dif = [0] * (N-1)
for i in range(1,N): dif[i-1] = A[i] - A[i-1]

ans = 1
t = 0
i = 0
for d in dif:
    if t == 0:
        t = d
    elif d*t < 0:
        ans += 1
        t = 0
print(ans)

Submission status: Accepted

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

ans = 1
tmp = aa[0]
flg_asc = flg_desc = True

for a in aa:
  if tmp > a:
    flg_asc = False
  if tmp < a:
    flg_desc = False
  
  if not flg_asc and not flg_desc:
    ans += 1
    flg_asc = flg_desc = True
    
  tmp = a
  
print(ans)

Submission status: Accepted

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

flg_inc = False
flg_dec = False

count = 1
for i in range(1, len(a)):
    if a[i] < a[i - 1]:
        flg_dec = True
        if flg_inc:
            count += 1
            flg_inc = False
            flg_dec = False
    elif a[i] > a[i - 1]:
        flg_inc = True
        if flg_dec:
            count += 1
            flg_inc = False
            flg_dec = False

print(count)

Submission status: Accepted

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

    
print(ans)

Submission status: Accepted

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

c = 0
up = '?'
pre = A[0]
for a in A:
    if a > pre:
        if up is False:
            c += 1
            up = '?'
        else:
            up = True
    elif a < pre:
        if up is True:
            c += 1
            up = '?'
        else:
            up = False
    pre = a

c += 1

print(c)

Submission status: Accepted

Solution code:
from subprocess import*
with open('Main.java','w')as f:f.write(r"""
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.next());
		int ans = 0;
		int max = Integer.parseInt(sc.next());
		int min = max;
		boolean up = true;
		boolean down = true;
		for (int i = 1; i < n; i++) {
			int a = Integer.parseInt(sc.next());
			if (up) {
				if (a >= max)
					max = a;
				else
					up = false;
			}
			if (down) {
				if (a <= min)
					min = a;
				else
					down = false;
			}
			if (!up && !down) {
				ans++;
				max = a;
				min = a;
				up = true;
				down = true;
			}
		}
		ans++;
		System.out.println(ans);
	}
}
""")
call(('javac','Main.java'))
call(('java','Main'))

Submission status: Accepted

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

B = []
tmp = -1
for a in A:
    if a != tmp:
        B.append(a)
        tmp = a

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

def f(A):
    ans = 1
    n = len(A)
    if A[0] < A[1]:
        ff = 1
    else:
        ff = -1
    for i in range(1, n - 1):
        if ff == 0:
            if A[i] < A[i + 1]:
                ff = 1
            else:
                ff = -1
        elif ff == 1:
            if A[i] > A[i + 1]:
                ans += 1
                ff = 0
        else:
            if A[i] < A[i + 1]:
                ans += 1
                ff = 0
    return ans

print(f(B))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 1
i = 0
flg = 3
while i < n - 1:
    if (flg >> 1) & 1 and a[i] > a[i + 1]:
        flg -= 2
    if flg & 1 and a[i] < a[i + 1]:
        flg -= 1
    if flg == 0:
        ans += 1
        flg = 3
    i += 1
print(ans)

Submission status: Accepted

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

ans=0
flg ='0'
for i in range(1,N):

    if flg=='0' and A[i-1]<A[i]:
        flg='+'
    elif flg=='0' and A[i-1]>A[i]:
        flg='-'

    if flg=='+' and A[i-1]>A[i]:
        ans+=1
        flg='0'
    elif flg=='-' and A[i-1]<A[i]:
        ans+=1
        flg='0'
    
    # print(flg)

print(ans+1)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.append(0)
b=[]
for i in range(n):
  if a[i]!=a[i+1]:
    b.append(a[i])
n=len(b)
s=n
for i in range(n-1):
  if b[i]!=b[i+1]:
    s=i
    if b[s]<b[s+1]:
      f=0
    else:
      f=1
    break
p=1
for i in range(s+1,n-1):
  if f==0:
    if b[i]>b[i+1]:
      p+=1
      if i==n-2 or b[i+1]>b[i+2]:
        f=1
      else:
        f=0
  else:
    if b[i]<b[i+1]:
      p+=1
      if i==n-2 or b[i+1]>b[i+2]:
        f=1
      else:
        f=0
print(p)

Submission status: Accepted

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
data = [int(x) for x in stdin.readline().rstrip().split()]

flag_increase = 0
flag_decrease = 0
ans = 0
tmp = data[0]

for i in data:
    if tmp > i:
        flag_decrease = 1
        if flag_increase == 1:
            ans += 1
            flag_increase = 0
            flag_decrease = 0
        tmp = i
        
    if tmp < i:
        flag_increase = 1
        if flag_decrease == 1:
            ans += 1
            flag_decrease = 0
            flag_increase = 0
        tmp = i
        
    #print(i,ans,flag_increase,flag_decrease)
print(ans+1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys

sys.setrecursionlimit(10**7)


def read_h(typ=int):
    return list(map(typ, input().split()))


def read_v(n, m=1, typ=int):
    return [read_h() if m > 1 else typ(input()) for _ in range(n)]


def main():
    N, = read_h()
    A = read_h()

    if len(A) == 1:
        print(1)
        return

    cnt = 1
    inc = None

    for i in range(len(A) - 1):
        #  print(A[i:i + 2])

        if inc is None and A[i] != A[i + 1]:
            inc = A[i] < A[i + 1]
            continue

        if A[i] == A[i + 1] or (inc and A[i] < A[i + 1]) or (not inc and A[i] > A[i + 1]):
            continue

        #  print('change')
        cnt += 1
        inc = None

    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Accepted

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int, input().split()))
cnt=1
condition=''
for i in range(N-1):
  if A[i]>A[i+1] and condition=='plus':
    cnt+=1
    condition=''
  elif A[i]<A[i+1] and condition=='minus':
    cnt+=1
    condition=''
  elif A[i]>A[i+1]:
    condition='minus'
  elif A[i]<A[i+1]:
    condition='plus'

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

count, state = 0, 0
for n in range(1, N):
	d = A[n] - A[n - 1]
	#print(d, state)
	if d == 0:
		continue
	if d < 0 and state <= 0:
		state = -1
		continue
	if 0 < d and 0 <= state:
		state = 1
		continue
	count += 1
	state = 0
print(count + 1)

Submission status: Accepted

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

ans = 0

flg = 0
t = A[0]

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

print(ans+1)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
if n <= 2:
    print(1)
    quit()
h = 0
ans = 1
for i in range(n - 1):
    if (l[i + 1] - l[i]) * h < 0:
        ans += 1
        h = 0
        continue
    if l[i + 1] - l[i] < 0:
        h = -1
    elif l[i + 1] - l[i] > 0:
        h = 1
print(ans)