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:
def main():
    N = int(input())
    A = [int(a) for a in input().split()]

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

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

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

    print(ans + 1)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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


flg=0
cnt=0
swi=0

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

for i in range(1,n):
    if swi==1:
        if a[i-1]<a[i]:
            flg=1
        elif a[i-1]>a[i]:
            flg=-1
        else:
            flg=0
        swi=0
        continue

    if flg==1:
        if a[i-1]<=a[i]:
            continue
        else:
            cnt+=1
            flg=-1
            swi=1
    elif flg==-1:
        if a[i-1]>=a[i]:
            continue
        else:
            cnt+=1
            flg=1
            swi=1
    else:
        if a[i]<a[i-1]:
            flg=1
        elif a[i]>a[i-1]:
            flg=-1
        else:
            flg=0

print(cnt+1)

Submission status: Wrong Answer

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

def qSort(a):#クイックソート
    if len(a) in (0,1):
        return a
    p=a[-1]
    l=[x for x in a[:-1] if x<=p]
    r=[x for x in a[:-1] if x>p ]
    return qSort(l)+[p]+qSort(r)


def rqSort(a):#クイックソート逆順
    if len(a) in (0,1):
        return a
    p=a[-1]
    l=[x for x in a[:-1] if x>p]
    r=[x for x in a[:-1] if x<=p ]
    return qSort(l)+[p]+qSort(r)


count=1

j=0
for i in range(N+1):
  k=i
  if A[j:k]==qSort(A[j:k]) or A[j:k]==rqSort(A[j:k]):
    continue
  else:
    count+=1
    j=i-1
    
print(count)

Submission status: Wrong Answer

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

def qSort(a):#クイックソート
    if len(a) in (0,1):
        return a
    
    p=a[-1]
    l=[x for x in a[:-1] if x<=p]
    r=[x for x in a[:-1] if x>p ]
    
    return qSort(l)+[p]+qSort(r)


count=1

j=0
for i in range(N+1):
  k=i
  if (A[j:k]==qSort(A[j:k]) or A[j:k]==reversed(qSort(A[j:k]))):
    continue
  else:
    count+=1
    j=i-1
    
print(count)

Submission status: Wrong Answer

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

count=0

j=len(A)+1
for i in range(len(A)-1,0,-1):
  k=i
  if A[k:j]==sorted(A[k:j]) or A[k:j]==sorted(A[k:j],reverse=True):
    continue
  else:
    count+=1
    j=k+1
    
print(count+1)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n, *lst = map(int, open(0).read().split())
if n < 3:
  print(1)
  quit()
group = 1
start = (lst[1] >= lst[0])
for i in range(2, n - 1):
  if (lst[i] >= lst[i - 1]) != start:
    start = (lst[i + 1] >= lst[i])
    group += 1
if (lst[-1] >= lst[-2]) != start:
  group += 1
print(group)

Submission status: Wrong Answer

Solution code:
n, *lst = map(int, open(0).read().split())
if n < 3:
  print(1)
  quit()
group = 0
start = (lst[1] >= lst[0])
for i in range(2, n - 1):
  if (lst[i] >= lst[i - 1]) != start:
    start = (lst[i + 1] >= lst[i])
    group += 1
if (lst[-1] >= lst[-2]) != start:
  group += 1
if group != 0:
  group += 1
print(group)

Submission status: Wrong Answer

Solution code:
n, *lst = map(int, open(0).read().split())
if n < 3:
  print(0)
  quit()
group = 0
start = (lst[1] >= lst[0])
for i in range(2, n - 1):
  if (lst[i] >= lst[i - 1]) != start:
    start = (lst[i + 1] >= lst[i])
    group += 1
if (lst[-1] >= lst[-2]) != start:
  group += 1
if group != 0:
  group += 1
print(group)

Submission status: Wrong Answer

Solution code:
n, *lst = map(int, open(0).read().split())
if n < 3:
  print(0)
  quit()
group = 0
start = lst[1] >= lst[0]
for i in range(2, n - 1):
  if (lst[i] >= lst[i - 1]) != start:
    start = (lst[i + 1] > lst[i])
    group += 1
if (lst[-1] >= lst[-2]) != start:
  group += 1
if group != 0:
  group += 1
print(group)

Submission status: Wrong Answer

Solution code:
n, *lst = map(int, open(0).read().split())
if n < 3:
  print(0)
  quit()
group = 0
start = lst[1] > lst[0]
for i in range(2, n - 1):
  if (lst[i] > lst[i - 1]) != start:
    start = (lst[i + 1] > lst[i])
    group += 1
if (lst[-1] > lst[-2]) != start:
  group += 1
if group != 0:
  group += 1
print(group)

Submission status: Wrong Answer

Solution code:
n, *lst = map(int, open(0).read().split())
if n < 3:
  print(0)
  quit()
group = 0
start = lst[1] > lst[0]
for i in range(2, n - 1):
  if (lst[i] > lst[i - 1]) == start:
    continue
  else:
    start = lst[i + 1] > lst[i]
    group += 1
if (lst[-1] > lst[-2]) != start:
  group += 1
if group != 0:
  group += 1
print(group)

Submission status: Wrong Answer

Solution code:
n, *lst = map(int, open(0).read().split())
if n < 3:
  print(0)
  quit()
group = 1
start = lst[1] > lst[0]
for i in range(2, n - 1):
  if (lst[i] > lst[i - 1]) == start:
    continue
  else:
    start = lst[i + 1] > lst[i]
    group += 1
if (lst[-1] > lst[-2]) != start:
  group += 1
print(group)

Submission status: Wrong Answer

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

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


print(cnt)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

cnt = 0
flag = 0

for i in range(N-1):
  if A[i+1]-A[i] > 0:
    if flag == 1:
      cnt +=0
    elif flag == -1:
      cnt +=1
      flag = 0
    elif flag ==0:
      flag = 1
  elif A[i+1]-A[i] < 0:
    if flag == 1:
      cnt +=1
      flag =0
    elif flag ==-1:
      cnt +=1
    elif flag ==0:
      flag = -1
  elif A[i+1]-A[i] == 0:
    flag += 0
      
    
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-1):
  #print(A[i+1]-A[i])
  if A[i+1]-A[i] >= 0:
    if flag == 1:
      cnt +=0
    elif flag == -1:
      cnt +=1
      flag = 0
    elif flag ==0:
      flag = 1
  elif A[i+1]-A[i] <= 0:
    if flag == 1:
      cnt +=1
      flag =0
    elif flag ==-1:
      cnt +=1
    elif flag ==0:
      flag = -1
      
    
print(cnt+1)

Submission status: Wrong Answer

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

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

import sys
input = sys.stdin.readline

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


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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

        if judge==[]:
            judge.append(flag)
        else:
            ff=judge.pop()
            if flag==ff:
                judge.append(flag)
            else:
                check=i+1
                cnt+=1
                judge.append(flag)
if check==n-1:
    cnt+=1
if cnt!=0:
    cnt+=1
print(cnt)

Submission status: Wrong Answer

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

        if judge==[]:
            judge.append(flag)
        else:
            ff=judge.pop()
            if flag==ff:
                judge.append(flag)
            else:
                check=i+1
                cnt+=1
                judge.append(flag)
if check==n-1:
    cnt+=1
print(cnt+1)

Submission status: Wrong Answer

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

        if judge==[]:
            judge.append(flag)
        else:
            ff=judge.pop()
            if flag==ff:
                judge.append(flag)
            else:
                check.append(i+1)
                cnt+=1
                judge.append(flag)
if n-1 in check:
    cnt+=1
print(cnt+1)

Submission status: Wrong Answer

Solution code:
n,*a=map(int,open(0).read().split())
ans,b,c=0,a[0],0
for i in a:
    if i==b:
        continue
    elif i>b and c!=1:
        c=1
        ans+=1
    elif i<b and c!=-1:
        c=0
        ans+=1
    b=i    
print(ans//2+1)

Submission status: Wrong Answer

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

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


flg=0
cnt=0
swi=0
for i in range(1,n):
    if swi==1:
        if a[i-1]==a[i]:
            flg==0
        elif a[i-1]<a[i]:
            flg=1
        elif a[i-1]>a[i]:
            flg=-1
        swi=0
        continue

    if a[i-1]<a[i]:
        if flg==-1:
            cnt+=1
            swi=1
        flg=1
    elif a[i-1]>a[i]:
        if flg ==1:
            cnt+=1
            swi=1
        flg=-1

print(cnt+1)

Submission status: Wrong Answer

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

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


flg=0
cnt=0
swi=0
for i in range(1,n):
    if swi==1:
        if a[i]==a[i-1]:
            flg==0
        elif a[i-1]<a[i]:
            flg=1
        elif a[i-1]>a[i]:
            flg=-1
        swi=0
        continue

    if a[i-1]<a[i]:
        if flg==-1:
            cnt+=1
            swi=1
        flg=1
    elif a[i-1]>a[i]:
        if flg ==1:
            cnt+=1
            swi=1
        flg=-1

print(cnt+1)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
count = 1
skip = False
for i in range(0, N-2):
    if not skip:
        before = A[i+1] - A[i]
        after = A[i+2] - A[i+1]
        if ((before > 0) - (before < 0))*((after > 0) - (after < 0)) == -1:
            count += 1
            skip = True
    else:
        skip = False
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
count = 1
skip = False
for i in range(0, N-3):
    if not skip:
        before = A[i+1] - A[i]
        after = A[i+2] - A[i+1]
        if ((before > 0) - (before < 0))*((after > 0) - (after < 0)) == -1:
            count += 1
            skip = True
    else:
        skip = False
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
if N == 0:
    print(0)
    exit()
count = 1
skip = False
for i in range(0, N-2):
    if not skip:
        before = A[i+1] - A[i]
        after = A[i+2] - A[i+1]
        if abs(((before > 0) - (before < 0)) - ((after > 0) - (after < 0))) == 2:
            count += 1
            skip = True
    else:
        skip = False
        if i == N-2:
          count += 1
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
if N == 0:
    print(0)
    exit()
count = 1
skip = False
for i in range(0, N-2):
    if not skip:
        before = A[i+1] -A[i]
        after = A[i+2] - A[i+1]
        if abs(((before > 0) - (before < 0)) - ((after > 0) - (after < 0))) == 2:
            count += 1
            skip = True
    else:
        skip = False
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
count = 1
skip = False
for i in range(0, N-2):
    if not skip:
        before = A[i+1] -A[i]
        after = A[i+2] - A[i+1]
        if abs(((before > 0) - (before < 0)) - ((after > 0) - (after < 0))) == 2:
            count += 1
            skip = True
    else:
        skip = False
print(count)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
import numpy as np
n=int(input())
a=list(map(int,input().split()))
a.insert(0,0)
#連続する重複を除外
arr=np.array(a)
diff=np.diff(arr)
aa=arr[np.nonzero(diff)]
#print(diff,aa)

aaa=aa.tolist()
point=[]
cnt=1

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N  = int(input())
A = list(map(int, input().split()))
status = 0 # 0 = defalt 1 = up 2 = down
cnt = 1
for i in range(1,N):
    if A[i-1] < A[i]: #up petern
        if status == 0:
            status = 1
        elif status == 2:
            status = 0
            cnt += 1
    elif A[i-1] > A[i]:
        if status == 0:
            status = 2
        elif status:
            status = 0
            cnt += 1
    else:
        continue
print(cnt)

Submission status: Wrong Answer

Solution code:
N  = int(input())
A = list(map(int, input().split()))
status = 0 # 0 = defalt 1 = up 2 = down
cnt = 1
for i in range(1,N):
    if A[i-1] < A[i]: #up petern
        if status == 0:
            status = 1
        elif status == 2:
            status = 0
            cnt += 1
    elif A[i-1] > A[i]:
        if status == 0:
            status = 2
        elif status:
            status = 0
            cnt += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
N  = int(input())
A = list(map(int, input().split()))
status = 0 # 0 = defalt 1 = up 2 = down
cnt = 0
pre = A[0]
for i in range(1,N):
    if pre < A[i]: #up petern
        if status == 0:
            status = 1
        elif status == 2:
            status = 0
            cnt += 1
    elif pre > A[i]:
        if status == 0:
            status = 2
        elif status:
            status = 0
            cnt += 1
    pre = A[i]
print(cnt+1)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()

n = int(input())
a = list(map(int, input().split()))
a_ = []
"""
for i in a:
    if not a_:
        a_.append(i)
    elif i != a_[-1]:
        a_.append(i)
"""
cnt = 0
flag = 0
"""
if len(a_) == 1 or len(a_) == 2:
    print(1)
    sys.exit()
"""
for i in range(n - 1):
    if flag == 0:
        if a[i + 1] - a[i] > 0:
            flag = 1
        else:
            flag = -1
    elif flag == 1 and a[i + 1] - a[i] < 0:
        cnt += 1
        flag = 0
    elif flag == -1 and a[i + 1] - a[i] > 0:
        cnt += 1
        flag = 0
print(cnt + 1)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()

n = int(input())
a = list(map(int, input().split()))
a_ = []
for i in a:
    if not a_:
        a_.append(i)
    elif i != a_[-1]:
        a_.append(i)
cnt = 0
flag = 0
if len(a_) == 1 or len(a_) == 2:
    print(1)
    sys.exit()
for i in range(len(a_) - 1):
    if flag == 0:
        if a[i + 1] - a[i] > 0:
            flag = 1
        else:
            flag = -1
    elif flag == 1 and a[i + 1] - a[i] < 0:
        cnt += 1
        flag = 0
    elif flag == -1 and a[i + 1] - a[i] > 0:
        cnt += 1
        flag = 0
print(cnt + 1)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()

n = int(input())
a = list(map(int, input().split()))
a_ = []
for i in a:
    if not a_:
        a_.append(i)
    elif i != a_[-1]:
        a_.append(i)
cnt = 0
flag = 0
if len(a_) == 1 or len(a_) == 2:
    print(1)
    sys.exit()
for i in range(len(a_) - 1):
    if flag == 0:
        if a[i + 1] - a[i] > 0:
            flag = 1
        else:
            flag = -1
        continue
    if flag == 1 and a[i + 1] - a[i] < 0:
        cnt += 1
        flag = 0
        #if i + 2 < n:
            #a[i + 1] = a[i + 2]
    elif flag == -1 and a[i + 1] - a[i] > 0:
        cnt += 1
        flag = 0
        #if i + 2 < n:
            #a[i + 1] = a[i + 2]
print(cnt + 1)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()

n = int(input())
a = list(map(int, input().split()))
a_ = []
for i in a:
    if not a_:
        a_.append(i)
    elif i != a_[-1]:
        a_.append(i)
cnt = 0
flag = 0
if len(a_) == 1 or len(a_) == 2:
    print(1)
    sys.exit()
for i in range(n - 1):
    if flag == 0:
        if a[i + 1] - a[i] > 0:
            flag = 1
        else:
            flag = -1
        continue
    if flag == 1 and a[i + 1] - a[i] < 0:
        cnt += 1
        flag = 0
        if i + 2 < n:
            a[i + 1] = a[i + 2]
    elif flag == -1 and a[i + 1] - a[i] > 0:
        cnt += 1
        flag = 0
        if i + 2 < n:
            a[i + 1] = a[i + 2]
print(cnt + 1)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()

n = int(input())
a = list(map(int, input().split()))
a_ = []
for i in a:
    if not a_:
        a_.append(i)
    elif i != a_[-1]:
        a_.append(i)
cnt = 0
flag = 0
if len(a_) == 1 or len(a_) == 2:
    print(1)
    sys.exit()
for i in range(n - 1):
    if flag == 0:
        if a[i + 1] - a[i] > 0:
            flag = 1
        else:
            flag = -1
        continue
    if flag == 1 and a[i + 1] - a[i] < 0:
        cnt += 1
        flag = 0
    elif flag == -1 and a[i + 1] - a[i] > 0:
        cnt += 1
        flag = 0
print(cnt + 1)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()

n = int(input())
a = list(map(int, input().split()))
a_ = []
for i in a:
    if not a_:
        a_.append(i)
    elif i != a_[-1]:
        a_.append(i)
cnt = 0
flag = 0
if len(a_) == 1 or len(a_) == 2:
    print(0)
    sys.exit()
for i in range(n - 1):
    if flag == 0:
        if a[i + 1] - a[i] > 0:
            flag = 1
        else:
            flag = -1
        continue
    if flag == 1 and a[i + 1] - a[i] < 0:
        cnt += 1
        flag = 0
    elif flag == -1 and a[i + 1] - a[i] > 0:
        cnt += 1
        flag = 0
print(cnt + 1)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()

n = int(input())
a = list(map(int, input().split()))
a_ = []
for i in a:
    if not a_:
        a_.append(i)
    elif i != a_[-1]:
        a_.append(i)
cnt = 0
flag = 0
for i in range(n - 1):
    if flag == 0:
        if a[i + 1] - a[i] > 0:
            flag = 1
        else:
            flag = -1
        continue
    if flag == 1 and a[i + 1] - a[i] < 0:
        cnt += 1
        flag = 0
    elif flag == -1 and a[i + 1] - a[i] > 0:
        cnt += 1
        flag = 0
print(cnt + 1)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()

n = int(input())
a = list(map(int, input().split()))
if n == 1 or n == 2:
    print(0)
    sys.exit()
cnt = 0
for i in range(2, n):
    if (a[i] - a[i - 1]) * (a[i - 1] - a[i - 2]) < 0:
        cnt += 1
        a[i - 1] = a[i]
print(cnt + 1)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
old = A[0]
d = 0
ans = 1
for i in range(1,N):
  d_old = d
  d = A[i] - old
  if d*d_old <0:
    ans += 1
    d = 0
  old = A[i]
print(ans)

Submission status: Wrong Answer

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

cnt = 1
sa = 0
for i in range(1, n):
    new = a[i] - a[i-1]
    if new * sa < 0:
        cnt += 1
        sa = 0
        continue
    sa = new
print(cnt)

Submission status: Wrong Answer

Solution code:
import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n = int(ipt())
    a = [int(i) for i in ipt().split()]
    ans = 0
    t = 0
    pa = 0
    for i in a:
        if pa > i:
            if t > 0:
                t = 0
                ans += 1
            else:
                t = -1
        elif pa < i:
            if t < 0:
                t = 0
                ans += 1
            else:
                t = 1
        pa = i
    print(ans+1)

    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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