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:
#!/usr/bin/env python3
import sys


def solve(N: int, A: "List[int]"):
    ix = 0
    cnt = 0
    while(ix < N):
        nix = ix
        tA = A[ix]
        minx = 1
        while(nix+1 < N and A[nix] <= A[nix+1]):
            minx += 1
            nix += 1
        nix = ix
        tA = A[ix]
        maxx = 1
        while(nix+1 < N and A[nix] >= A[nix+1]):
            minx += 1
            nix += 1
        ix = ix+max(minx, maxx)
        cnt += 1

    print(cnt)

    return


# Generated by 1.1.5 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=int(input())
a=list(map(int,input().split()))
a.insert(0,0)
print(a,len(a))
cnt=1
val=0
l=len(a)-1
for n in range(1,l):
	if a[n-1]<=a[n]:
		val=1
		continue
	elif a[n-1]>a[n] and val==1:
		if n==l-2:
			cnt+=1
		val=0
		cnt+=1
	elif a[n-1]>=a[n]:
		val=2
		continue
	elif a[n-1]<a[n] and val==2:
		if n==l-2:
			cnt+=1
		val=0
		cnt+=1
print(cnt)

Submission status: Wrong Answer

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

ans = 0
l = 0
ud = 0
for a in A:
  if l==0:
    ans += 1
  elif l==1:
    if b>a:
      ud = 1
      l+=1
    elif b<a:
      ud = -1
      l+=1
  else:
    if b>a and ud == -1:
      ans += 1
      l = 1
    elif b<a and ud == 1:
      ans += 1
      l = 1
  b = a
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ll=[a[0],a[1]]
if a[0]<a[1]:
    f=0
elif a[0]>a[1]:
    f=1
else:
    f=2

for i in range(2,n):
    if a[i-1]<a[i]:
        if f==0 or f==2:
            ll.append(a[i])
            f=0
        else:
            l.append(ll)
            ll=[a[i]]
            f=2
    elif  a[i-1]>=a[i]:
        if f==1 or f==2:
            ll.append(a[i])
            f=1
        else:
            l.append(ll)
            ll=[a[i]]
            f=2
    else:
        ll.append(a[i])
l.append(ll)
print(len(l))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(c) for c in input().split(' ')]
#a = [1, 2, 3, 2, 2, 1]
l = len(a)
c = 1
for i in range(l):
    if i == 0:
        if l == 1:
            pass
        else:
            prev = a[0]            
            if a[1] > a[0]:
                inc = True
            else:
                inc = False
        continue
    current = a[i]
    if prev == current:
        pass
    elif prev  < current and inc :
        pass
    elif prev < current and not inc:
        inc = True
        c += 1
    elif prev > current and not inc:
        pass
    elif prev > current and inc:
        inc = False
        c +=1
    prev = current
print(c)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(c) for c in input().split(' ')]
l = len(a)
c = 1
for i in range(l):
    if i == 0:
        if l == 1:
            pass
        else:
            prev = a[0]
            if a[1] > a[0]:
                inc = True
            else:
                inc = False
        continue
    current = a[i]
    if prev  < current and inc :
        pass
    elif prev > current and not inc:
        inc = True
        c += 1
    elif prev < current and not inc:
        pass
    elif prev > current and inc:
        inc = False
        c +=1
    prev = current
print(c)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys, math, fractions, itertools, collections, copy, heapq


def solve(N: int, A: "List[int]"):
    counter = 0
    i = 1
    while i < N-1:
        if (A[i-1] - A[i])*(A[i] - A[i+1]) < 0:
            counter += 1
            i += 1
        i += 1
    print(counter+1)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#! /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
N = int(input())
A = list(map(int, input().split()))
count = 1
tmp = -1
if N <= 2:
    print('1')
    exit()
for i in range(N - 2):
    if (A[i + 2] - A[i + 1]) * (A[i + 1] - A[i]) < 0 and tmp < i:
        count += 1
        tmp = i+1
print(str(count))

Submission status: Wrong Answer

Solution code:
#! /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
N = int(input())
A = list(map(int, input().split()))
count = 1
tmp = -1
for i in range(N - 2):
    if (A[i + 2] - A[i + 1]) * (A[i + 1] - A[i]) < 0 and tmp < i:
        count += 1
        tmp = i+1
print(str(count))

Submission status: Wrong Answer

Solution code:

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

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

Submission status: Wrong Answer

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

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

print (count)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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


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

Submission status: Wrong Answer

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

c = 1
i =2

if N ==1 or N ==2:
    print(1)
    exit()
while i<N:
    if (A[i-1]-A[i-2])*(A[i]-A[i-1])<0:
        c=c+1
        i = i+1
    i = i+1
print(c)

Submission status: Wrong Answer

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

c = 0
i =2

if N ==1 or N ==2:
    print(1)
    exit()
while i<N:
    if (A[i-1]-A[i-2])*(A[i]-A[i-1])<0:
        c=c+1
        i = i+1
    i = i+1
print(c+1)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if n==1:
        print(1)
else:
    ans=1

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

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

Submission status: Wrong Answer

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

if n==1:
        print(1)
else:
    ans=1

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

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

Submission status: Wrong Answer

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

if n==1:
        print(1)
else:
    ans=1

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

    c=b[0]
    for i in range(1,n-1):
            if c*b[i]>=0:
                    c=b[i]
            else:
                    ans+=1
                    c=b[i]
    print(int(ans/2+1))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

print(ans)

Submission status: Wrong Answer

Solution code:
N = input()
N =int(N)
nums= list(map(int,input().split(" ")))
tendency = 0
ans = 1
diff = []
for i in  range(N -1 ):
    diff.append(nums[i+1] - nums[i])
temp = 1
for i in range(len(diff) -1 ):
    temp *= diff[i] * diff[i + 1]
    if temp < 0:
        ans += 1


print(ans)

Submission status: Wrong Answer

Solution code:
# AGC013A - Sorted Arrays
def main():
    n = int(input())
    A = list(map(int, input().rstrip().split())) + [0]
    ans, cur = 0, A[0]
    trend = A[0] < A[1]  # increasing (1) / decreasing (0)
    flat = 0  # after changing a trend, if same numbers continue or not
    for i, j in enumerate(A[1:-1], start=1):
        if flat and j != cur:  # find out a new trend
            trend = cur < j
            flat ^= 1
        elif (trend and j < cur) or (not trend and j > cur):  # change of trend
            ans += 1
            if j != A[i + 1]:
                flg = j < A[i + 1]
            else:
                flat ^= 1
        cur = j
    print(ans + 1)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# AGC013A - Sorted Arrays
def main():
    n = int(input())
    A = list(map(int, input().rstrip().split())) + [0]
    ans, cur = 0, A[0]
    trend = A[0] < A[1]  # increasing (1) / decreasing (0)
    flat = 0  # after changing a trend, if same numbers continue or not
    for i, j in enumerate(A[1:-1], start=1):
        if flat and j != cur:  # find out a new trend
            trend = cur < j
            flat ^= 1
        if (trend and j < cur) or (not trend and j > cur):  # change of trend
            ans += 1
            if j != A[i + 1]:
                flg = j < A[i + 1]
            else:
                flat ^= 1
        cur = j
    print(ans + 1)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

ans = 1
t = dif[0]
i = 0
while i < N:
    if dif[i] == 0:
        i += 1
        continue
    if dif[i]*t < 0:
        ans += 1
        t = dif[i]
    i += 1
from math import ceil
print(ceil(ans/2)+1)

Submission status: Wrong Answer

Solution code:
n = input()
s = list(map(int, input().split()))
cnt = 0
before = 0
for num in s:
  if(before > num):
    cnt += 1
  before = num
print(cnt)

Submission status: Wrong Answer

Solution code:
n = input()
s = list(map(int, input().split()))
cnt = 0
before = 0
for num in range(len(s)):
  if(before > num):
    cnt += 1
  before = num
print(cnt)

Submission status: Wrong Answer

Solution code:
n = input()
s = list(map(int, input().split()))
cnt = 0
before = 0
for num in range(1, len(s)):
  if(before > num):
    cnt += 1
  before = num
print(cnt)

Submission status: Wrong Answer

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

up = True
down = True
Pass = False
c = 0
for i in range(N-1):
    if Pass:
        if i == N-2:
            c += 1
        Pass = False
        continue
    if up and A[i] > A[i+1]:
        c += 1
        if i == N-2:
            break
        if A[i+1] < A[i+2]:
            Pass = True
            up = True
            down = False
        else:
            up = False
            down = True
    elif down and A[i] < A[i+1]:
        c += 1
        if i == N-2:
            break
        if A[i+1] > A[i+2]:
            Pass = True
            up = False
            down = True
        else:
            up = True
            down = False
if up and down:
    c += 1
print(c)

Submission status: Wrong Answer

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

up = True
down = True
Pass = False
c = 0
for i in range(N-1):
    if Pass:
        if i == N-2:
            c += 1
        Pass = False
        continue
    if up and A[i] > A[i+1]:
        c += 1
        if i == N-2:
            break
        if A[i+1] < A[i+2]:
            Pass = True
            up = True
            down = False
        else:
            up = False
            down = True
    elif down and A[i] < A[i+1]:
        c += 1
        if i == N-2:
            break
        if A[i+1] > A[i+2]:
            Pass = True
            up = False
            down = True
        else:
            up = True
            down = False
print(c)

Submission status: Wrong Answer

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

import sys

sys.setrecursionlimit(10**7)


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


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


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

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

    cnt = 1

    i = 1
    while i <= len(A) - 2:
        if (A[i - 1] < A[i] and A[i] > A[i + 1]) or (A[i - 1] > A[i] and A[i] < A[i + 1]):
            #  print(A[i - 1:i + 2])
            cnt += 1
            i += 2
        else:
            i += 1

    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

state=0
ans=1
prev = A[0]
for a in A:
  if state<0 and a>prev:
    ans += 1
  elif state>0 and a<prev:
    ans += 1
  if a>prev:
    state = 1
  elif a==prev:
    state = 0
  else:
    state = -1
  prev = a
  
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
num_list = [ int(v) for v in input().split() ]
mode = 1
ans = 0
for i in range(n-1):
    if mode == 1 and num_list[i+1] < num_list[i]:
        mode = -1
        ans += 1
    elif mode == -1 and num_list[i+1] > num_list[i]:
        mode = 1
    print(mode)
    
  
print(ans+1)

Submission status: Wrong Answer

Solution code:
n = int(input())
num_list = [ int(v) for v in input().split() ]
mode = 1
ans = 0
for i in range(n-1):
    if mode == 1 and num_list[i+1] < num_list[i]:
        mode = -1
        ans += 1
    elif mode == -1 and num_list[i+1] > num_list[i]:
        mode = 1
        ans += 1
    
  
print(ans+1)

Submission status: Wrong Answer

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

flag=0
 
def keisan(i,j): 
  if i<j:
    flag=0
  else:
    flag=1
  return flag  

ans=1
for i in range(len(a)-1):
  if a[i]==a[i+1]:
    break
  r_flag=keisan(a[i],a[i+1])
  if r_flag != flag:
    ans+=1


print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int,input().split()))
if n <= 2:
    print(1)
    exit()
B = [A[0]]
for i in range(1,n):
    if A[i] != A[i-1]:
        B.append(A[i])
n = len(B)
B.append(B[-1])
B.append(B[-1])
i = 0 
direction = (A[0]<A[1])
res = 1

while i<n:
    i += 1
    if B[i] > B[i+1] and direction:
        res += 1
        direction = (B[i+1]<B[i+2])
    elif B[i] < B[i+1] and not direction:
        res += 1
        direction = (B[i+1]<B[i+2])
print(res)

Submission status: Wrong Answer

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

result = 0
increase = None
previous = 0

for current in a:
    if increase is None:
        if previous == 0:
            previous = current
            continue
        increase = True if previous < current else False
    
    if (increase and previous > current) or (not increase and previous < current):
        result += 1
        increase = False if increase else False
    previous = current

result += 1
print(result)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int,input().split()))
A.append(A[-1])
i = 0 
direction = 0
res = 1
if A[0]<=A[1]:
    direction = 1

while i<n:
    i += 1
    
    if direction:
        if A[i]>A[i-1]:
            res += 1
            direction = 0
            i += 1
    else:
        if A[i]<A[i-1]:
            res += 1
            direction = 1
            i +=1
print(res)

Submission status: Wrong Answer

Solution code:
n = input();
n = int(n);
a = input().rstrip('\n')
a = a.split(" ");
a.append(a[n-1]);
state = int(a[1]) - int(a[0]);
ans = 1;
for i in range(2,n) :
	if int(a[i]) - int(a[i-1]) > 0 and state < 0 :
		ans = ans + 1;
		state = int(a[i+1]) - int(a[i]);
	elif int(a[i]) - int(a[i-1]) < 0 and state > 0 :
		ans = ans + 1;
		state = int(a[i+1]) - int(a[i]);
print(ans);

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
aArray = [int(x) for x in input().split()]

incFrag = False
decFrag = False

ans = 1
for i in range(0,n - 1):
    if aArray[i + 1] > aArray[i]:
        if decFrag:
            ans += 1
            decFrag = False
        else:
            incFrag = True
    else:
        if incFrag:
            ans += 1
            incFrag = False
        else:
            decFrag = True
print(ans)

# print(*data, sep='\n')

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split())) + ["a"]
cnt = 0
cur = 0
while len(a) > 2:
  l,r = 0,1
  while a[r] != "a" and a[r] == a[l]:
    r += 1
  if a[r] == "a":
    print(cnt+1)
    exit()
  d = a[r]-a[l]
  while a[r+1] != "a" and (a[r+1]-a[r])*d>=0:
    r += 1
  cnt += 1
  d = 0
  a = a[r+1:]
print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
ans = 1
init = False
for i in range(N-1):
    if A[i] == A[i+1]:
        continue
    if not init:
        inc = True if A[0] < A[1] else False
        init = True
    if A[i] < A[i+1]:
        if not inc:
            ans += 1
        inc = True
    else:
        if inc:
            ans += 1
        inc = False

print(ans)

Submission status: Wrong Answer

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

cnt = 1
flag = True
num = [0,flag]
for i in range(N-1):
    if flag:
        if a[i] > a[i+1]:
            if i > num[0] + 1 or flag != num[1]:
                cnt += 1
                num = [i,flag]
            flag = False
            #print(i, flag, num)
    elif not flag:
        if a[i] < a[i+1]:
            if i > num[0] + 1 or flag == num[1]:
                cnt += 1
                num = [i,flag]
            flag = True
            #print(i, flag, num)
print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=1
t=0
pre=0
for i in input().split():
    a=int(i)
    if pre==0:
        pre=a
    else:
        if pre>a:
            if -t<0 and t**2==1:
                ans+=1
                t=-2
            else:
                t=-1
        elif pre<a:
            if t<0 and t**2==1:
                ans+=1
                t=2
            else:
                t=1
        else:
            continue
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
before=0
state='minus'
ans=0

for a in A:
  if a==before:
    continue
  elif a-before>0:
    if state=='minus':
      ans+=1
      state='None'
    elif state=='None':
      state='plus'
  else:
    if state=='plus':
      state='None'
      ans+=1
    if state=='None':
      state='minus'
  before=a
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
before=0
state=0
ans=0
for a in A:
  
  if a==before:
    continue
  elif a-before>0:
    if state==0:
      state=1
      ans+=1
  else:
    if state==1:
      state=0
      ans+=1
  before=a
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
i = 0
c = 0
while i+2 <= n-1:
  if (a[i]-a[i+1])*(a[i+1]-a[i+2])<0:
    c += 1
    i += 1
  i += 1
print(c+1)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = [0] * N  # B[i]はA[i]がA[i-1]に比べて増加したかを表す
for i in range(1, N):
	if A[i] > A[i - 1]:
		B[i] = "+"
	elif A[i] < A[i - 1]:
		B[i] = "-"
ans = 1
now = "+"
for j in range(1, N):
	if now == "+" and B[j] == "-":
		ans += 1
		if j != N - 1:
			now = B[j + 1]
	elif now == "-" and B[j] == "+":
		ans += 1
		if j != N - 1:
			now = B[j + 1]
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

prev = a[0]
result = 0
up_down = None
for v in a[1:]:
    if up_down is None:
        if prev > v:
            up_down = lambda a, b: a > b
        elif prev < v:
            up_down = lambda a, b: a < b
        else:
            continue
    if not up_down(prev, v):
        up_down = None
        result += 1
    prev = v
print(result + 1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
ans1 = 1
m = False
for i in range(n - 1):
  if a[i] == a[i + 1]:
    pass
  elif a[i] > a[i + 1] ^ m:
    ans1 += 1
    m ^= True
ans2 = 1
m = True
for i in range(n - 1):
  if a[i] == a[i + 1]:
    pass
  elif a[i] < a[i + 1] ^ m:
    ans2 += 1
    m ^= True
print(min(ans1, ans2))

Submission status: Wrong Answer

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

a = sum([int(x>y) for x,y in zip([0]+A,A+[10**10])])
b = sum([int(x<y) for x,y in zip([10**10]+A,A+[0])])
print(min(a,b))

Submission status: Wrong Answer

Solution code:
from itertools import*
n,*a=map(int,open(0).read().split())
a=[k for k,v in groupby(a)]
#print(a)
a=[1 if a[i+1]-a[i]>0 else -1 for i in range(len(a)-1)]
c=1
tmp=0
for i in [k for k,v in groupby(a)]:
    tmp+=i
    if tmp==0:
        c+=1
print(c+(tmp!=0))

Submission status: Wrong Answer

Solution code:
from itertools import*
n,*a=map(int,open(0).read().split())
a=[k for k,v in groupby(a)]
#print(a)
a=[1 if a[i+1]-a[i]>0 else -1 for i in range(len(a)-1)]
#print(a)
b=len([k for k,v in groupby(a) if len(list(v))==1 and k==-1]) #-1が一個の数
#print(b)
c=[k for k,v in groupby(a)]
#print(c)
#+だけ圧縮-はその塊の長さ-1で圧縮
print(len(c)-b+(a[-2:]==[1,-1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

#AN[i]-AN[i-1]をsubsに入れる
subs = [0]
for beforeA,A in zip(AN[0:N-1],AN[1:N]):
	subs.append(A-beforeA)

#print(subs)

subLen = len(subs)
chg = True
ans = 1
for beforeSub,sub in zip(subs[0:subLen-1],subs[1:subLen]):		#
	if chg:
		chg=False
		continue

	if sub * beforeSub < 0:					#-1,1 or 1,-1なら区切る
		chg = True
		ans+=1
print(ans)