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
sumDiff = 0	#この値の符号によって単純非減少、単純非増加を判定
		#初期や区切った直後は0でそこからdiffを積み上げる。符号が異なる場合を前で除いているので
		#絶対値的に大きくだけで符号は変わらない。0のときも問題なく適用できる
for i in range(1,len(A)):
	
	diff = A[i]-A[i-1]
	if sumDiff*diff < 0:		#sumDiff,diff=(正,負),(負,正)のときだけ成立 どちらかが0の場合非成立
		ans += 1
		sumDiff = 0		#sumDiffをリセット(区切った直後にまた区切ることはないので、
					#0だとこの分岐に引っかからないようにした)
	else:
		sumDiff += diff		#代入だと0のとき符号が維持できない。加算することで符号が維持できる
 
print(ans)

Submission status: Accepted

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

ans = float('inf')
for way in 1, -1:
    b = 1
    t = a[0]
    for aa in a:
        if way == 1:
            if aa < t:
                b += 1
                way = 0
        elif way == -1:
            if aa > t:
                b += 1
                way = 0
        else:
            if aa < t:
                way = -1
            elif aa == t:
                way = 0
            else:
                # aa > t:
                way = 1
        t = aa
    ans = min(ans, b)
print(ans)

Submission status: Accepted

Solution code:
import numpy

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

ans = 1				#配列数。切らない場合1なので初期値1
beforeDiff = 0				#一つ前の差分値。初期値は任意
chgFlag = True				#区切りフラグ。初期値は0番目に区切った扱いにするのでTrue

for i in range(1,N):			#i-1を参照するため1スタート
	diff = AN[i] - AN[i-1]
	
	if chgFlag or beforeDiff==0:	#符号を維持(区切り直後、前回の差が0)
		beforeDiff = diff	#区切り直後の差分値を入れる(重要なのは符号)
		chgFlag = False	#区切らない
		continue
		
	if (numpy.sign(beforeDiff) == 1 and numpy.sign(diff) == -1) or (numpy.sign(beforeDiff) == -1 and numpy.sign(diff) == 1):		#以前と符号が逆なら区切る
		chgFlag = True		#区切る
		ans += 1
		
print(ans)

Submission status: Accepted

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

ans = 1				#配列数。切らない場合1なので初期値1
beforeDiff = 0				#一つ前の差分値。初期値は任意
chgFlag = True				#区切りフラグ。初期値は0番目に区切った扱いにするのでTrue

for i in range(1,N):			#i-1を参照するため1スタート
	diff = AN[i] - AN[i-1]
	
	if chgFlag or beforeDiff==0:	#符号を維持(区切り直後、前回の差が0)
		beforeDiff = diff	#区切り直後の差分値を入れる(重要なのは符号)
		chgFlag = False	#区切らない
		continue
		
	if beforeDiff*diff < 0:		#以前と符号が逆なら区切る
		chgFlag = True		#区切る
		ans += 1
		
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int,input().split()))
L.append(0)
cur = L[0]
fugou = '±'
for i in range(1,N):
  if L[i] > cur:
    if not i == N:
      fugou = '+'
      break
  elif L[i] < cur:
    if not i == N:
      fugou = '-'
      break
if fugou == '±':
  print(1)
else:
  ans = 0
  for i in range(N):
    if fugou == '+':
      if cur > L[i]:
        fugou = '±'
        ans += 1
    elif fugou == '-':
      if cur < L[i]:
        fugou = '±'
        ans += 1
    else:
      if cur < L[i]:
        fugou = '+'
      elif cur > L[i]:
        fugou = '-'
    cur = L[i]
  print(ans+1)

Submission status: Accepted

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

ans = 1		#切らない場合1
beforeDiff = 0

for i in range(1,N):
	diff = AN[i] - AN[i-1]
	
	if beforeDiff==0:	#符号を維持
		beforeDiff = diff
	elif beforeDiff*diff < 0:	#以前と符号が逆なら区切る
		beforeDiff = 0	#長さ1では切らず、次のループでdiffを代入させるため0
		ans += 1
				#以前と符号が同じなら何もしない
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [int(input_raw[0])]
    input_raw = read_func().strip().split()
    A = [int(input_raw[i]) for i in range(N)]

    pre_stat = 999
    count = 1
    for i in range(N - 1):
        if A[i] < A[i + 1]:
            stat = 1
        elif A[i] > A[i + 1]:
            stat = -1
        else:
            stat = 0
        if pre_stat != 999 and stat != 0:
            if stat != pre_stat:
                count += 1
                pre_stat = 999
        elif stat != 0:
            pre_stat = stat
    print count

if __name__ == '__main__':
    main()

Submission status: Accepted

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

p = A[0]
count = 0
b = 0
c = 0
for i in range(1,N):
    a = A[i]
    b += a-p
    c += abs(a-p)
    if abs(b) != c:
        count += 1
        b = 0
        c = 0
    p = a
print(count+1)

Submission status: Accepted

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

if n == 1:
    cnt = 1
else:
    if A[0] < A[1]:
        flag = 1
    elif A[0] > A[1]:
        flag = -1
    else:
        flag = 0
    
    cnt = 1
    x = A[1]
    for i in range(2, n):
        if flag == 1:
            if x > A[i]:
                cnt += 1
                flag = 0                
        elif flag == -1:
            if x < A[i]:
                cnt += 1
                flag = 0
        else:
            if x < A[i]:
                flag = 1
            elif x > A[i]:
                flag = -1
            else:
                flag = 0
        x = A[i]
    
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
s = a[0]
c,j = 0,0
for i in a[1:]:
 
  if j ==0:
    if i-s > 0:
      j =1
      s = i
      continue
    elif i-s < 0:
      j = -1
      s = i
      continue
  elif i-s > 0 and j ==-1:
    j = 0
    c+=1
    
  if i-s < 0 and j==1:
    j = 0
    c+=1
  s = i
  
print(c+1)

Submission status: Accepted

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

if n == 1:
    cnt = 1
else:
    if A[0] < A[1]:
        flag = 1
    elif A[0] > A[1]:
        flag = -1
    else:
        flag = 0
    
    cnt = 1
    x = A[1]
    for i in range(2, n):
        if flag == 1:
            if x <= A[i]:
                pass
            else:
                cnt += 1
                flag = 0
                
        elif flag == -1:
            if x < A[i]:
                cnt += 1
                flag = 0
            else:
                pass
            
        else:
            if x < A[i]:
                flag = 1
            elif x > A[i]:
                flag = -1
            else:
                flag = 0
        #print('x', x, 'A[i]', A[i], 'flag', flag, 'cnt', cnt)
        x = A[i]
    
print(cnt)

Submission status: Accepted

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

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

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

Submission status: Accepted

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


def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = [a[0]]
    ans = 1

    for i, j in zip(a, a[1:]):
        if i != j:
            b.append(j)

    m = len(b)
    i = 0
    j = 1

    while i + 1 < m:
        while j + 1 < m:
            if (b[i + 1] - b[i]) * (b[j + 1] - b[j]) < 0:
                ans += 1
                i = j + 1
            j += 1

        i += 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def solve():
  n= int(input())
  a = list(int(i) for i in input().split())
  ct = 0
  flag = 0
  for i in range(n-1):    
   if a[i+1] > a[i]:
    if flag == -1:
      ct+= 1
      flag = 0
    else:
      flag = 1
   elif a[i+1] < a[i]:
    if flag == 1:
      ct+= 1
      flag = 0
    else:
      flag = -1 
  return ct+1
  
  
if __name__ == '__main__':
  print(solve())

Submission status: Accepted

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

if N == 1:
    print(1)
else:
    divide = 0
    if A[0] < A[1]:
        up = 1
    elif A[0] > A[1]:
        up = -1
    else:
        up = 0
    for i in range(1,N-1):
        if A[i] == A[i+1]:
            continue
        elif A[i] < A[i+1]:
            if up < 0:
                divide += 1
                up = 0
            else:
                up = 1
                continue
        else:
            if up > 0:
                divide += 1
                up = 0
            else:
                up = -1
                continue
    print(divide+1)

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
# from collections import Counter, defaultdict

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

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    n = II()
    A = LI()

    # 1: 単調非減少，2: 単調非増加，0: 未定
    state = 0
    prev = A[0]
    res = 1
    for cur in A:
        if state:
            if state == 1 and prev > cur:
                res += 1
                state = 0
            if state == 2 and prev < cur:
                res += 1
                state = 0
        else:
            if prev < cur:
                state = 1
            if prev > cur:
                state = 2
        prev = cur

    return res


print(main())

Submission status: Accepted

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

flag = True
flag_=True #単調増加True,単調減少False
ans = 0

for i in range(n-1):
  if flag == True:
    if a[i]==a[i+1]:
      continue
    elif a[i]< a[i+1]:
      flag_ = True
    elif a[i]> a[i+1]:
      flag_ = False
    flag = False
      
  else:
    if flag_ == True and a[i]<= a[i+1]:
      continue
    elif flag_ == False and a[i]>= a[i+1]:
      continue
    else:
      ans = ans +1
      flag = True
print(ans+1)

Submission status: Accepted

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

dif = []
for i in range(1,N):
    if A_arr[i] > A_arr[i-1]:
        dif.append(1)
    elif A_arr[i] < A_arr[i-1]:
        dif.append(-1)
    else:
        dif.append(0)

tmp = 0
count = 0
for i in range(len(dif)):
    if tmp * dif[i] == -1:
        count += 1
        tmp = 0
    elif dif[i] != 0:
        tmp = dif[i]
    else:
        pass

print(count+1)

Submission status: Accepted

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

s = 0
ans = 1
for i in range(1, N):
    d = A[i] - A[i - 1]
    if d * s < 0:
        ans += 1
        s = 0
        continue
    if d != 0:
        s = d
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
#  0 : 未定
#  1 : 増加
# -1 : 減少
res = 0

prv = a[0]
cond = 0
for i in a:
    if cond == 0:
        if prv > i:
            cond = -1
        elif prv < i:
            cond = 1
    elif cond == 1:
        if prv > i:
            cond = 0
            res += 1
    else:
        if prv < i:
            cond = 0
            res += 1
    prv = i

print(res + 1)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = 1
t = 0

for k in range(1,N):
    if A[k-1] > A[k]:
        t = -1
        break
    elif A[k-1] < A[k]:
        t = 1
        break
if t==0:
    print(1)
    exit(0)

k = 0
while k < N-1:
    k += 1
    if t == 1:
        if A[k-1] > A[k]:
            ans += 1
            while k+1<N and A[k]==A[k+1]:
                k += 1
            if k == N-1:
                break
            elif A[k] > A[k+1]:
                t = -1
            else:
                t = 1
    else:
        if A[k-1] < A[k]:
            ans += 1
            while k+1<N and A[k]==A[k+1]:
                k += 1
            if k == N-1:
                break
            elif A[k] > A[k+1]:
                t = -1
            else:
                t = 1

print(ans)

Submission status: Accepted

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

def search(nums, state):
    cur = nums[0]
    ans = 1
    for num in nums[1:]:
        if state == "up":
            if cur > num:
                ans += 1
                state = "neutral"
        
        elif state == "down":
            if cur < num:
                ans += 1
                state = "neutral"
        
        elif state == "neutral":
            if cur > num:
                state = "down"
            elif cur < num:
                state = "up"
            else:
                state = "neutral"
        
        cur = num
    return ans 
    
print(min(search(nums, "up"), search(nums, "down")))

Submission status: Accepted

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

ans = 1
up = None
for a,b in zip(A,A[1:]):
    if a==b: continue
    if up is None:
        up = a<b
    elif up:
        if a>b:
            up = None
            ans += 1
    else:
        if a<b:
            up = None
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
input()
a = [int(i) for i in input().split()]
mode = ''
cnt = 1
for i in range(len(a)-1):
  if mode == '' and a[i] < a[i+1]:
    mode = 'inc'
  elif mode == '' and a[i] > a[i+1]:
    mode = 'dec'
  elif mode == 'inc' and a[i] > a[i+1]:
    mode = ''
    cnt += 1
  elif mode == 'dec' and a[i] < a[i+1]:
    mode = ''
    cnt += 1
print(cnt)

Submission status: Accepted

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

    if flag == 1 and a[i] <= a[i+1]:
        continue
    if flag == 1 and a[i] > a[i+1]:
        flag = 0
        ans[i] = 1
        continue

    if flag == -1 and a[i] >= a[i+1]:
        continue
    if flag == -1 and a[i] < a[i+1]:
        flag = 0
        ans[i] = 1
        continue

print(ans.count(1))

Submission status: Accepted

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

ans = 1
up = None
for l, r in zip(a, a[1:]):
    if l == r:
        continue
    if l < r:
        if up is None:
            up = True
        elif not up:
            ans += 1
            up = None
    else:
        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()))
ans=1
d=0
for i in range(1,n):
    diff=a[i]-a[i-1]
    if d==0:
        d=diff
    elif d*diff<0:
        d=0
        ans+=1
print(ans)

Submission status: Accepted

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

preva = None
mode = 0  # 0: 不定 1: 単調増加 2: 単調減少
c = 0
for a in alist:
  if preva is None:
    preva = a
  elif mode == 0:
    if preva == a:
      continue
    if preva > a:
      mode = 2
    else:
      mode = 1
    preva = a
  elif mode == 1 and preva > a:
    c += 1
    preva = a
    mode = 0
  elif mode == 2 and preva < a:
    c += 1
    preva = a
    mode = 0
  else:
    preva = a
print(c + 1)

Submission status: Accepted

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

count = 0
sign = 0
num = 0
ans = 0
for i, ai in enumerate(a):
    if count == 0:
        num = ai
        sign = 0
        count += 1
    elif count == 1:
        if ai > num:
            sign = 1
        elif ai < num:
            sign = -1
        else:
            sign = 0
        count += 1
        num = ai
    else:
        if sign == 0:
            if ai > num:
                sign = 1
            elif ai < num:
                sign = -1
            else:
                sign = 0
            count += 1
            num = ai
        elif sign > 0:
            if ai < num:
                num = ai
                count = 1
                sign = 0
                ans += 1
            else:
                num = ai
        else:
            if ai > num:
                num = ai
                count = 1
                sign = 0
                ans += 1
            else:
                num = ai

if count > 0:
    ans += 1

print(ans)

Submission status: Accepted

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

ans = 1
up = None
for a,b in zip(A,A[1:]):
    if a==b: continue
    if a<b:
        if up is None:
            up = True
        elif not up:
            ans += 1
            up = None
    else:
        if up is None:
            up = False
        elif up:
            ans += 1
            up = None
print(ans)

Submission status: Accepted

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

if n==1:
    print(1)
else:
    mode = None
    ans = 1
    for i in range(n-1):
        if aa[i] != aa[i+1]:
            if mode == None:
                mode = aa[i]<aa[i+1]
            elif (aa[i] < aa[i+1]) != mode:
                ans+=1
                mode = None
        # print(aa[i], aa[i+1], aa[i]<aa[i+1],  mode, ans)
    print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
if n==1:
    print(1)
else:
    u=a[0]<=a[1]
    d=a[0]>=a[1]
    res=0
    for i in range(2,n):
        u=u and a[i-1]<=a[i]
        d=d and a[i-1]>=a[i]
        #print(i,u,d)
        if not any([u,d]):
            res+=1
            u=True
            d=True
    print(res+1)

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
def C(n,A):
    if n==1 or n==2:
        return 1
    else:
        cnt=1
        d=A[1]-A[0]
        for i in range(2,n):
            if d:
                if (A[i]-A[i-1])*d<0:
                    cnt+=1
                    d=0
            else:
                d=A[i]-A[i-1]
        return cnt
print(C(n,A))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
cnt = 0
id = 1
while a != []:
    if len(a)==1:
        cnt += 1
        break
    if a[0] == a[1]:
        while a[id-1] == a[id] and id <= len(a)-1:
            id += 1
            if id == len(a):break
        if id == n:
            cnt = 1
            break
        del(a[:id-1])
        id = 1
    if a[0] > a[1]:
        while  a[id-1] >= a[id] and id <= len(a)-1:
            id += 1
            if id == len(a):break
    elif a[0] < a[1]:
        while a[id-1] <= a[id] and id <= len(a)-1:
            id += 1
            if id == len(a):break
    cnt += 1
    del(a[:id])
    id = 1


print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 傾きの正負の変化を調べるのに、
# AとB(傾き)をかけてその符号を見るという方法があると気づいた
N = int(input())
A = list(map(int, input().split(" ")))

c = 1
e = 0
for i in range(N-1):
    d = A[i+1]-A[i]
    # print(d, e, e*d < 0)
    # 一方が負の数、もう一方が正の数なら
    if e*d < 0:
        c += 1
        e = 0
    elif d != 0: # この条件忘れた
        e = d

print(c)

Submission status: Accepted

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

s = 0
c = 0
for i in range(1,N):
    d = A[i] - A[i-1]
    if d * s < 0:
        c += 1
        s = 0
        continue
    if d != 0:
        s = d
print(c+1)

Submission status: Accepted

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

ans = 0
diff = 0
for i in range(1, n):
  tmp_diff = a[i] - a[i - 1]
  if tmp_diff != 0 and diff == 0:
    diff = tmp_diff
    continue
  if not(tmp_diff == 0 or tmp_diff * diff > 0):
    ans += 1
    diff = 0
    continue
print(ans + 1)

Submission status: Accepted

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

ans = 1
d = 0
tmp = A[0]
for a in A[1:]:
    if d*(a-tmp) < 0:
        ans += 1
        d = 0
    else:
        d += a-tmp
    tmp = a

print(ans)

Submission status: Accepted

Solution code:
def solve(N, As):
    increasing = None
    ans = 1
    for a1, a2 in zip(As, As[1:]):
        if increasing is None:
            if a1 < a2:
                increasing = True
            elif a1 > a2:
                increasing = False
        else:
            if a1 == a2 or (a1 < a2 and increasing) or (a1 > a2 and not increasing):
                pass
            else:
                ans += 1
                increasing = None
    return ans


if __name__ == "__main__":
    N = int(input())
    As = list(map(int, input().split(" ")))
    print(solve(N, As))

Submission status: Accepted

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

decided = False
upping = True
i = 1
before = A[0]
ans = 1
while i < N:
    if decided:
        if upping:
            if before <= A[i]:
                pass
            else:
                decided = False
                ans += 1
        else:
            if before >= A[i]:
                pass
            else:
                decided = False
                ans += 1
    else:
        if before > A[i]:
            upping = False
            decided = True
        elif before < A[i]:
            upping = True
            decided = True
        elif before == A[i]:
            pass
    before = A[i]
    i += 1
print(ans)

Submission status: Accepted

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

    diff = [A[i]-A[i-1] for i in range(1, N)]
    ans = 1

    last = None
    for d in [d for d in diff if d != 0]:
        if d == 0:
            continue
        if last is None:
            last = d
        else:
            if last * d < 0:
                ans += 1
                last = None
            else:
                last = d
    print(ans)


solve()

Submission status: Accepted

Solution code:
N = int(input())
cnt = 0
tmp = ''
M = list(map(int,input().split()))
L = []
for x in M:
    if x == tmp:
        pass
    else:
        L.append(x)
    tmp = x
    

    
while True:
    try: 
        if L[0] <L[1]>L[2] or L[0] > L[1] <L[2]:
            a0=L.pop(0)
            a1=L.pop(0)
            cnt += 1
        else:
            L.pop(0)
    except:
        break
#    print(L)
print(1+cnt)

Submission status: Accepted

Solution code:
# _*_ coding:utf-8 _*_
#  Atcoder_Grand_Contest013-A
#  TODO https://atcoder.jp/contests/agc013/tasks/agc013_a

def solveProblem(allNumber,aSequence):
	flag="Flat"
	count = 1
	for i in range(allNumber-1):
		diff = aSequence[i+1]-aSequence[i]
		if flag == "Plus":
			if diff < 0:
				flag="Flat"
				count=count+1
		elif flag == "Minus":
			if diff > 0:
				flag="Flat"
				count = count+1
		else:
			if diff > 0 :
				flag="Plus"
			elif diff < 0:
				flag="Minus"

	answer = count
	return answer


if __name__ == '__main__':
	N = int(input().strip())
	As = list(map(int,input().strip().split(' ')))
	solution = solveProblem(N,As)
	print("{}".format(solution))

Submission status: Accepted

Solution code:
# _*_ coding:utf-8 _*_
#  Atcoder_Grand_Contest013-A
#  TODO https://atcoder.jp/contests/agc013/tasks/agc013_a


def solveProblem(allNumber,aList):
	flag = "Flat"
	count = 1
	for i in range(allNumber - 1):
		b = aList[i + 1] - aList[i]
		if flag == "Plus":
			if b < 0:
				flag = "Flat"
				count = count+1
		elif flag == "Minus":
			if b > 0:
				flag = "Flag"
				count = count+1
		else:
			if b > 0:
				flag = "Plus"
			elif b < 0:
				flag = "Minus"
	answer = count
	return answer

if __name__ == '__main__':
	N = int(input().strip())
	As = list(map(int,input().split()))
	solution = solveProblem(N,As)
	print("{}".format(solution))

Submission status: Accepted

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

if N < 3:
	print(ans)
	exit()

if a[0] < a[1]:
	flag = 1
if a[0] > a[1]:
	flag = -1
if a[0] == a[1]:
	flag = 0


for i in range(2,N):
	if flag == 1 and a[i-1] > a[i]:
		ans += 1
		flag = 0
	elif flag == -1 and a[i-1] < a[i]:
		ans += 1
		flag = 0
	elif flag == 0 and a[i-1] < a[i]:
		flag = 1
	elif flag == 0 and a[i-1] > a[i]:
		flag = -1
	
print(ans)

Submission status: Accepted

Solution code:
def solve(A, inc):

    ans = 1
    N = len(A)
    l = []
    for i in range(1, N):
        if A[i] > A[i-1]:
            l.append(1)
        elif A[i] < A[i-1]:
            l.append(-1)

    for i in range(1, len(l)):
        if l[i]*l[i-1] < 0:
            l[i] = 0
            ans += 1
    return ans


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

print(solve(A, True))

Submission status: Accepted

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

import sys, re
from collections import deque, defaultdict, Counter
from math import sqrt, hypot, factorial, pi, sin, cos, radians
if sys.version_info.minor >= 5: from math import gcd
else: from fractions import gcd 
from heapq import heappop, heappush, heapify, heappushpop
from bisect import bisect_left, bisect_right
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from functools import reduce, partial
from fractions import Fraction
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def ceil(a, b=1): return int(-(-a // b))
def round(x): return int((x*2+1) // 2)
def fermat(x, y, MOD): return x * pow(y, MOD-2, MOD) % MOD
def lcm(x, y): return (x * y) // gcd(x, y)
def lcm_list(nums): return reduce(lcm, nums, 1)
def gcd_list(nums): return reduce(gcd, nums, nums[0])
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N = INT()
aN = LIST()

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

# フラグで向かう方向を管理
# 0:flat 1:up 2:down
flag = 0
if aN[0] < aN[1]:
    flag = 1
elif aN[0] == aN[1]:
    flag = 0
else:
    flag = 2

cnt = 1
# 向きが変わる所を区切り位置とする
for i in range(2, N):
    if aN[i-1] == aN[i]:
        continue
    elif aN[i-1] > aN[i]:
        if flag == 1:
            cnt += 1
            flag = 0
        else:
            flag = 2
    elif aN[i-1] < aN[i]:
        if flag == 2:
            cnt += 1
            flag = 0
        else:
            flag = 1
print(cnt)

Submission status: Accepted

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

ans = 1
inc = 0
tmp = li[0]
for a in li[1:]:
    if inc * (a - tmp) < 0:
        ans += 1
        inc = 0
    else:
        inc += a - tmp
    tmp = a

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a = [a[0]] + a
hoge = 0
ans = 1
for i in range(n):
    prev = a[i]
    now = a[i+1]
    # どちらにも行ける
    if hoge == 0:
        if now > prev:
            hoge = 1
        elif now < prev:
            hoge = -1
    # 増加中
    elif hoge == 1:
        if now < prev:
            hoge = 0
            ans += 1
    # 減少中
    else:
        if now > prev:
            hoge = 0
            ans += 1
print(ans)

Submission status: Accepted

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

ans = 1

up = False
down = False
ok = True

t = a[0]
i = 1
while i < N:
    if ok:
        if a[i] > t:
            up = True
            down = False
            ok = False
        elif a[i] == t:
            up = False
            down = False
            ok = True
        else:
            up = False
            down = True
            ok = False
    elif up:
        if a[i] >= t:
            up = True
            down = False
            ok = False
        else:
            up = False
            down = False
            ok = True
            ans += 1
    elif down:
        if a[i] <= t:
            up = False
            down = True
            ok = False
        else:
            up = False
            down = False
            ok = True
            ans += 1
    t = a[i]
    i += 1
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
if N==1:
    print(1)
    exit()
T=[[A[0],A[1]]]
for i in range(2,N):
    if len(T[-1])<=1:
        T[-1].append(A[i])
        continue
    if not((T[-1][0]>=A[i-2]>=A[i-1]>=A[i]) or (T[-1][0]<=A[i-2]<=A[i-1]<=A[i])):
        T.append([A[i]])
    else:
        T[-1].append(A[i])
print(len(T))

Submission status: Accepted

Solution code:
a = int(input())
ar = list(map(int,input().split(" ")))
count = 1
n = ""
for i in range(a-1):
    if ar[i] > ar[i+1]:
        if n == "P":
            count += 1
            n = ""
        else:
            n = "M"
    elif ar[i] < ar[i+1]:
        if n == "M":
            count += 1
            n = ""
        else:
            n = "P"
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())

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


count=1

state=0

for i in range(1,n):
    a=state
    d=x[i]-x[i-1]
    if(d>0):
        state=1
    elif(d==0):
        pass
    elif(d<0):
        state=-1
    if(state*a==-1):
        count+=1
        state=0

    


print(count)

Submission status: Accepted

Solution code:
import sys

import numpy as np


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


def get_state(x, y):
    if x > y:
        return "Down"
    if x < y:
        return "Up"
    return "Equal"


N = ii()
A = il()
pre_state = None
c = 1
for i in range(N - 1):
    current_state = get_state(A[i], A[i + 1])
    if current_state == "Equal" or current_state == pre_state:
        continue
    if pre_state is None:
        pre_state = current_state
    else:
        pre_state = None
        c += 1
print(c)