Problem:


Score : 300 points

Problem Statement

You are given an array A of length N.
Your task is to divide it into several contiguous subarrays.
Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order.
At least how many subarrays do you need to divide A into?

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Each A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible number of subarrays after division of A.

Sample Input 1

6
1 2 3 2 2 1

Sample Output 1

2

One optimal solution is to divide the array into [1,2,3] and [2,2,1].

Sample Input 2

9
1 2 1 2 1 2 1 2 1

Sample Output 2

5

Sample Input 3

7
1 2 3 2 1 999999999 1000000000

Sample Output 3

3



Sample input:
6
1 2 3 2 2 1


Sample output:
2

Submission status: Accepted

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

ans=1
x=y=0
c=A[0]

for i in range(1,N):
    if c < A[i]:
        x=1
    elif c>A[i]:
        y=1
    
    if x==1 and y==1:
        ans+=1
        x=y=0
    
    c=A[i]

print(ans)

Submission status: Accepted

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

cnt = 1
first = True
increasing = True

for i in range(1, n):
    if (first):
        if (a[i] > a[i-1]):
            first = False
            increasing = True
        elif (a[i] < a[i-1]):
            first = False
            increasing = False
    else:
        if (a[i] > a[i-1]) and (increasing == False):
            cnt += 1
            first = True
        elif (a[i] < a[i-1]) and (increasing == True):
            cnt += 1
            first = True

print(cnt)

Submission status: Accepted

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

uf,df = True,True
x = a[0]
for ai in a:
    
    if x > ai:
        uf = False
    if x < ai:
        df = False
    if (uf or df) is False:
        ans += 1
        uf,df, = True,True
    x = ai
print(ans+1)

Submission status: Accepted

Solution code:
def solve():
  ans = 1
  N = int(input())
  A = list(map(int, input().split()))
  ud = 0
  for i in range(N-1):
    a = A[i+1]-A[i]
    if a*ud<0:
      ans += 1
      ud = 0
    elif ud==0:
      ud = a
  return ans
print(solve())

Submission status: Accepted

Solution code:
def check(a,b):
  if a<b:
    return 'up'
  if a>b:
    return 'down'
  return ''

def solve():
  ans = 1
  N = int(input())
  A = list(map(int, input().split()))
  ud = ''
  for i in range(N-1):
    a = check(A[i],A[i+1])
    if ud=='' or ud==a:
      ud = a
      continue
    if a=='':
      continue
    ans += 1
    ud = ''
  return ans
print(solve())

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
is_up=False
is_even=True
pre_a=a[0]
cnt=1
for aa in a:
  if is_even and pre_a != aa:
    is_even=False
    is_up=pre_a < aa
  if not is_even:
    if (is_up and pre_a > aa) or (not is_up and pre_a < aa):
      is_even=True
      cnt+=1
  pre_a=aa
print(cnt)

Submission status: Accepted

Solution code:
N,*A = map(int, open(0).read().split())
ans = 1
if N <= 2:
    print(1)
else:
    if A[1] > A[0]:
        dif = 1
    elif A[1] < A[0]:
        dif = -1
    else:
        dif = 0
    for i in range(2,N):
        if A[i] > A[i-1]:
            if dif == -1:
                ans += 1
                dif = 0
            elif dif == 0:
                dif = 1
        elif A[i] < A[i-1]:
            if dif == 1:
                ans += 1
                dif = 0
            elif dif == 0:
                dif = -1
    print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
counter=1
res=0
for i in range(len(A)-1):
    if (A[i+1]<A[i] and res>0) or (A[i+1]>A[i] and res<0):
        counter+=1
        res=0
    elif A[i+1]!=A[i]:
        res=A[i+1]-A[i]


print(counter)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000)
N = int(input())
A = list(map(int, input().split()))

ans = 0
def main(i, A):
  global N
  global ans
  if A[i] == A[i+1]:
    if i == N-2:
      ans += 1
      return ans
    else:
      return main(i+1, A)
  elif A[i] < A[i+1]:
    while i < N-1:
      if A[i] <= A[i+1]:
        i += 1
      else:
        ans += 1
        if i == N-2:
          ans += 1
          return ans
        else:
          return main(i+1, A)
    else:
      ans += 1
      return ans
  else:
    while i < N-1:
      if A[i] >= A[i+1]:
        i += 1
      else:
        ans += 1
        if i == N-2:
          ans += 1
          return ans
        else:
          return main(i+1, A)
    else:
      ans += 1
      return ans
    
if N == 1:
  print(1)
else:
  print(main(0, A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
nb = 0
da = 0
for i in range(len(A)-1):
    if A[i] > A[i+1] and da > 0:
        nb += 1
        da = 0
    elif A[i] < A[i+1] and da < 0:
        nb += 1
        da = 0
    else:
        if A[i+1] - A[i] != 0:
            da = A[i+1] - A[i]
print(nb + 1)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = []
flg = 'hi'
for i in range(n-1):
    if a[i] > a[i+1]:
        if flg != 'pl':
            flg = 'mi'
        else:
            b.append(i)
            flg = 'hi'
    elif a[i] < a[i+1]:
        if flg != 'mi':
            flg = 'pl'
        else:
            b.append(i)
            flg = 'hi'
print(len(b)+1)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
zougen = 2
ans = 0

for i in range(1,n):
    if i == n-1:
        if zougen == 2:
            ans += 1
        elif zougen == 0:
            if a[i]-a[i-1]<0:
                ans += 2
            else:
                ans += 1
        else:
            if a[i]-a[i-1] > 0:
                ans += 2
            else:
                ans += 1
    elif zougen == 2:
        if a[i]-a[i-1]>0:
            zougen = 0
        elif a[i]-a[i-1]<0:
            zougen = 1
        else:
            zougen = 2
    elif zougen == 0:
        if a[i]-a[i-1]<0:
            zougen = 2
            ans += 1
    elif zougen == 1:
        if a[i]-a[i-1]>0:
            zougen = 2
            ans += 1

if n == 1:
    ans = 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

#最小の分割数をdpで考える。
dp = (1, 1)#増減　増加
for i in range(1, N):
    if A[i] > A[i-1]:#大きいときは右側更新
        dp = min(dp)+1, min(dp[0]+1, dp[1])
    elif A[i] == A[i-1]:
        pass
    else:#小さいときは左側
        dp = min(dp[0], dp[1]+1), min(dp)+1
    #print(dp)
print(min(dp))

Submission status: Accepted

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

n = int(input())
a = list(map(int, input().split()))
d = [0 for _ in range(n-1)]
for i in range(n-1):
    d[i] = a[i+1]-a[i]

# print(d)
ans = 1 
state = 'flat'
for i in range(n-1):
    if i == 0:
        if d[i] > 0:
            state = 'up'    
        elif d[i] < 0:
            state = 'down'

    else:
        if d[i] > 0:
            if state == 'flat':
                state = 'up'
            elif state == 'down':
                state = 'flat'
                ans += 1

        elif d[i] < 0:
            if state == 'flat':
                state = 'down'
            elif state == 'up':
                state = 'flat'
                ans += 1

print(ans)

Submission status: Accepted

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

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

f = 0
cnt = 0
if a[0] < a[1]:
    f = 1
elif a[0] > a[1]:
    f = 2
tmp = a[1]

for i in range(2,n):
    if tmp < a[i]:
        if f == 2:
            cnt += 1
            f = 0
        elif f == 0:
            f = 1
    elif tmp > a[i]:
        if f == 1:
            cnt += 1
            f = 0
        elif f == 0:
            f = 2
    tmp = a[i]
cnt += 1

print(cnt)

Submission status: Accepted

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

n = int(input())
a = list(map(int, input().split()))
d = [0 for _ in range(n-1)]
for i in range(n-1):
    d[i] = a[i+1]-a[i]

ans = 1 
flg = [False, False]

for i in range(n-1):
    if d[i] > 0:
        flg[0] = True
    elif d[i] < 0:
        flg[1] = True

    if flg[0] == flg[1] == True:
        ans += 1
        flg = [False, False]

print(ans)

Submission status: Accepted

Solution code:
# 58
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')

n = I()
a = LI()
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: Accepted

Solution code:
N = int(input())
L = list(map(int, input().split()))
if N == 1 or N == 2:
  print(1)
else:
  pre = L[0]
  greater = False
  less = False
  ans = 1
  for l in L[1:]:
    if greater:
      if pre > l:
        ans += 1
        greater = False
    elif less:
      if pre < l:
        ans += 1
        less = False
    else:
      if pre < l:
        greater = True
      elif pre > l:
        less = True
    pre = l
    
  print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    i = 0
    while i < n:
        while i+1<n and a[i]==a[i+1]:
            i += 1
        if i+1<n and a[i] < a[i+1]:
            while i+1<n and a[i]<=a[i+1]:
                i += 1
        elif i+1<n and a[i] > a[i+1]:
            while i+1<n and a[i]>=a[i+1]:
                i += 1
        ans += 1
        i += 1
    print(ans)
            
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, *a = map(int, open(0).read().split())
c = 1
d = [1] * n
a = [a[0]] + [a[i] for i in range(1, n) if a[i - 1] != a[i]]
for i in range(2, len(a)):
    if (a[i] - a[i - 1]) * (a[i - 1] - a[i - 2]) < 0 and d[i - 1]:
        c += 1
        d[i] = 0
print(c)

Submission status: Accepted

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

ans = 1
flg = -1
for i in range(1, n):
    if a[i-1] == a[i]:
        continue
    
    elif a[i-1] < a[i]:
        if flg == 1 or flg == -1:
            flg = 1
            continue
        else:
            ans += 1
            flg = -1

    else:
        if flg == 0 or flg == -1:
            flg = 0
            continue
        else:
            ans += 1
            flg = -1

print(ans)

Submission status: Accepted

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

# 最小の答えは1、ここに足していく
ans = 1

# 増加と減少切り替わったタイミングで組の数を増やす
flags = [False, False]

for i in range(N - 1):
    da = a[i + 1] - a[i]
    if da > 0:
        flags[0] = True
    elif da < 0:
        flags[1] = True

    if flags[0] == True and flags[1] == True:
        ans += 1
        # 初期化
        flags = [False, False]

print(ans)

Submission status: Accepted

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

if n<=2:
    print(1)
else:
    status="U"
    for i in range(n):
        if a[i+1]-a[i]<0:
            status="D"
            break
        elif a[i+1]-a[i]>0:
            status="U"
            break

    cnt=1
    reset_flag=False
    for i in range(n-1):
        if reset_flag:
            if a[i+1]-a[i]>0:
                status="U"
                reset_flag=False
            elif a[i+1]-a[i]<0:
                status="D"
                reset_flag=False
            else:
                continue
        elif status=="D" and a[i+1]-a[i]>0:
            cnt+=1
            reset_flag=True
        elif status=="U" and a[i+1]-a[i]<0:
            cnt+=1
            reset_flag=True
    print(cnt)

Submission status: Accepted

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

now = False
count = 1
for i in range(N-1):
    if A[i] < A[i+1]:
        if now == 1:
            pass
        elif now == -1:
            count += 1
            now = False
        else:
            now = 1
    elif A[i] > A[i+1]:
        if now == 1:
            count += 1
            now = False
        elif now == -1:
            pass
        else:
            now = -1

print(count)

Submission status: Accepted

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

ans = 1
flags=[False,False]

for i in range(N-1):
  da = a[i+1] - a[i]
  if da > 0:
    flags[0] = True
  elif da < 0:
    flags[1] = True
    
  if all(flags):
    ans += 1
    flags = [False,False]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
a = 0
S = 0
num = 1
for i in range(1,N):
    if A[i] - A[i - 1] == 0:
        a = 0
    elif A[i] - A[i - 1] > 0:
        a = 1
    else:
        a = 2
    if S == 0:
        S = a
    elif (S != a) and (a != 0):
        num += 1
        S = 0
print(num)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from collections import deque
def main():
    n, *a = map(int, read().split())
    a = deque(a)
    r = 1
    flag = False
    muki = 0
    n1 = a.popleft()
    while a:
        n2 = a.popleft()
        if flag:
            if n1 < n2 and muki:
                n1 = n2
            elif n1 > n2 and not muki:
                n1 = n2
            elif n1 == n2:
                pass
            else:
                r += 1
                flag = False
                n1 = n2
        else:
            if n1 < n2:
                flag = True
                muki = 1
                n1 = n2
            elif n1 > n2:
                flag = True
                muki = 0
                n1 = n2
    print(r)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

F=0
K=1
b=A[0]

for a in A:
    if F==0:
        if b<a:
            F=1
            b=a
        elif b>a:
            F=-1
            b=a
    elif F==1 and b>a:
        K+=1
        F=0
    elif F==-1 and b<a:
        K+=1
        F=0
    b=a
print(K)

Submission status: Accepted

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

Submission status: Accepted

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

i = 0
c = 0
while i < n:
    if i == n - 1:
        c += 1
        break
    if a[i] == a[i+1]:
        i += 1
    elif a[i] < a[i+1]:
        while i < n - 1 and a[i] <= a[i+1]:
            i += 1
        c += 1
        i += 1
    elif a[i] > a[i+1]:
        while i < n - 1 and a[i] >= a[i+1]:
            i += 1
        c += 1
        i += 1
print(c)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = int(readline())
    a = list(map(int, readline().split()))

    i = 0
    ans = 0

    while i < n:
        s = a[i]
        j = i
        k = i
        while j < n and s <= a[j]:
            s = a[j]
            j += 1
        while k < n and s >= a[k]:
            s = a[k]
            k += 1
        i = max(j, k)
        ans += 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
N = int(input())
A = list(map(int, input().split()))
i = 0
cnt = 0
while i < N:
    while i < N - 1 and A[i + 1] == A[i]:
        i += 1
    if i >= N - 1:
        cnt += 1
        print(cnt)
        sys.exit()
    if A[i + 1] < A[i]:
        while i < N - 1 and A[i + 1] <= A[i]:
            i += 1
    else:
        while i < N - 1 and A[i + 1] >= A[i]:
            i += 1
    cnt += 1
    i += 1
print(cnt)

Submission status: Accepted

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

def linput(ty=int, cvt=list):
	return cvt(map(ty,input().split()))

def vinput(rep=1, ty=int, cvt=list):
	return cvt(ty(input().rstrip()) for _ in "*"*rep)

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

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

def main():
	N = int(input())
	vA = linput()
	#vA.sort()
	#vA.reverse()
			
	vC =[a-pp for a,pp in zip(vA[1:],vA)
		 if a!=pp]
		
	res = 0
	pp = 0

	for c in vC:
		if pp==0:
			pp = 1 if c>0 else -1
			res += 1
		if c*pp<0:
			pp = 0
	
	if pp==0:
		res += 1
	#sT = "No Yes".split()
	#print(sT[res])
	print(res)

if __name__ == "__main__":
	main()

Submission status: Accepted

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

def linput(ty=int, cvt=list):
	return cvt(map(ty,input().split()))

def vinput(rep=1, ty=int, cvt=list):
	return cvt(ty(input().rstrip()) for _ in "*"*rep)

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

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

def main():
	N = int(input())
	vA = linput()
	#vA.sort()
	#vA.reverse()
	
	if N==1:
		print(1)
		return
		
	vC =[a-pp for a,pp in zip(vA[1:],vA)]
		
	res = 0
	pp = 0
	cnt = 0
	for c in vC:
		if c==0: continue
		if pp==0:
			pp = 1 if c>0 else -1
			cnt = 1
			res += 1
			continue
		if c>0 and pp<0:
			cnt = 1
			pp = 0
			continue
		if c<0 and pp>0:
			cnt = 1
			pp = 0
			continue
		cnt += 1
	
	if pp==0:
		res += 1
	#sT = "No Yes".split()
	#print(sT[res])
	print(res)

if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
A = list(map(int, input().split()))


res = 0
i=0

while(i<N):    
    #same
    while(i+1 < N and A[i] == A[i+1]):
        i+=1
    
    #up
    if(i+1 < N and A[i] < A[i+1]):
        while(i+1 < N and A[i] <= A[i+1]):
            i+=1
    
    #down
    elif(i+1 < N and A[i] > A[i+1]):
        while(i+1 < N and A[i] >= A[i+1]):
            i+=1

    res+=1
    i+=1
   


print(res)

Submission status: Accepted

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

N = int(input())
A = list(map(int,input().split()))
B = []
for i in range(1,N):
    if A[i] - A[i-1] != 0:
        B.append(A[i] - A[i-1])
cnt = 0
for i in range(1,len(B)):
    if i >1:
        if B[i-1]*B[i-2] < 0 :
            continue
    if B[i]*B[i-1]<0:
        cnt += 1
        B[i]=0
        
print(cnt+1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
 
## Library
 
import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
import copy
 
## input
 
# N=int(input())
# A,B,C,D=map(int, input().split())
# S = input()
# yoko = list(map(int, input().split()))
# tate = [int(input()) for _ in range(N)]
# N, M = map(int,input().split()) 
#    P = [list(map(int,input().split())) for i in range(M)]
#    S = []
#    for _ in range(N):
#        S.append(list(input()))
 
N=int(input())
A=list(map(int, input().split()))

ans = 1
state = "normal"

for i in range(1,N):
    if state == "normal":
        if A[i-1] == A[i]:
            continue
        elif A[i-1] < A[i]:
            state = "up"
        else:
            state = "down"
    elif state == "up":
        if A[i-1] > A[i]:
            state = "normal"
            ans += 1
    elif state == "down":
        if A[i-1] < A[i]:
            state = "normal"
            ans += 1 


print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')


def solve():
    n = int(input())
    a = [int(x) for x in input().split()]
    b = []
    p = 0
    for e in a:
        if p != e:
            b.append(e)
        p = e
    a = b
    n = len(b)

    ans = 0
    i = 0
    while i < n:
        ans += 1
        if i + 1 == n: break
        else:
            if a[i] < a[i+1]:
                while i + 1 < n and a[i] < a[i+1]:
                    i += 1
                i += 1
            else:
                while i + 1 < n and a[i] > a[i+1]:
                    i += 1
                i += 1
    print(ans)



    

t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""



"""

Submission status: Accepted

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

i = 1
ans = 1
flg = 0
for i, j in zip(A[:-1],A[1:]):
    if i < j:
        if flg == 0:
            flg = 1
        elif flg == 2:
            ans += 1
            flg = 0
    if i > j:
        if flg == 0:
            flg = 2
        elif flg == 1:
            ans += 1
            flg = 0
print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/agc013/tasks/agc013_a
# 単調性を愚直に調べれば良い #と思ったけどこれはめんどいぞ
from itertools import groupby
N = int(input())
A = list(map(int, input().split()))
B = []
for key, v in groupby(A):
    B.append(key)

# /\か\/となる個数を見つければ良い？違う
diff = []
for i in range(len(B) - 1):
    diff.append(1 if B[i] < B[i + 1] else -1)

# 初めて符号が変わった瞬間のものは考慮しない
nums = []
for d, v in groupby(diff):
    nums.append(len(list(v)))

# print(nums)
is_can_del = 0
ans = 0
for n in nums:
    if n == 1 and is_can_del == 1:
        is_can_del = 0
    else:
        ans += 1
        is_can_del = 1
if is_can_del == 0:
    ans += 1
print(ans)
# diff = []
# for i in range(len(A) - 1):
#     if A[i] == A[i + 1]:
#         # diff.append(0)
#         pass
#     elif A[i] < A[i + 1]:
#         diff.append(1)
#     else:
#         diff.append(-1)
# print(diff)
# print(list(groupby(diff)))


# 前処理として増加なのか減少なのかを記録

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
a = list(map(int, input().split()))

ans = 1
f = 0

for i in range(n):
    if f == 0:
        f = 1
    elif f == 1:
        if a[i] > a[i - 1]:
            f = 2
        elif a[i] < a[i - 1]:
            f = 3
    elif f == 2:
        if a[i] < a[i - 1]:
            ans += 1
            f = 1
    elif f == 3:
        if a[i] > a[i - 1]:
            ans += 1
            f = 1
print(ans)

Submission status: Accepted

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

ans = 1
flg = 0
rev = 0 # 0:単調非増加、1:単調非減少
for c,n in zip(A, A[1:]):
    
    if not flg:
        if c < n:
            # 単調非増加を仮定する
            flg = 1
        elif c > n:
            # 単調非減少を仮定する
            flg = 1
            rev = 1
    else:
        if rev:
            c = -c
            n = -n

        if c > n:
            ans += 1
            flg = 0
            rev = 0
            
print(ans)

Submission status: Accepted

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

#%%
flag = None
up = False
down = False
ans = 1
base = A[0]

for i in range(1, n):
    if base < A[i]:
        up = True
        base = A[i]
    elif base > A[i]:
        down = True
    if (up == True) and (down == True):
        up = False
        down = False
        ans += 1
    base = A[i]
    
print(ans)

Submission status: Accepted

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

flag=0 #増加で継続する場合1,減少で継続する場合-1,切り替わり時0
cnt=0

for i in range(N-1):
    if A[i+1]-A[i] >0:
        if flag ==1:
            continue
        elif flag ==-1:
            flag =0
            cnt+=1
        elif flag ==0:
            flag =1
    elif A[i+1]-A[i] <0:
        if flag ==-1:
            continue
        elif flag ==1:
            flag =0
            cnt+=1
        elif flag ==0:
            flag =-1
    elif A[i+1]-A[i] ==0:
        if flag == 0:
            flag = 0
        elif flag ==1:
            flag =1
        elif flag ==-1:
            flag =-1
print(cnt+1) #区切った数ではなく分割された個数なので+1

Submission status: Accepted

Solution code:
n = int(input())
a_l = list(map(int, input().split()))
ans = 1
is_plus = None
sub = []
for i in a_l:
    if len(sub) == 0:
        sub.append(i)
        continue
    if i == sub[-1]:
        sub.append(i)
        continue

    if is_plus is None:
        if sub[-1] > i:
            is_plus = False
        else:
            is_plus = True
        sub.append(i)

    elif is_plus is True:
        if sub[-1] > i:
            sub = [i]
            ans += 1
            is_plus = None
        else:
            sub.append(i)
    else:
        if sub[-1] < i:
            sub = [i]
            ans += 1
            is_plus = None
        else:
            sub.append(i)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
aset=set(a)
ans=1
if len(aset)>1:
    num1=a[0]
    num2=a[1]
    num3=2 #Ai<Ai+1 1  Ai>Ai+1 0
    if num1<num2:
        num3=1
    if num1>num2:
        num3=0
    for i in range(len(a)-2):
        num=a[2+i]
        if num3==2 and num2>num:
            num3=0
        if num3==2 and num2<num:
            num3=1
        if num3==0 and num2<num:
            ans+=1
            num3=2
        if num3==1 and num2>num:
            ans+=1
            num3=2
        num2=num
print(ans)

Submission status: Accepted

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


p = 0
res = 0

while p < N:
    while p + 1 < N and A[p] == A[p + 1]:
        p += 1
    if p + 1 < N and A[p] < A[p + 1]:
        while p + 1 < N and A[p] <= A[p + 1]:
            p += 1
    elif p + 1 < N and A[p] > A[p + 1]:
        while p + 1 < N and A[p] >= A[p + 1]:
            p += 1
    res += 1
    p += 1
print(res)

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
a = list(map(int, input().split()))
l = []
prev = None
for i in range(n):
    if prev!=a[i]:
        l.append(a[i])
    prev = a[i]
        
if len(l)==1:
    ans = 1
else:
    m = len(l)
    prev = None
    ans = 0
    for i in range(m-1):
        s = l[i]<l[i+1]
        if prev is None:
            prev = s
        elif prev!=s:
            ans += 1
            prev = None
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
ans=1
vec=0
for i in range(n-1):
    now=a[i+1]-a[i]
    if vec*now<0:
        ans+=1
        vec=0
    elif vec==0:
        vec=now
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

def f():
    for i in range(1, N):
        if A[i-1] < A[i]:
            state = 0
            break
        if A[i-1] > A[i]:
            state = 1
            break
    else:
        print(1)
        return

    cnt = 1

    for i in range(1, N):
        if A[i-1] < A[i]:
            if state == 1:
                state = 2
                cnt += 1
            else:
                state = 0

        if A[i-1] > A[i]:
            if state == 0:
                state = 2
                cnt += 1
            else:
                state = 1
    
    print(cnt)

f()

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
n = int(input())
a = list(map(int, input().split()))
ans = 1
# 増加or減少
check = 0
before = a[0]
for i in a[1:]:
    if i == before:
        continue
    if before > i:
        if check == 0:
            check = 1
        elif check == -1:
            ans += 1
            check = 0
    else:
        if check == 0:
            check = -1
        elif check == 1:
            ans += 1
            check = 0
    before = i

print(ans)

Submission status: Accepted

Solution code:
from math import ceil,floor,comb,factorial,gcd,pow,sqrt,log2,cos,sin,tan,pi,inf
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heappop,heappush
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
a = LIST()

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

if inv == 1:
    ans += 1
    
print(ans)

Submission status: Accepted

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

if N == 1:
  print(1)
else:
  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)