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:
# coding: utf-8
# Here your code



N = input()
a = [int(i) for i in input().split()] 
result = 0
first_flag = 1

for i in range(1,len(a)):
    if first_flag == 1:
        if a[i-1] > a[i]:
            first_flag = 2
        elif a[i-1] < a[i]:
            first_flag = 3
    elif first_flag == 2:
        if a[i-1] < a[i]:
            result += 1
            first_flag = 1
    elif first_flag == 3:
        if a[i-1] > a[i]:
            result += 1
            first_flag = 1    

print(result+1)

Submission status: Accepted

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

d=False#単調減少
u=False#単調増加

cnt=0

for i in range(0,n-1):
  l=a[i];r=a[i+1]
  if l<r:u=True
  elif l>r:d=True
    
  if u and d:
    cnt+=1
    u=False;d=False
print(cnt+1)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
ret = 1
up, down = False, False
for i in range(N - 1):
    if A[i] < A[i + 1]:
        up = True
    elif A[i] > A[i + 1]:
        down = True
    if up == down == True:
        ret += 1
        up, down = False, False
print(ret)

Submission status: Accepted

Solution code:
n = int(input())
line = list(map(int,input().split()))
count = 1
up = False
down =False
for i in range(n - 1):
  if line[i] < line[i + 1]:
    up = True
  elif line[i] > line[i + 1]:
    down = True
  if up == down == True:
    count += 1
    up =False
    down =False
print(count)

Submission status: Accepted

Solution code:

n = int(input())
a = list(map(int, input().split()))
#print(a)
di = ""
count = 0
i = 0
while i != n - 1:
    if a[i] == a[i+1] and di == "":
        di = ""
    elif a[i] == a[i+1]:
        di = di #そのまま

    if a[i] < a[i+1] and di == "":
        di = "増加"
    if a[i] > a[i+1] and di == "":
        di = "減少"

    if di == "増加" and a[i] > a[i+1]:
        count += 1
        di = ""
    if di == "減少" and a[i] < a[i+1]:
        count += 1
        di = ""

    i += 1
print(count + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
flag = None
c = 1 
def check(i):
        for j in range(i, N - 1): 
                if A[j] - A[j + 1] < 0:
                        return 1 
                elif A[j] - A[j + 1] > 0:
                        return -1
flag = check(0)
for i in range(N - 1): 
        if flag == None:
                break
        elif flag == 1:
                if A[i] - A[i + 1] > 0:
                        c += 1
                        flag = check(i + 1)
        elif flag == -1: 
                if A[i] - A[i + 1] < 0:
                        c += 1
                        flag = check(i + 1)
print(c)

Submission status: Accepted

Solution code:
n=int(input())
s=list(map(int,input().split()))
cnt=1
u=False
d=False
for i in range(n-1):
    if s[i]<s[i+1]:
        u=True
    elif s[i]>s[i+1]:
        d=True
    if u and d:
        cnt+=1
        u=False
        d=False
print(cnt)

Submission status: Accepted

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

flag = 0  #unknown:0, increasing:1, decreasing:2
ans = 1
cur = a[0]

for i in a:
    if (flag == 1 and i < cur) or (flag == 2 and i > cur):
        ans += 1
        flag = 0
    elif flag != 1 and cur < i:
        flag = 1
    elif flag != 2 and cur > i:
        flag = 2
    cur = i
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:


def main()->None:
    # http://drken1215.hatenablog.com/entry/2019/03/28/014100
    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)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
B = [A[0]]
for i in range(1, N) :
  if A[i] != B[-1] :
    B.append(A[i])

N = len(B)

ret = 1
i = 2
while i < N :
  if (B[i] - B[i-1]) * (B[i-1] - B[i-2]) < 0 :
    ret += 1
    i += 1
  i += 1

print(ret)

Submission status: Accepted

Solution code:
n = int(input())

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

flag = 0
ans = 0

for i in range(1,n):
	#print(i,flag)
	
	if flag == 0:
		if l[i] > l[i-1]:
			flag = 1
		elif l[i] < l[i-1]:
			flag = -1
		else:
			pass
						
	elif flag == 1:
		if l[i] > l[i-1]:
			pass
		elif l[i] == l[i-1]:
			pass
		elif l[i] < l[i-1]:
			ans = ans + 1
			flag = 0
			
	elif flag == -1:
		if l[i] < l[i-1]:
			pass
		elif l[i] == l[i-1]:
			pass
		elif l[i] > l[i-1]:
			ans = ans + 1
			flag = 0		

print(ans+1)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

inc = 0
cnt = 0
for i in range(n-1):
    if inc == -1 and L[i] < L[i+1]:
        cnt += 1
        inc = 0
    elif inc == 1 and L[i] > L[i+1]:
        cnt += 1
        inc = 0
    elif inc == 0 and L[i] > L[i+1]:
        inc = -1
    elif inc == 0 and L[i] < L[i+1]:
        inc = 1
print(cnt+1)

Submission status: Accepted

Solution code:
# coding=utf-8
N=int(1e5+10)
n=input()
a=map(int,raw_input().split())
ans=1;lst=a[0];up=-1;
for i in range(1,len(a)):
    if up==-1:
        if(lst>a[i]): up=0
        if(lst<a[i]): up=1
    else:
        if(up==0 and lst<a[i]): ans=ans+1;up=-1
        if(up==1 and lst>a[i]): ans=ans+1;up=-1
    lst=a[i]
print ans

Submission status: Accepted

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

cnt=1
s=0
for i in range(1,len(a)):
 if a[i-1]!=a[i]:
  if s==0 and a[i-1]<a[i]:
   s=1
  elif s==0 and a[i-1]>a[i]:
   s=-1
  elif s==-1 and a[i-1]<a[i]:
   s=0
   cnt+=1
  elif s==1 and a[i-1]>a[i]:
   s=0
   cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
import numpy as np
count=0
i=0
while i < n:
  while i<n-1:
    if a[i+1]==a[i]:
      i+=1
    else:
      break
  #print(i)
  if i<n-1 and a[i+1]>a[i]:
    while i<n-1 and a[i+1]>=a[i]:
      i+=1
  elif i<n-1 and a[i+1]<a[i]:
    while i<n-1 and a[i+1]<=a[i]:
      i+=1
  count+=1
  i+=1
  #print(i)

print(count)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
i = 0

while i < n:
    if i == n - 1:
        ans += 1
        break

    # 増加
    plus = 0
    while i + plus + 1 < n:
        if a[i + plus] <= a[i + plus + 1]:
            plus += 1
        else:
            break

    # 減少
    minus = 0
    while i + minus+1 < n:
        if a[i + minus] >= a[i + minus+1]:
            minus += 1
        else:
            break

    ans += 1
    if plus >= minus:
        i += plus + 1
    else:
        i += minus + 1


print(ans)

Submission status: Accepted

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

mode = 0
ans = 0
for i in range(1, n):
    if a[i]-a[i-1] == 0:
        continue
    if a[i]-a[i-1] > 0:
        if mode == 0:
            mode = 1
        if mode == -1:
            ans += 1
            mode = 0
    if a[i]-a[i-1] < 0:
        if mode == 0:
            mode = -1
        if mode == 1:
            ans += 1
            mode = 0
print(ans+1)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
ans = 0
f = -1
for i in range(n):
  if i == n-1:
    ans += 1
  else:
    if A[i] != A[i+1]:
      if f == -1:
        f = A[i] < A[i+1]
      else:
        if f ^ (A[i] < A[i+1]):
          ans += 1
          f = -1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

before = A[0]
before_ = "="
count = 1
for idx, a in enumerate(A[1:]):
    if before == a:
        now = before_
    if before < a:
        now = "+"
    if before > a:
        now = "-"

    if (before_ == "+" and now == "-") or (before_ == "-" and now == "+"):
        count += 1
        before_ = "="
    else:
        before_ = now
    before = a
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

import numpy as np
import scipy

N = int(input())
#C = [[0 for i in range(3)] for j in range(3)]
#for i in range(3):
#    C[i] = list(map(int, input().split()))
#print(C)
A = list(map(int, input().split()))

split = 0
order = ""
for i in range(len(A)-1):
    if order == "up" and A[i] > A[i+1]:
        split += 1
        order = ""
    elif order == "down" and A[i] < A[i+1]:
        split += 1
        order = ""
    elif order == "":
        if A[i] < A[i+1]:
            order = "up"
        elif A[i] > A[i+1]:
            order = "down"

print(split+1)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
fp=A[0];ans=1;fp=fn=False
for i in range(1,N):
    if A[i-1]<A[i]:
        if not fp and not fn:
            fp=True
        elif fn:
            ans+=1
            fn=False
    if A[i-1]>A[i]:
        if not fp and not fn:
            fn=True
        elif fp:
            ans+=1
            fp=False

print(ans)

Submission status: Accepted

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

last = a[0]
direction = 0

ans = 1

for x in a[1:]:
    if direction == 0:
        if last < x:
            direction = 1
        elif last > x:
            direction = -1
        last = x
    elif direction == 1:
        if last > x:
            direction = 0
            ans += 1
        last = x
    elif direction == -1:
        if last < x:
            direction = 0
            ans += 1
        last = x

print(ans)

Submission status: Accepted

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

ans = 1
up = None

for x, y in zip(a, a[1:]):
    if x < y:
        if up is None:
            up = True
        elif not up:
            ans += 1
            up = None
    elif x > y:
        if up is None:
            up = False
        elif up:
            ans += 1
            up = None
print(ans)

Submission status: Accepted

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

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

dec, inc = False, False
cnt = 1

for i in range(n-1):
    if a[i+1] - a[i] < 0:
        dec = True
    elif a[i+1] - a[i] > 0:
        inc = True

    if dec and inc:
        cnt += 1
        dec, inc = False, False

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
ascend = True
for j in range(1,N):
    if A[j-1] < A[j]:
        break
    if A[j-1] > A[j]:
        ascend = False
        break
ans = 1
for i in range(1,N):
    if ascend:
        if A[i-1] > A[i]:
            ans += 1
            ascend = True
            for j in range(i+1,N):
                if A[j-1] < A[j]:
                    break
                if A[j-1] > A[j]:
                    ascend = False
                    break
    else:
        if A[i-1] < A[i]:
            ans += 1
            ascend = True
            for j in range(i+1,N):
                if A[j-1] < A[j]:
                    break
                if A[j-1] > A[j]:
                    ascend = False
                    break
print(ans)

Submission status: Accepted

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

cnt = 0
swich = ""

for i in range(n):
    if i == 0:
        s = a_li[i]
        continue
    
    if a_li[i] == s:
        continue
    
    if swich == "":
        if a_li[i] > s:
            swich = "+"
        else:
            swich = "-"
            
    elif a_li[i] > s:
        if swich == "+":
            pass
        else:
            cnt += 1
            swich = ""
    elif a_li[i] < s:
        if swich == "-":
            pass
        else:
            cnt += 1
            swich = ""
            
    s = a_li[i]

print(cnt + 1)

Submission status: Accepted

Solution code:
N = input()
As = [int(x) for x in input().split()]

new_As = [As[0]]
for i in range(1, len(As)):
    if As[i-1] != As[i]:
        new_As.append(As[i])
As = new_As

idx = 0
mode_change_count = 1
while True:            
    if idx >= (len(As)-2):
        break
    if (As[idx+1] - As[idx])*(As[idx+2] - As[idx+1]) > 0:
        idx += 1
    elif (As[idx+1] - As[idx])*(As[idx+2] - As[idx+1]) < 0:
        idx += 2
        mode_change_count += 1
  
print(mode_change_count)

Submission status: Accepted

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

print(counter)

Submission status: Accepted

Solution code:
def upper_or_lower(i):
    if A[i] < A[i + 1]:
        return "upper"
    elif A[i] > A[i + 1]:
        return "lower"
    else:
        return upper_or_lower(i + 1)


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

state = "upper"  # state が upper か lower で判定
ans = 1

# i = 0 の処理
if (len(A) > 2) and (A[0] > A[1]):
    state = "lower"

for i in range(2, N):
    if (state == "upper") and (A[i - 1] > A[i]):
        ans += 1
        if i < N - 1:
            state = upper_or_lower(i)
    elif (state == "lower") and (A[i - 1] < A[i]):
        ans += 1
        if i < N - 1:
            state = upper_or_lower(i)
    else:
        continue

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

b = 0
s = 0
ans = 1
for i in range(n-1):
    if a[i] < a[i+1]:
        b = 1
    elif a[i] > a[i+1]:
        s = 1
    if b and s:
        ans += 1
        b = 0
        s = 0
print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
# A - Sorted Arrays
N = int(input())
A = list(map(int,input().split()))

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

# 1:plus -1:minus
plus_minus = 0
ans = 0
for i in range(N-1):

    if A[i+1]-A[i]>0:
        if plus_minus == 0:
            ans += 1
            plus_minus = 1
        elif plus_minus == -1:
            plus_minus = 0
        
    elif A[i+1]-A[i]<0:
        if plus_minus == 0:
            ans += 1
            plus_minus = -1
        elif plus_minus == 1:
            plus_minus = 0
    
    if i+1==N-1:
        if plus_minus == 0:
            ans += 1
            continue

print(ans)

Submission status: Accepted

Solution code:
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N = int(sys.stdin.buffer.readline())
A = list(map(int, sys.stdin.buffer.readline().split()))

A = np.array(A, dtype=int)

dp = np.zeros((N, 2), dtype=int)
dp[0] = 1
D = np.diff(A) <= 0
U = np.diff(A) >= 0

for i, (d, u) in enumerate(zip(D, U), 1):
    if u and d:
        dp[i] = dp[i - 1]
    if u and not d:
        dp[i][0] = dp[i - 1][0]
        dp[i][1] = dp[i - 1].min() + 1
    if not u and d:
        dp[i][0] = dp[i - 1].min() + 1
        dp[i][1] = dp[i - 1][1]

print(dp[-1].min())

Submission status: Accepted

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
A = [int(_) for _ in stdin.readline().rstrip().split()]

pmz = 0
ans = 1

for i in range(N-1):
    diff = A[i+1] - A[i]
    if pmz * diff < 0:
      pmz = 0
      ans +=1
    elif pmz == 0:
      pmz = diff
    
print(ans)

Submission status: Accepted

Solution code:
import sys
n = int(input())
a = list(map(int, sys.stdin.readline().split()))
cnt = 1
up = False
down = False

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

print(cnt)

Submission status: Accepted

Solution code:
def hantei(current):
    #print(current)
    if current == n-1:
        up = True
        return up
    for i in range(current, n):
        if a[i+1] != a[i]:
            if a[i+1] > a[i]:
                up = True
            else:
                up = False
            break
    return up
    #print(up)

n = int(input())
a = [int(x) for x in input().split()]

ans = 0
ans_list = [[]]

up = hantei(0)

for i in range(n):
    #print(up, i, a[i-1], a[i])
    if i == 0:
        ans_list[ans].append(a[i])
        continue
    if ((up == True and a[i-1] < a[i]) or (up == False and a[i-1] > a[i]) or (a[i-1] == a[i])):
        #print('thru', i, a[i-1], a[i])
        #print(ans_list, ans)
        ans_list[ans].append(a[i])
    elif up == False and a[i-1] < a[i]:
        #print('down .. stop', i, a[i-1], a[i])
        ans += 1
        ans_list.append([])
        #print(ans_list, ans)
        ans_list[ans].append(a[i])
        up = hantei(i)
    elif up == True and a[i-1] > a[i]:
        #print('up .. stop', i, a[i-1], a[i])
        ans += 1
        ans_list.append([])
        #print(ans_list, ans)
        ans_list[ans].append(a[i])
        up = hantei(i)
print(len(ans_list))

Submission status: Accepted

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

sgn = 0
answer = 1
for i in range(N-1):
    cur = A[i+1] - A[i]
    if cur * sgn < 0:
        answer += 1
        sgn = 0
        continue
    if cur != 0:
        sgn = cur
print(answer)

Submission status: Accepted

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

pre = a[0]
now = ""
cnt = 1
for i in a:
    if now == "":
        if i > pre:
            now = ">"
        elif i < pre:
            now = "<"
    else:
        if i > pre and now == "<":
            now = ""
            cnt += 1
        elif i < pre and now == ">":
            now = ""
            cnt += 1
    pre = i
print(cnt)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
_=int(input())
A=list(map(int,input().split()))
N=len(A)

trend=0
ans=0
for i in range(N-1):
    temp=A[i+1]-A[i]
    if trend==0:
        if temp>0:
            trend=1
            ans+=1
        elif temp<0:
            trend=-1
            ans+=1
        else:
            pass
    elif trend==1:
        if temp>0:
            pass
        elif temp<0:
            trend=0
        else:
            pass
    elif trend==-1:
        if temp>0:
            trend=0
        elif temp<0:
            pass
        else:
            pass

print(ans if trend!=0 else ans+1)

Submission status: Accepted

Solution code:
# AGC013
N = int(input())
A = list(map(int, input().split()))
if N == 1:
    print(1)
    exit()
for i in range(N):
    if A[i] == A[i+1]:
        pass
    elif A[i] > A[i+1]:
        now = "N"
        break
    else:
        now = "P"
        break
ans = 1
for i in range(N-1):
    if now == "?":
        if A[i] == A[i+1]:
            pass
        elif A[i] > A[i+1]:
            now = "N"
        else:
            now = "P"
    elif now == "P":
        if A[i] <= A[i+1]:
            pass
        else:
            ans += 1
            now = "?"
    else:
        if A[i] >= A[i+1]:
            pass
        else:
            ans += 1
            now = "?"
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

count = 0

equal = 0
minus = 1
plus = 2

status = equal

for i in range(n - 1):
    if status == equal:
        if a[i] < a[i+1]:
            status = plus
        elif a[i] > a[i+1]:
            status = minus 
    elif status == plus:
        if a[i] > a[i+1]:
            status = equal
            count += 1
    elif status == minus:
        if a[i] < a[i+1]:
            status = equal
            count +=1
print(count+1)

Submission status: Accepted

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

cnt = 0

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

Submission status: Accepted

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

cut=0
if n==0:
  print(1)
else:
  status=2 # 0:plus 1:minus 2:other
  for i in range(n-1):
    if status==2:
      if a[i]<a[i+1]:
        status=0
      elif a[i]>a[i+1]:
        status=1
    elif status==0:
      if a[i]>a[i+1]:
        cut+=1
        status=2
    elif status==1:
      if a[i]<a[i+1]:
        cut+=1
        status=2
print(cut+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
if N == 1:
  print(1)
  exit()
ans = 0
i = 0
while i < N-1:
  ans += 1
  tag = A[i]
  if tag == A[i+1]:
    while tag == A[i+1]:
      i += 1
      if i >= N - 1:
        break
  if tag < A[i+1]:
    #単調非減少
    while tag <= A[i+1]:
      tag = A[i+1]
      i += 1
      if i >= N - 1:
        break
    i += 1
    if i == N - 1:
      ans += 1
  else:
    #単調非増加
    while tag >= A[i+1]:
      tag = A[i+1]
      i += 1
      if i >= N - 1:
        break
    i += 1
    if i == N - 1:
      ans += 1
print(ans)

Submission status: Accepted

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

    UP = 1
    NEUTRAL = 0
    DOWN = -1

    sgn = NEUTRAL

    ans = 0
    for i in range(n-1):
        if sgn == UP:
            if a[i] > a[i+1]:
                sgn = NEUTRAL
                ans += 1
        elif sgn == NEUTRAL:
            if a[i] < a[i+1]:
                sgn = UP
            elif a[i] > a[i+1]:
                sgn = DOWN
        else:
            if a[i] < a[i+1]:
                sgn = NEUTRAL
                ans += 1

    ans += 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from operator import lt, gt

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

ans = n + 1
for first_checker in (lt, gt):
    cnt = 1
    prev = a[0]
    f = first_checker
    for x in a:
        if x == prev:
            pass
        elif f is None:
            if x > prev:
                f = gt
            else:
                f = lt
        elif f(x, prev):
            pass
        else:
            f = None
            cnt += 1
        prev = x
    ans = min(ans, cnt)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int, input().split()))
f=None 
s=1
for i in range(n-1):
    if a[i]==a[i+1]:
        pass
    elif f==None:
        f=(a[i]<a[i+1]) 
    elif f==(a[i]<a[i+1]):
        pass
    else:
        s+=1
        f=None 
print(s)

Submission status: Accepted

Solution code:
n = input()
iter_ = iter(list(map(int, input().rstrip("\n").split())))
ans = 0

last = next(iter_)
try:
    while True:
        ans += 1
        now = next(iter_)
        while now == last:
            now = next(iter_)
        laststate = (last < now)
        state = laststate
        while state == laststate:
            last = now
            now = next(iter_)
            while now == last:
                now = next(iter_)
            state = (last < now)
        last = now
except StopIteration:
    print(ans)

Submission status: Accepted

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

flg = None 
slash = 0
for i in range(n - 1):
    if a[i] == a[i + 1]:
        pass
    elif flg == None:
        flg = (a[i] < a[i + 1]) 
    elif flg == (a[i] < a[i + 1]):
        pass
    else:
        slash += 1
        flg = None 

print(slash + 1)

Submission status: Accepted

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

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

count=1
mode=0
for i,Num in enumerate(A):
    if(mode==0):
        mode=1
    elif(mode==1):
        if(A[i]==A[i-1]):
            mode=1
        elif(A[i]>A[i-1]):
            mode=2
        elif(A[i]<A[i-1]):
            mode=3
    elif(mode==2):
        if(A[i]<A[i-1]):
            mode=1
            count+=1
    elif(mode==3):
        if(A[i]>A[i-1]):
            mode=1
            count+=1

print(count)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(lambda x: int(x), input().split()))
last=a[0]
inc=None
ans=1
for i in range(1,len(a)):
    if inc==True and a[i]>=last:
        pass
    elif inc==True and a[i]<last:
        inc=None
        ans+=1
    elif inc==False and a[i]<=last:
        pass
    elif inc==False and a[i]>last:
        inc=None
        ans+=1
    elif inc==None and a[i]>last:
        inc=True
    elif inc==None and a[i]<last:
        inc=False
    elif inc==None and a[i]==last:
        pass
    last=a[i]
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]

ans=0
def plus_minus(i):
    while True:
        if i==n-1:
            break
        if a[i]==a[i+1]:
            i+=1
            continue
        if a[i]<a[i+1]:
            return 1
        if a[i]>a[i+1]:
            return -1

check=plus_minus(0)            
ans=1
for j in range(n-1):
    if check==1:
        if a[j] <= a[j+1]:
            continue
        else:
            ans+=1
            check=plus_minus(j+1)
    else:
        if a[j]>=a[j+1]:
            continue
        else:
            ans+=1
            check=plus_minus(j+1)
print(ans)