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: Wrong Answer

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


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


def INT(): return int(input())


def MAP(): return map(int, input().split())


def LIST(): return list(map(int, input().split()))


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


def main():
	n = INT()
	a = LIST()
	p = 0
	q = 0
	l = []
	temp = 0

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

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

	for i in range(n-1):
		if a[i] < a[i+1] and sign == -1:
			p += 1
			sign = 1
			# print("a")
			if temp == 0:
				q += 1
				temp = 1

		elif a[i] > a[i+1] and sign == 1:
			q += 1
			sign = -1
			# print("b")
			if temp == 0:
				p += 1
				temp = 1


	# print(p, q)
	print(max(p, q) + 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


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


def INT(): return int(input())


def MAP(): return map(int, input().split())


def LIST(): return list(map(int, input().split()))


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


def main():
	n = INT()
	a = LIST()
	ans = 1
	l=[]
	
	for i in range(n-1):
		if a[i] < a[i+1]:
			l.append(1)
		elif a[i] == a[i+1]:
			l.append(0)
		else:
			l.append(-1)

	# print(l)

	for i in range(n-1):
		if l[i] == 0:
			if i == 0:
				l[i] = l[i+1]
			else:
				l[i] = l[i-1]

	# print(l)

	for i in range(n-2):
		if l[i] != l[i+1]:
			ans += 1

	print(ans)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

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

print(ans+1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
new_mode, mode = '', ''
cnt = 0
for i in range(1, n):
    tmp = a[i-1]-a[i]
    if tmp==0: continue
    elif tmp==1: mode='-'
    elif tmp==-1: mode='+'
    else: mode='?'
    if new_mode=='' and mode!='?':  new_mode=mode
    elif new_mode!=mode or mode=='?':
        cnt+=1
        new_mode=''
print(cnt if cnt==0 else cnt+1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
new_mode, mode = '', ''
cnt = 0
for i in range(1, n):
    tmp = a[i-1]-a[i]
    if tmp==0: continue
    elif tmp==1: mode='-'
    elif tmp==-1: mode='+'
    else: mode='?'
    if new_mode=='':  new_mode=mode
    elif new_mode!=mode:
        cnt+=1
        new_mode=''
print(cnt if cnt==0 else cnt+1)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
s=sum(a)
if s%n!=0:
  print(s//n+1)
else:
  print((s//n) )

Submission status: Wrong Answer

Solution code:

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

flg=0 #0:start 1:high 2:low
cnt=0
for i in range(N-1):
    if A[i] >= A[i+1]:
        if flg==1:
            continue
        elif flg==2:
            cnt+=1
            flg=0
        else:
            flg=1
    elif A[i] <= A[i+1]:
        if flg==2:
            continue
        elif flg==1:
            cnt+=1
            flg=0
        else:
            flg=2
print(cnt+1)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())

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

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

Submission status: Wrong Answer

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

res = 0
for i in range(n-1):
	if l[i+1] < l[i]:
		res += 1

print(res+1)

Submission status: Wrong Answer

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

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


print(ans)

Submission status: Wrong Answer

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

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


print(ans)

Submission status: Wrong Answer

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

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

            
    if A[i-1] == A[i]:
        A[i-1] = A[i-2] 
            


print(ans)

Submission status: Wrong Answer

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

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


print(ans)

Submission status: Wrong Answer

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

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


print(ans)

Submission status: Wrong Answer

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

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


print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
arr = list(map(int, input().split()))
count = 0
ans = 1
while True:
    tmp = count
    for i in range(tmp,N-1):
        if arr[i] > arr[i+1]:
            count = i + 1
            break
    if count == tmp:
        break
    for i in range(tmp,N-1):
        if arr[i] < arr[i+1]:
            count = max(count, i + 1)
            break
    if count == tmp:
        break
    ans += 1
print(ans)

Submission status: Wrong Answer

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

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


N = iin()
A = iln()

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

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

print(cnt)

Submission status: Wrong Answer

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

ind = 0
prev = 0
status = 0 #-1 減少　1増加
cnt = 1

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

print(cnt)

Submission status: Wrong Answer

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

for i in range(N-1):
  if A[i]<=A[i+1]:
    flag.append(1)
  elif i>0 and A[i]==A[i-1]:
    flag.append(flag[i-1])
  else:
    flag.append(-1)

ans = 1
c = 1
for i in range(N-2):
  if flag[i]!=flag[i+1] and c!=0:
    ans+=1
    c=0
  else:
    c+=1

print(ans)

Submission status: Wrong Answer

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

for i in range(N-1):
  if A[i]<=A[i+1]:
    flag.append(1)
  elif i>0 and A[i]==A[i+1]:
    flag.append(flag[i-1])
  else:
    flag.append(-1)

ans = 1
c = 1
for i in range(N-2):
  if flag[i]!=flag[i+1] and c!=0:
    ans+=1
    c=0
  else:
    c+=1

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
As = list(map(int, input().split()))
As += [As[-1]]

ind = 0
prev = 0
flg = 0
cnt = 1

while ind < N:
    a = As[ind]
    if flg == 0: #"減少しない"
        if prev > a:
            cnt += 1
            if As[ind] < As[ind+1]:flg = 0
            else:flg = 1
            ind += 1
    elif flg == 1:
        if prev < a:
            cnt += 1
            if As[ind] < As[ind+1]:flg = 0
            else:flg =1
            ind += 1
    prev = As[ind]
    ind += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=input()
flag = []

for i in range(N-1):
  if A[i]<=A[i+1]:
    flag.append(1)
  elif i>0 and A[i]==A[i+1]:
    flag.append(flag[i-1])
  else:
    flag.append(-1)

ans = 1
c = 1
for i in range(N-2):
  if flag[i]!=flag[i+1] and c!=0:
    ans+=1
    c=0
  else:
    c+=1

print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=input()
flag = []

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

Submission status: Wrong Answer

Solution code:
from math import ceil


def main():
    length = int(input())
    sequence = list(map(int, input().split()))
    count = 1
    before_val = sequence[0]
    if length > 2:
        delta = 1  # increase: 1, decrease: 0
        if sequence[1] < before_val:
            delta = 0
        before_val = sequence[1]
        for i in range(2, length):
            if before_val < sequence[i] and delta == 0:
                delta = 1
                count += 1
            elif sequence[i] < before_val and delta == 1:
                delta = 0
                count += 1
            before_val = sequence[i]
    print(ceil(count / 2) + 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


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

ans = 1
is_increase = A[2] < A[3]
reset = 0
for a, b in zip(A[1:-1], A[2:]):
    if reset:
        if a == b:
            continue
        elif a < b:
            is_increase = 1
        else:
            is_increase = 0
        reset = 0

    if is_increase:
        if a < b:
            pass
        else:
            ans += 1
            is_increase = 0
            reset = 1
    else:
        if a < b:
            ans += 1
            is_increase = 0
            reset = 1
        else:
            pass
print(ans)

Submission status: Wrong Answer

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


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

ans = 1
is_increase = A[0] < A[1]
reset = 0
for a, b in zip(A[:-1], A[1:]):
    if reset:
        if a == b:
            continue
        elif a < b:
            is_increase = 1
        else:
            is_increase = 0
        reset = 0

    if is_increase:
        if a < b:
            pass
        else:
            ans += 1
            is_increase = 0
            reset = 1
    else:
        if a < b:
            ans += 1
            is_increase = 0
            reset = 1
        else:
            pass
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))
ans = 1
prev = -1
is_increase = 1
tmp = -1
for a in A:
    if tmp != -1:
        if tmp == a:
            continue
        if tmp < a:
            is_increase = 1
        else:
            is_increase = 0
        tmp = -1
    if is_increase:
        if prev < a:
            pass
        else:
            ans += 1
            tmp = a
            # is_increase = 0
    else:
        if prev < a:
            ans += 1
            tmp = a
            # is_increase = 1
    prev = a
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))
ans = 1
prev = -1
is_increase = 1
tmp = -1
for a in A:
    if tmp != -1:
        if tmp < a:
            is_increase = 1
        else:
            is_increase = 0
        tmp = -1
    if is_increase:
        if prev < a:
            pass
        else:
            ans += 1
            tmp = a
    else:
        if prev < a:
            ans += 1
            tmp = a
    prev = a
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    ans = 0
    i = 0
    while i < N:
        pos1, pos2 = i, i
        while pos1+1 < N and A[pos1] <= A[pos1+1]:
            pos1 += 1
        while pos2+1 < N and A[pos2] > A[pos2+1]:
            pos2 += 1
        ans += 1
        i = max(pos1, pos2) + 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    ans = 0
    i = 0
    while i < N:
        while i+1 < N and A[i] == A[i+1]:
            i += 1
        pos1, pos2 = i, i
        while pos1+1 < N and A[pos1] < A[pos1+1]:
            pos1 += 1
        while pos2+1 < N and A[pos2] > A[pos2+1]:
            pos2 += 1
        ans += 1
        i = max(pos1, pos2) + 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    ans = 0
    i = 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
        ans += 1
        i += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    ans = 1
    if N <= 2:
        return print(ans)
    flag = True  # 単調非減少
    if A[0] > A[1]:
        flag = False
    for i in range(N-1):
        if flag:
            if A[i] > A[i+1]:
                # 単調非減少でなければ分ける
                ans += 1
                if i <= N - 3:
                    if A[i+1] < A[i+2]:
                        flag = True
                    else:
                        flag = False
        else:
            if A[i] < A[i+1]:
                # 単調非増加でなければ分ける
                ans += 1
                if i <= N - 3:
                    if A[i+1] < A[i+2]:
                        flag = True
                    else:
                        flag = False

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    ans = [1, 1, 1, 1]
    for i in range(N-1):
        if A[i] > A[i+1]:
            # 単調非減少でなければ分ける
            ans[0] += 1
        if A[i] < A[i+1]:
            # 単調非増加でなければ分ける
            ans[1] += 1
    flag = True
    for i in range(N-1):
        if flag:
            if A[i] > A[i+1]:
                # 単調非減少でなければ分ける
                ans[2] += 1
                flag = False
        else:
            if A[i] < A[i+1]:
                # 単調非増加でなければ分ける
                ans[2] += 1
                flag = True
    flag = False
    for i in range(N-1):
        if flag:
            if A[i] > A[i+1]:
                # 単調非減少でなければ分ける
                ans[3] += 1
                flag = False
        else:
            if A[i] < A[i+1]:
                # 単調非増加でなければ分ける
                ans[3] += 1
                flag = True

    print(min(ans))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

count=1
flag=True
for i in range(n-1):
    if i==0:
        ans=a[i+1]-a[i]
    if not flag:
        ans=a[i+1]-a[i]
        flag=True
        continue

    if (ans<=0 and a[i+1]-a[i]<=0) or (ans>=0 and a[i+1]-a[i]>=0):
        ans=a[i+1]-a[i]
        continue

    else:
        count+=1
        flag=False

print(count)

Submission status: Wrong Answer

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

count=1
flag=True
for i in range(n-1):
    if i==0:
        ans=a[i+1]-a[i]
    if not flag:
        ans=a[i+1]-a[i]
        flag=True

    if (ans<=0 and a[i+1]-a[i]<=0) or (ans>=0 and a[i+1]-a[i]>=0):
        ans=a[i+1]-a[i]
        continue

    else:
        count+=1
        flag=False

print(count)

Submission status: Wrong Answer

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

count=1
flag=True
for i in range(n-1):
    if i==0:
        ans=a[i+1]-a[i]
    if not flag:
        ans=a[i+1]-a[i]
        flag=True

    if (ans<=0 and a[i+1]-a[i]<=0) or (ans>=0 and a[i+1]-a[i]>=0):
        continue

    else:
        count+=1
        flag=False

print(count)

Submission status: Wrong Answer

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

count=0
flag=True
for i in range(n-1):
    if i==0:
        ans=a[i+1]-a[i]
    if not flag:
        ans=a[i+1]-a[i]
        flag=True

    if ans<=0 and a[i+1]-a[i]<=0 or ans>=0 and a[i+1]-a[i]>=0:
        continue

    else:
        if count==0:
            count+=1
        count+=1
        flag=False

print(count)

Submission status: Wrong Answer

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

count=0
flag=True
for i in range(n-1):
    if i==0:
        count+=1
        ans=a[i+1]-a[i]
    if not flag:
        ans=a[i+1]-a[i]
        flag=True

    if ans<=0 and a[i+1]-a[i]<=0 or ans>=0 and a[i+1]-a[i]>=0:
        continue

    else:
        count+=1
        flag=False

print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split(' ')))
count = 0
tmp_arr = []
inc = -1
for i, a in enumerate(A):
    if len(tmp_arr) == 0:
        tmp_arr.append(a)
        continue
    if N-1 == i:
        count += 1
    if inc == 0 and a > tmp_arr[-1]:
        tmp_arr = []
        inc = -1
        count += 1
    elif inc == 1 and a < tmp_arr[-1]:
        tmp_arr = []
        inc = -1
        count += 1
    else:
        if a > tmp_arr[-1]:
            inc = 1
        elif a < tmp_arr[-1]:
            inc = 0
    tmp_arr.append(a)
print(count)

Submission status: Wrong Answer

Solution code:
#n = int(input())
#n,k = map(int,input().split())
#x = list(map(int,input().split()))

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

ans = 2
mm = 0
i = 0
while(i<N-2):
    if (A[i]-A[i+1])*(A[i+1]-A[i+2])<0:
        ans += 1
        i+=1
    i+=1
    
print(ans)

Submission status: Wrong Answer

Solution code:
#n = int(input())
#n,k = map(int,input().split())
#x = list(map(int,input().split()))

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

ans = 0
mm = 0
i = 0
while(i<N-2):
    if (A[i]-A[i+1])*(A[i+1]-A[i+2])<0:
        ans += 1
        i+=1
    i+=1
    
print(ans)

Submission status: Wrong Answer

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

ans = 1
mm = 0
i = 0
while(i<N-2):
    if (A[i]-A[i+1])*(A[i+1]-A[i+2])<0:
        ans += 1
        i+=1
    i+=1
    
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(e) for e in input().split()]
i = 0
count = 0
while n > i+2:
    if a[i] == a[i+1] and n > i+2:
        i+=1
        continue
    elif a[i] < a[i+1]:
        while a[i] <= a[i+1] and n > i+2:
            i += 1
    elif a[i] > a[i+1]:
        while a[i] >= a[i+1] and n > i+2:
            i += 1
    count += 1
    i+=1
print(count + 1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = 0
flagu = 0
flagd = 0

for i in range(n):
	if i == 0:
		continue
	elif ls[i - 1] < ls[i]:
		if flagd == 1:
			ans += 1
			flagu = 0
			flagd = 0
		elif flagu == 0:
			flagu = 1
	elif ls[i - 1] > ls[i]:
		if flagu == 1:
			ans += 1
			flagu = 0
			flagd = 0
		elif flagd == 0:
			flagd = 1
	
	if i == n - 1:
		ans += 1
	
print(ans)

Submission status: Wrong Answer

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

ans = 0
flagu = 0
flagd = 0

for i in range(n):
	if i == 0:
		continue
	elif ls[i - 1] < ls[i] and flagu == 0:
		flagu = 1
	elif ls[i - 1] > ls[i] and  flagu == 1:
		ans += 1
		flagu = 0
		flagd = 0
	elif ls[i - 1] > ls[i] and flagd == 0:
		flagd = 1
	elif ls[i - 1] < ls[i] and flagd == 1:
		ans += 1
		flagu = 0
		flagd = 0
	
	if i == n - 1:
		ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
n = input()
a = [int(e) for e in input().split()]
count = 1
for i in range(1, len(a)-1):
    if a[i] < a[i-1] and a[i+1] < a[i]:
        count = count + 1
    elif a[i] > a[i-1] and a[i] > a[i+1] :
        count = count + 1
print(count)

Submission status: Wrong Answer

Solution code:
n = 6
a = [int(e) for e in input().split()]
count = 1
for i in range(1, len(a)-1):
    if a[i] < a[i-1] and a[i+1] < a[i]:
        count = count + 1
    elif a[i] > a[i-1] and a[i] > a[i+1] :
        count = count + 1
print(count)

Submission status: Wrong Answer

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

ans = []

add_list = []
redu_list = []
pre = [a[0]]

index = 0
for i in range(1, n):
  v = a[i]
  if pre[index] < v:
    # add
    if len(redu_list) > 0:
      ans.append(pre)
      redu_list = []
      pre = [v]
      index = 0
      continue
      
    add_list.append(v)
  elif pre[index] == v:
    # as it is
    if len(add_list) != 0:
      add_list.append(v)
    else:
      redu_list.append(v)
  else:
    # reduce
    if len(add_list) > 0:
      ans.append(pre)
      add_list = []
      pre = [v]
      index = 0
      continue
    redu_list.append(v)
  index += 1
  pre.append(v)
    
print(len(ans)+1)

Submission status: Wrong Answer

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

res = 0
if n==1 or n==2:
    res = 1
else:
    for i in range(1,n-1):
        if lst[i-1] > lst[i] and lst[i] < lst[i+1]:
            res += 1
            lst[i] = lst[i+1]
        elif lst[i-1] < lst[i] and lst[i] > lst[i+1]:
            res += 1
            lst[i] = lst[i+1]
print(res + 1)