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:
N = int(input())
A = list(map(int, input().split()))
cnt = 1
if N == 1 or len(set(A)) == 1:
	print(cnt)
	
status = 0
for i in range(N-1):
	if A[i] > A[i+1]:
		if status == 0: status = 1
		elif status == -1:
			status = 0
			cnt += 1
	elif A[i] < A[i+1]:
		if status == 0: status = -1
		elif status == 1:
			status = 0
			cnt += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
i=0
ans=0
while i<n:
  k=set()
  while a[i]<=a[i+1] if (i+1)<n else False:
    k.add(i)
    k.add(i+1)
    i+=1
  if len(k)!=0:
    i+=1
    ans+=1
  if (i+1)==n:
    break
  k=set()
  while a[i]>=a[i+1] if (i+1)<n else False:
    k.add(i)
    k.add(i+1)
    i+=1
  if len(k)!=0:
    i+=1
    ans+=1
  if (i+1)==n:
    break
#if i==n:
print(ans)
#else:
#  print(ans+1)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
i=0
ans=0
while i<n:
  k=set()
  while a[i]<=a[i+1] if (i+1)<n else False:
    k.add(i)
    k.add(i+1)
    i+=1
  if len(k)!=0:
    i+=1
    ans+=1
  if (i+1)==n:
    break
  k=set()
  while a[i]>=a[i+1] if (i+1)<n else False:
    k.add(i)
    k.add(i+1)
    i+=1
  if len(k)!=0:
    i+=1
    ans+=1
  if (i+1)==n:
    break
if i==n:
  print(ans)
else:
  print(ans+1)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
i=0
ans=0
while i<n:
  k=set()
  while a[i]<=a[i+1] if (i+1)<n else False:
    k.add(i)
    k.add(i+1)
    i+=1
  if len(k)!=0:
    i+=1
    ans+=1
  k=set()
  while a[i]>=a[i+1] if (i+1)<n else False:
    k.add(i)
    k.add(i+1)
    i+=1
  if len(k)!=0:
    i+=1
    ans+=1
  if (i+1)==n:
    break
if i==n:
  print(ans)
else:
  print(ans+1)

Submission status: Wrong Answer

Solution code:
ans = 1
rans = 1
N = int(input())
A = list(map(int, input().split()))
up = True
if N != 1 and A[0] > A[1]:
  up = False
for i in range(1, N-1):
  if A[i] > A[i+1] and up:
    ans += 1
    up = False
  elif A[i] < A[i+1] and not up:
    ans += 1
    up = True
for i in reversed(range(1, N-1)):
  if A[i] > A[i+1] and up:
    rans += 1
    up = False
  elif A[i] < A[i+1] and not up:
    rans += 1
    up = True
print(min(ans, rans))

Submission status: Wrong Answer

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

cnt = n
prev = a[0]
trend ='N'

for i in range(1, n):
    if a[i] > prev:
        if (trend == 'U' or trend == 'N'):
            cnt -= 1
            trend ='U'
        else:
            trend = 'N'
    if a[i] < prev:
        if (trend == 'D' or trend == 'N'):
            cnt -= 1
            trend = 'D'
        else:
            trend = 'N'
    if a[i] == prev:
        cnt -= 1
        trend = 'N'
    prev = a[i]
print(cnt)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
from itertools import permutations
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

cnt = 1
x = []
for i in range(1, N):
    if A[i] >= A[i - 1]:
        cnt += 1
    else:
        x.append(cnt)
        cnt = 1
x.append(cnt)

ans = 0
i = 0
while i < len(x):
    if x[i] != 1:
        ans += 1
        i += 1
        continue
    j = i
    while j < len(x) and x[j] == 1:
        j += 1
    i = j
    ans += 1
print(ans)

Submission status: Wrong Answer

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

list1 =[0] * n
cnt = 1  
for i in range(n-1):
  if aa[i+1] > aa[i]:
    list1[i+1] = 1
  elif aa[i+1] < aa[i]:
    list1[i+1] = -1
  elif aa[i+1] == aa[i]:    
    list1[i+1] = 0

while 1 in list1 and -1 in list1:
  del list1[:max(list1.index(1), list1.index(-1))]
  list1[0] == 0
  cnt += 1
  
print(cnt)

Submission status: Wrong Answer

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

list1 =[0] * n
cnt = 1  
for i in range(n-1):
  if aa[i+1] > aa[i]:
    list1[i+1] = 1
  elif aa[i+1] < aa[i]:
    list1[i+1] = -1
  elif aa[i+1] == aa[i]:    
    list1[i+1] = 0

print(list1)    
while 1 in list1 and -1 in list1:
  del list1[:max(list1.index(1), list1.index(-1))]
  list1[0] == 0
  cnt += 1
  
print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
num_list = list(map(int, input().split()))
ans = 1
cnt_list = [0] * n
a = int()

for i in range(1,n):
    if num_list[i-1] > num_list[i]:
        cnt_list[i] = -1
    if num_list[i-1] < num_list[i]:
        cnt_list[i] = 1

while 1 in cnt_list and -1 in cnt_list:
    del cnt_list[:max(cnt_list.index(1),cnt_list.index(-1))]
    ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
num_list = list(map(int, input().split()))
ans = 1
cnt_list = [0] * n
a = int()

for i in range(1,n):
    if num_list[i-1] > num_list[i]:
        cnt_list[i] = -1
    if num_list[i-1] < num_list[i]:
        cnt_list[i] = 1

for i in range(1,n-1):
    if not cnt_list[i] == 0:
        a = cnt_list[i]
        break

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

print(ans+1)

Submission status: Wrong Answer

Solution code:
n = int(input())
num_list = list(map(int, input().split()))
ans = 1
cnt_list = [0] * n
a = int()

for i in range(1,n):
    if num_list[i-1] > num_list[i]:
        cnt_list[i] = -1
    if num_list[i-1] < num_list[i]:
        cnt_list[i] = 1

for i in range(1,n-1):
    if not cnt_list[i] == 0:
        a = cnt_list[i]
        break

for i in range(1,n-1):
    if cnt_list[i] == -1 * a:
        if cnt_list[i+1] == a or cnt_list[i-1] == a:
            ans+=1
if cnt_list[n-1] == -1*a and cnt_list[n-2] == a:
    ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
num_list = list(map(int, input().split()))
ans = 1
cnt_list = [0] * n

for i in range(1,n):
    if num_list[i-1] > num_list[i]:
        cnt_list[i] = -1
    if num_list[i-1] < num_list[i]:
        cnt_list[i] = 1

for i in range(1,n-1):
    if cnt_list[i] == 1:
        if cnt_list[i+1] == -1 or cnt_list[i-1] == -1:
            ans+=1

print(ans)

Submission status: Wrong Answer

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

list1 =[0] * n
list1[0] = 1
list2 = [0] * (n-1)

if n >1:
  list1[1] = 1
  
for i in range(n-1):
  if aa[i+1] - aa[i] > 0:
    list2[i] = 1
  elif aa[i+1] - aa[i] < 0:
    list2[i] = -1
  elif aa[i+1] - aa[i] == 0:
    if i == 0:
      list2[i] == 0
    else:
      list2[i] = list2[i-1]


for i in range(2,n):
  if list1[i-2] == list1[i-1]:
    if (aa[i] - aa[i-1]) * (aa[i-1] -aa[i-2]) < 0:
      list1[i] = list1[i-1] + 1
    elif (aa[i] - aa[i-1]) * (aa[i-1] -aa[i-2]) > 0:
      list1[i] = list1[i-1]
    else:
      if list2[i-2]*(aa[i]-aa[i-1]) < 0:
         list1[i] = list1[i-1] + 1
      else:
         list1[i] = list1[i-1]
      
        
  else:
    list1[i] = list1[i-1]
    
print(list1[n-1])

Submission status: Wrong Answer

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

list1 =[0] * n
list1[0] = 1

if n >1:
  list1[1] = 1

for i in range(2,n):
  if list1[i-2] == list1[i-1]:
    if (aa[i] - aa[i-1]) * (aa[i-1] -aa[i-2]) < 0:
      list1[i] = list1[i-1] + 1
    elif (aa[i] - aa[i-1]) * (aa[i-1] -aa[i-2]) > 0:
      list1[i] = list1[i-1]
    elif (aa[i] - aa[i-1]) * (aa[i-1] -aa[i-2]) == 0:
      if aa[i] == aa[i-1]:
        list1[i] = list1[i-1]
      else:
        list1[i] = list1[i-1] + 1
  else:
    list1[i] = list1[i-1]
    
print(list1[n-1])

Submission status: Wrong Answer

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

list1 =[0] * n
list1[0] = 1

if n >1:
  list1[1] = 1

for i in range(2,n):
  if list1[i-2] == list1[i-1]:
    if (aa[i] - aa[i-1]) * (aa[i-1] -aa[i-2]) < 0:
      list1[i] = list1[i-1] + 1
    else:
      list1[i] = list1[i-1]
  else:
    list1[i] = list1[i-1]
    
print(list1[n-1])

Submission status: Wrong Answer

Solution code:
#editorial
n=int(input())
if n==1:
    print(1)
    exit()
*a,=map(int, input().split())
pre=a[1]-a[0]
ans=1
for i in range(2,n):
    if pre*(a[i]-a[i-1])<0:
        pre=0
        ans+=1
    else:
        pre=a[i]-a[i-1]
print(ans)

Submission status: Wrong Answer

Solution code:
#editorial
n=int(input())
*a,=map(int, input().split())
pre=0
ans=1
for i in range(1,n):
    if pre*(a[i]-a[i-1])<0:
        pre=0
        ans+=1
    else:
        pre=a[i]-a[i-1]
print(ans)

Submission status: Wrong Answer

Solution code:
#editorial
n=int(input())
*a,=map(int, input().split())
pre=0
ans=0
for i in range(1,n):
    if pre*(a[i]-a[i-1])<0:
        pre=0
        ans+=1
    else:
        pre=a[i]-a[i-1]
print(ans)

Submission status: Wrong Answer

Solution code:
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)

Submission status: Wrong Answer

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

ans = 0
pass_flag = False
for i in range(1,n-1):
    if pass_flag:
        pass_flag = False
        continue
    if A[i-1] < A[i] and A[i] > A[i+1]:
        if not ans:
            ans += 1
        ans += 1
        pass_flag = True
    if A[i-1] > A[i] and A[i] < A[i+1]:
        if not ans:
            ans += 1
        ans += 1
        pass_flag = True

print(ans)

Submission status: Wrong Answer

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

cnt = 0
if n == 1:
  print (1)
  exit()
c = 0
for i in range(n-1):
  if c == 2 and a[i+1] < a[i]:
    cnt += 1
    c = -1
  elif c == 1 and a[i+1] > a[i]:
    cnt += 1
    c = -1
  else:
    if a[i+1] > a[i]:
      c = 2
    elif a[i+1] == a[i]:
      c = 0
    else:
      c = 1
print (cnt+1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
N = int(input())
A = list(map(int, input().split()))
# print(f'{A=}')

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

trend = 1 if A[1] - A[0] >= 0 else -1
skip = False
ans = 1
for i in range(2, N - 1):
    if skip:
        skip = False
        continue
    if (A[i + 1] - A[i]) * (A[i] - A[i - 1]) < 0:
        ans += 1
        skip = True
        # print(f'{A[i]=}, {i=}')

if (A[-1] - A[-2]) * (A[-2] - A[-3]) < 0:
    ans += 1

# for i in range(2, N - 1):
#     if skip:
#         skip = False
#         continue
#     if A[i + 1] - A[i] < 0 and trend == 1:
#         ans += 1
#         trend *= -1
#         skip = True
#         print(f'{A[i]=}, {i=}')
#         continue
#     if A[i + 1] - A[i] > 0 and trend == -1:
#         ans += 1
#         trend *= -1
#         skip = True
#         print(f'{A[i]=}, {i=}')
print(ans)

# 12 12 12 12 1

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(ans+1)

Submission status: Wrong Answer

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

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

print(ans+1)

Submission status: Wrong Answer

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

B=[A[i]-A[i-1] for i in range(1,N)]
ans=0
first=B[0]
for i in range(N-1):
    if first*B[i]>=0:
        pass
    else:
        ans+=1
        if i+1<=N-2:
            first=B[i+1]

print(ans+1)

Submission status: Wrong Answer

Solution code:
# vim: fileencoding=utf-8


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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
A=list(map(int,input().split()))
count=1
data=[]
for i in range(n-2):
    if (A[i]-A[i+1]>0 and A[i+1]-A[i+2]<0) or (A[i]-A[i+1]<0 and A[i+1]-A[i+2]>0):
        if i-1 not in data:
            count += 1
            data.append(i)
print(count)

Submission status: Wrong Answer

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

flag = 0
ans = 1
tmp = 0

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import deque

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

tmp = []
cnt = 0
while d:
  v = d.popleft()
  if len(tmp)<=1:
    pass
  else:
    if not v >= tmp[-1] >= tmp[-2] or v <= tmp[-1] <= tmp[-2]:
      tmp = []
      cnt += 1
  tmp.append(v)
#  print(d,tmp,cnt)
if tmp:
  cnt+=1
print(cnt)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))

v = A[1:] - A[:-1]
# print(v)

cnt = 0
i = 0
while i + 1 < len(v):
    # print('i', i, v[i])
    if v[i] * v[i + 1] < 0:
        cnt += 1
        i += 1
    i += 1
ans = cnt + 1
print(ans)

# 単調非減少 : v[i + 1] - v[i] >= 0
# 単調非増加 : v[i + 1] - v[i] <= 0
# 折り返し点 : v[i] * v[i + 1] < 0 and v[i + 1] * v[i + 2] < 0

Submission status: Wrong Answer

Solution code:
'''
Created on 2020/08/28

@author: harurun
'''
def main():
  import sys
  pin=sys.stdin.readline
  pout=sys.stdout.write
  perr=sys.stderr.write
  
  N=int(pin())
  A=list(map(int,pin().split()))
  if N==1 or N==2:
    print(1)
    return
  ans=1
  i=0
  while i<N-2:
    if (A[i]<A[i+1] and A[i+1]>A[i+2]) or (A[i]>A[i+1] and A[i+1]<A[i+2]):
      ans+=1
      i+=1
    i+=1
  print(ans)
  return
main()

Submission status: Wrong Answer

Solution code:
# 方針
# リストaのi番目に対して、a[i]=a[i+1]かcnt=1(ひとつ前のiのときに区切りが必要と分かった場合)ならば次へ
# そうでないならa[i],a[i+1],a[i+2]を比較し、単調となるなら次へ、凸となるならansを増やし、cnt=1に。
# ansを答える。

n = int(input())
a = list(map(int, input().split()))
ans = 1 # 数列の個数
cnt = 0 # 直前に分割したかのフラグ
flag = [0,0,0,0,0] #全体フラグ管理
for i in range(n-2):
#     print(flag)
#     print(i)
    if a[i] == a[i+1] or cnt == 1:
        cnt = 0
        flag[4] += 1
        continue
    if a[i] < a[i+1]:
        if a[i+1] <= a[i+2]:
            flag[0] +=1
            continue
        elif a[i+1] > a[i+2]:
            flag[1] += 1
            ans += 1
            cnt = 1
            continue
    if a[i] > a[i+1]:
        if a[i+1] >= a[i+2]:
            flag[2] +=1
            continue
        elif a[i+1] < a[i+2]:
            flag[3] += 1
            ans += 1
            cnt = 1
            continue
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 1 # 数列の個数
cnt = 0 # 直前に分割したかのフラグ
flag = [0,0,0,0,0] #全体フラグ管理
for i in range(n-2):
#     print(flag)
#     print(i)
    if a[i] == a[i+1] or cnt == 1:
        cnt = 0
        flag[4] += 1
        continue
    if a[i] < a[i+1]:
        if a[i+1] <= a[i+2]:
            flag[0] +=1
            continue
        elif a[i+1] > a[i+2]:
            flag[1] += 1
            ans += 1
            cnt = 1
            continue
    if a[i] > a[i+1]:
        if a[i+1] >= a[i+2]:
            flag[2] +=1
            continue
        elif a[i+1] < a[i+2]:
            flag[3] += 1
            ans += 1
            cnt = 1
            continue
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 1
cnt = 0
flag = [0,0,0,0,0]
for i in range(n-2):
#     print(flag)
#     print(i)
    if a[i] == a[i+1] or cnt == 1:
        cnt = 0
        flag[4] += 1
        continue
    if a[i] < a[i+1]:
        if a[i+1] <= a[i+2]:
            flag[0] +=1
            continue
        elif a[i+1] > a[i+2]:
            flag[1] += 1
            ans += 1
            cnt = 1
            continue
    if a[i] > a[i+1]:
        if a[i+1] >= a[i+2]:
            flag[2] +=1
            continue
        elif a[i+1] < a[i+2]:
            flag[3] += 1
            ans += 1
            cnt = 1
            continue
            
b = list(reversed(a))
bns = 1
dnt = 0
glag = [0,0,0,0,0]
for i in range(n-2):
#     print(glag)
#     print(i)
    if b[i] == b[i+1] or dnt == 1:
        dnt = 0
        glag[4] += 1
        continue
    if b[i] < b[i+1]:
        if b[i+1] <= b[i+2]:
            glag[0] +=1
            continue
        elif b[i+1] > b[i+2]:
            glag[1] += 1
            bns += 1
            dnt = 1
            continue
    if b[i] > b[i+1]:
        if b[i+1] >= b[i+2]:
            glag[2] +=1
            continue
        elif b[i+1] < b[i+2]:
            glag[3] += 1
            bns += 1
            dnt = 1
            continue


print(min(ans,bns))

Submission status: Wrong Answer

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

# print(flag)
print(ans)

Submission status: Wrong Answer

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

n = k()
a = l()

i = 0
while i < n-1:
    if (a[i-1]<a[i]>a[i+1]) or (a[i-1]>a[i]<a[i+1]):
        cnt += 1
        i+=ans
        ans = 0
    i+=1
    ans += 1

print(cnt+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

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

print(ans+co)

Submission status: Wrong Answer

Solution code:
n=int(input())
lis=list(map(int,input().split()))
ans=1
if n<=2:
  ans=1
else:
  bb=lis.pop(0)
  b=lis.pop(0)
  f=False
for i in lis:
  if n<=2:
    break
  if f:
    f^=1
  elif (bb>b and b<i) or (bb<b and b>i):
    ans+=1
    f^=1
  bb=b
  b=i
print(ans)

Submission status: Wrong Answer

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

cnt = 1 
for i in range(N-2):
    if A[i+1]==A[i+2]:
        A[i+1]= A[i]
    elif (A[i]-A[i+1])*(A[i+1]-A[i+2])>0:
        continue 
    else:
        cnt+=1
print(cnt)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a_ls = list(map(int, input().split()))
times1 = 0
i = 1
while 1 <= i < n-1:
    if a_ls[i-1] < a_ls[i] > a_ls[i+1] or a_ls[i-1] > a_ls[i] < a_ls[i+1]:
        times1 += 1
        i += 1
    i += 1

times2 = 0
i = n-2
while 1 <= i < n-1:
    if a_ls[i-1] < a_ls[i] > a_ls[i+1] or a_ls[i-1] > a_ls[i] < a_ls[i+1]:
        times2 += 1
        i -= 1
    i -= 1
times = min(times1,times2)

print(times+1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a_ls = list(map(int, input().split()))
times = 0
i = 1
while 1 <= i < n-1:
    if a_ls[i-1] < a_ls[i] > a_ls[i+1] or a_ls[i-1] > a_ls[i] < a_ls[i+1]:
        times += 1
        i += 1
    i += 1
print(times+1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a_ls = list(map(int, input().split()))
times = 0
i = 0
# indの右側を切断するかどうか
cut = [0] * n
while i+2 < n:
    if a_ls[i] < a_ls[i+1] > a_ls[i+2] or a_ls[i] > a_ls[i+1] < a_ls[i+2] and not cut[i]:
        times += 1
        cut[i+1] = 1
    i += 1
if times > 0:
    print(times+1)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
n = int(input())
a_ls = list(map(int, input().split()))
times = 0
i = 0
# indの右側を切断するかどうか
cut = [0] * n
while i+2 < n:
    if a_ls[i] < a_ls[i+1] > a_ls[i+2] or a_ls[i] > a_ls[i+1] < a_ls[i+2] and not cut[i]:
        times += 1
        cut[i+1] = 1
    i += 1
print(times+1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a_ls = list(map(int, input().split()))
times = 0
i = 0
while i+2 < n:
    if a_ls[i] < a_ls[i+1] > a_ls[i+2] or a_ls[i] > a_ls[i+1] < a_ls[i+2]:
        times += 1
        i += 1
    i += 1
print(times+1)

Submission status: Wrong Answer

Solution code:
s = input()

if s.count("R") == 0:
    print(0)
elif s.count("R") == 1:
    print(1)
elif s.count("R") == 3:
    print(3)
else:
    if s == "RRS" or s == "SRR":
        print(2)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
s = input()

if s.count("R") == 0:
    print(0)
elif s.count("R") == 1:
    print(1)
elif s.count("R") == 3:
    print(3)
else:
    if s == "*R*":
        print(2)
    else:
        print(1)

Submission status: Wrong Answer

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

n = k()
a = l()
i = 1
while i < n-1:
    if ((a[i-1]<a[i]) and (a[i]>a[i+1])) or ((a[i-1]>a[i]) and (a[i]<a[i+1])):
        cnt += 1
        i += 1
    i += 1

print(cnt+1)

Submission status: Wrong Answer

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

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

print(list)

i = 0
count = 0

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

print(count + 1)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

up = None

ans = 0

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

print(ans + 1)

Submission status: Wrong Answer

Solution code:
def solve(): 

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

    i = 0

    result = 1

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

    print(result)    

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
B=[2]*(N)
if N<=2:
    print(1)
    exit(0)
 
for i in range(N-1):
    if A[i]>A[i+1]:
        B[i]=-1
    elif A[i]==A[i+1]:
        B[i]=0
    elif A[i]<A[i+1]:
        B[i]=1
i=0
cnt=[0]*N
while True:
    if B[i]==1:
        if B[i+1]==0 or B[i+1]==1:
            i+=1
        elif B[i+1]==-1:
            if cnt[i-1]==0 or i==0:
                cnt[i]=1
            i+=1
        else:
            i+=1
    if B[i]==0:
        i+=1
    if B[i]==-1:
        if B[i+1]==0 or B[i+1]==-1:
            i+=1
        elif B[i+1]==1:
            if cnt[i-1]==0 or i==0:
                cnt[i]=1
            i+=1
        else:
            i+=1
    if B[i]==2:
        break
print(cnt.count(1)+1)