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()))

cnt = 1
inc = False
dec = False
p = A.pop(0)

for Ai in A:
    if (not inc) and (not dec):
        if Ai < p:
            dec = True
        elif Ai > p:
            inc = True
    elif inc:
        if Ai < p:
            cnt += 1
            inc = False
    elif dec:
        if Ai > p:
            cnt += 1
            dec = False
    p = Ai
    
print(cnt)

Submission status: Accepted

Solution code:
# 2020/05/07
# AtCoder Grand Contest 013 - A

# Input
n = int(input())
a = list(map(int,input().split()))

# Calc
cnt = 1
dr = 0
sav = a[0]
for i in range(1, n):
    # Initial Status
    if dr == 0:
        # Increase
        if sav < a[i]:
            dr = 1
        # Decrease
        elif sav > a[i]:
            dr = -1
    # Case Increase
    elif dr == 1:
        if sav > a[i]:
            cnt = cnt + 1
            dr = 0
    # Case Decrease
    elif dr == -1:
        if sav < a[i]:
            cnt = cnt + 1
            dr = 0
    sav = a[i]

ans = cnt

# Output
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
flag=True
s=False
e=0
c=1
for i in range(1,n):
    if a[i]==a[i-1]:
        continue
    if a[i]>a[i-1]:
        flag=True
        e=i
        break
    elif a[i]<a[i-1]:
        flag=False
        e=i
        break
if e==n:
    print(1)
    exit()

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

Submission status: Accepted

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

cnt = 1
mono = 0
for i in range(1, n):
  d = a[i] - a[i - 1]
  if d * mono < 0:
    cnt += 1
    mono = 0
  elif d != 0:
    mono = d
print(cnt)

Submission status: Accepted

Solution code:
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    N = int(_in[0])  # type:int
    A_arr = list(map(int,_in[1].split(' ')))  # type:list(int)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    cnt = 0
    i = -1
    while True:
        i += 1
        while i<N-1 and A_arr[i]==A_arr[i+1]:
            i += 1
        if i<N-1 and A_arr[i]<=A_arr[i+1]:
            while i<N-1 and A_arr[i]<=A_arr[i+1]:
                i += 1
        elif i<N-1 and A_arr[i]>=A_arr[i+1]:
            while i<N-1 and A_arr[i]>=A_arr[i+1]:
                i += 1
        cnt += 1
        if i>=N-1:
            break
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)

Submission status: Accepted

Solution code:
n=int(input())

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

index = 1

ans = 0

while index < n:
    ans += 1
    if a[index-1] < a[index]:
        flag = True
    elif a[index-1] > a[index]:
        flag = False
    else:
        flag3 = True
        for i in range(index,n):
            if a[index] != a[i]:
                flag3 = False
                if a[index] < a[i]:
                    flag = True
                    break
                else:
                    flag = False
                    break
        if flag3:
            break
    flag2 = True
    for i in range(index,n):
        if flag and a[i] < a[i-1]:
            index = i+1
            flag2 = False
            break
        elif a[i] > a[i-1] and flag == False:
            index = i+1
            flag2 = False
            break
    if flag2:
        break
if index == n:
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    lst = [int(_) for _ in input().split()]
    last = lst[0]
    A = [last]
    for i in range(1, N):
        if lst[i] == last: continue
        last = lst[i]
        A.append(lst[i])
    left = right = output = 0
    while left < len(A):
        if left == len(A)-1:
            output += 1
            break
        if A[left+1] - A[left] > 0:
            while right < len(A)-1 and A[right] < A[right+1]:
                right += 1
        elif A[left+1] - A[left] < 0:
            while right < len(A)-1 and A[right] > A[right+1]:
                right += 1
        output += 1
        left = right + 1
        right = left
    print(output)
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:

import numpy as np

n  = int(input())
a = np.array(list(map(int,input().split())))


ret = 0
i = 0
while i < len(a):
    j = i
    while j < n -1 and a[j] <= a[j+1]:
        j += 1
    h = i
    while h < n - 1 and a[h] >= a[h +1]:
        h += 1
    i = max(h,j) + 1
    ret += 1

print(ret)

Submission status: Accepted

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

N = int(input())
A = list(map(int, input().split()))
if N == 1:
    print(1)
    exit()
i = 0
B = []
b = [A[0]]
zouka = 0
f = True
while i < N-1:
    if A[i] == A[i+1]:
        b.append(A[i])
        i += 1
        continue

    if zouka == 0:
        if A[i] < A[i+1]:
            zouka = 1
        else:
            zouka = -1

    if A[i] < A[i+1]: #増加
        if zouka == 1:
            b.append(A[i+1])
        else:
            B.append(b)
            b = [A[i+1]]
            zouka = 0
            
    else: #減少
        if zouka == 1:
            B.append(b)
            b = [A[i+1]]
            zouka = 0
        else:
            b.append(A[i+1])
    i += 1
B.append(b)
print(len(B))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = input()
n = int(input())
#  = map(int, input().split())
a  = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

if n == 1:
    print(1)
else:
    ans = 1
    state = lambda n1, n2: 0 if n1 == n2 else (1 if n2 > n1 else -1)
    diff = [state(a1, a2) for a1, a2 in zip(a[:-1], a[1:]) if state(a1, a2) != 0]

    p = 1
    while p < len(diff):
        if abs(diff[p] - diff[p-1]) == 2:
            ans += 1
            p += 1

        p += 1

    print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = list(map(int, input().split()))

turn_p = 0
if N > 2:
    a_ = A[1]
    d_ = np.sign(A[1] - A[0])
    for a in A[2:]:
        d = np.sign(a - a_)
        #print(a, d, d_, turn_p)
        if d != 0:
            if d_ == 0:
                d_ = d
            elif d_ != d:
                turn_p += 1
                d_ = 0
        a_ = a 

print(turn_p+1)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
cnt = 1
b = [0] + a
c = [(i-j)//abs(i-j) for i, j in zip(a, b) if i != j]
del c[0]
if c:
  mae=c[0]
  d=iter(c)
  for x in d:
    if x != mae:
      cnt += 1
      try:
        mae = d.__next__()
      except StopIteration:
        break
print(cnt)

Submission status: Accepted

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

ans = 1
now = a[0]
before = '-'
for i in range(1,n):
    if before == '-':
        if now > a[i]:
            before = 'D'
        elif now< a[i]:
            before = 'U'

    else:
        if before == 'U' and now > a[i]:
            ans += 1
            before = '-'
        elif before == 'D' and now < a[i]:
            ans += 1
            before = '-'
    now = a[i]

print(ans)

Submission status: Accepted

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

def main():
    N = int(input())
    A = list(map(int, input().split()))
    ans = 0
    i = 0
    while 1:
        if i == N:
            break
        # 単調非減少と単調非増加の列の長い方を採用Greedy
        a = i
        while a < N-1 and A[a] <= A[a+1]:
            a += 1
        b = i
        while b < N-1 and A[b] >= A[b+1]:
            b += 1
        i = max(a,b) + 1
        ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''

    N = int(input())
    A_list = [int(item) for item in input().split()]

    if N >=2:
        prev_num = A_list[0]
        new_list = []
        prev_direction = 0
        cnt = 1
        for item in A_list[1:]:
            # print(item - prev_num)
            if item != prev_num:
                if prev_direction == 1:
                    if item - prev_num >= 0:
                        pass
                    else:
                        cnt += 1
                        prev_direction = 0
                elif prev_direction == -1:
                    if item - prev_num <= 0:
                        pass
                    else:
                        cnt += 1
                        prev_direction = 0
                else:
                    if item - prev_num > 0:
                        prev_direction = 1
                    elif item - prev_num < 0:
                        prev_direction = -1
    
            prev_num = item
    else:
        cnt = 1

    print(cnt)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
pumi=0
ff=0
cut=1
for i in range(0,N-1):
    if pumi==0:
        ff=0
    elif pumi==-1:
        ff=-1
    elif pumi==1:
        ff=1

    if A[i+1]>A[i]:
        pumi=1
    elif A[i+1]<A[i]:
        pumi=-1

    if pumi==-1 and ff==1:
        cut+=1
        pumi=0
    elif pumi==1 and ff==-1:
        cut+=1
        pumi=0
print(cut)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=1;before=a[0];mode=0
for i,v in enumerate(a):
    nmode=0
    if before>v:
        nmode=-1
    elif before<v:
        nmode=1
    before=v
    if mode*nmode==-1:
        mode=0
        ans+=1
    elif mode==0:
        mode=nmode
print(ans)

Submission status: Accepted

Solution code:
import math
import sys

n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().rstrip().split()))
flag = "Init"
ans = 0
for i in range(1, n):
    if flag == "Init":
        if a[i - 1] == a[i]:
            pass
        elif a[i - 1] > a[i]:
            flag = "-"
        else:
            flag = "+"
    elif flag == "-":
        if a[i - 1] < a[i]:
            ans += 1
            flag = "Init"
    else:
        if a[i - 1] > a[i]:
            ans += 1
            flag = "Init"
print(ans + 1)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))
    ans = 1
    flag = "Init"
    for i in range(1, n):
        if flag == "Init":
            if a[i - 1] == a[i]:
                pass
            elif a[i - 1] > a[i]:
                flag = "-"
            else:
                flag = "+"
        elif flag == "-":
            if a[i - 1] < a[i]:
                ans += 1
                flag = "Init"
        else:
            if a[i - 1] > a[i]:
                ans += 1
                flag = "Init"
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 1
trend = '-'
pre = a[0]
for now in a:
    if (trend=='<' and pre>now) or (trend=='>' and pre<now):
        ans += 1
        trend = '-'
    else:
        if pre<now:
            trend = '<'
        elif pre>now:
            trend = '>'
    pre = now
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
counter = 1 ###グループ数
sign = 0 ###増減、-1,0,1、0はグループの初回、または初回以降増減が見られないときのみ
for x in range (1,N):
	if A[x]>A[x-1] and (sign==0 or sign==1):
		sign = 1
	elif A[x]>A[x-1] and (sign==-1):
		counter += 1
		sign = 0
	elif A[x]<A[x-1] and (sign==0 or sign==-1):
		sign = -1
	elif A[x]<A[x-1] and (sign==1):
		counter += 1
		sign = 0
print(counter)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    last = A[0]
    direction = 0
    ans = 1
    for i in range(1,N):
        if direction == 0:
            if last< A[i]:
                direction = 1
            elif last>A[i]:
                direction = -1
            last = A[i]
            continue
        if direction == 1:
            if last<=A[i]:
                last = A[i]
                continue
            else:
                last = A[i]
                direction = 0
                ans += 1
                continue
        if direction == -1:
            if last >= A[i]:
                last = A[i]
                continue
            else:
                last = A[i]
                direction = 0
                ans += 1
                continue
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

    return N,A

def kansu(LN,LA):
    highflg = False
    lowflg = False
    count = 1

    for n in range(LN-1):
        if LA[n] == LA[n+1]:
            continue
        elif LA[n] < LA[n+1]:
            highflg = True
            if lowflg == True:
                count += 1
                lowflg = False
                highflg = False
        else:
            lowflg = True
            if highflg == True:
                count += 1
                lowflg = False
                highflg = False
    
    print(count)






N,A = nyu()
kansu(N,A)

Submission status: Accepted

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

ans1 = 1
flag = ''
for i in range(1, N):
  if flag == '':
    if L[i] < L[i-1]:
      flag = 'minus'
    elif L[i] > L[i-1]:
      flag = 'plus'
    else:
      flag = ''

  if flag == 'plus':
    if L[i] < L[i-1]:
      flag = ''
      ans1 += 1
  elif flag == 'minus':
    if L[i] > L[i-1]:
      flag = ''
      ans1 += 1


print(ans1)

Submission status: Accepted

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

i = 1
ans = 1
change = [False,False]

while i<n:
    if change == [True,True]:
        ans += 1
        change = [False,False]
        #print(change)
    elif a[i]>a[i-1]:
        change[0] = True
        i += 1
        #print(change)
    elif a[i]<a[i-1]:
        change[1] = True
        i += 1
        #print(change)
    else:
        i += 1
        #print(change)
if i==n and change==[True,True]:
    ans += 1
print(ans)

Submission status: Accepted

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

before = A[0]
is_ascending = None
ans = 0
for a in A:
    d = a - before
    if d == 0:
        pass
    else:
        if is_ascending == None:
            is_ascending = d > 0
        elif (is_ascending and d < 0) or (not is_ascending and d > 0):
            ans += 1
            is_ascending = None
    
    before = a

print(ans+1)

Submission status: Accepted

Solution code:
N = int(input())
src = list(map(int,input().split()))
asc = None
ans=1
for i in range(N-1):
    #隣接項間の差
    d = src[i+1]-src[i]
        #隣接項が等しいとき
    if d==0:
        continue
    #最初または区切った直後のとき
    if asc==None:
        asc = d > 0
        continue
    #隣接項が等しくないとき:
        #ascと隣接項間の差が一致していなければ区切る
    if (asc and d<0) or (not asc and d>0):
        asc = None
        ans+=1
print(ans)

Submission status: Accepted

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

ans = 1

b= [a[0]]
if N > 1:
    for i in range(1,N):
        if a[i] != a[i-1]:
            b.append(a[i])

    if b[0] < b[1]:
        up_down = 1
    else:
        up_down = -1

i = 1
while i < len(b):
    if up_down*(b[i] - b[i-1]) < 0:
        ans += 1
        if b[i] < b[min(len(b)-1,i+1)]:
            up_down = 1
        else:
            up_down = -1
        i += 2
    else:
        i += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
state = 100
count = 1
tmp = a[0]
for i in a:
  if i == tmp:
    pass
  elif i > tmp:
    if state == 1:
      pass
    elif state == 0:
      count += 1
      state = 100
    else:
      state = 1
  else:
    if state == 0:
      pass
    elif state == 1:
      count += 1
      state = 100
    else:
      state = 0
  tmp = i
print(count)

Submission status: Accepted

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

res = 0
i = 0
while i < n:
	# sama
	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
	res += 1
	i += 1

print(res)

Submission status: Accepted

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

    res = 0
    i = 0
    while i + 1 <= 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
    		i += 1
    		res += 1
    
    print(res)
    
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N = int( input())
    A = list( map( int, input().split()))
    # if N == 1 or N == 2:
    #     print(1)
    #     return
    sing = 0
    now = A[0]
    ans = 1
    for a in A[1:]:
        if sing == 0:
            if now < a:
                sing = 1
            elif now > a:
                sing = -1
        elif sing > 0:
            if not ( now <= a):
                ans += 1
                sing = 0
        else:
            if not ( now >= a ):
                ans += 1
                sing = 0
        now = a
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

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

divide = 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

    divide += 1
    i += 1

print(divide)

Submission status: Accepted

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

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n = int(input())
    A = readInts()
    cnt = 0
    i = 0
    while i<n:
        while i+1 < n:
            if A[i] == A[i+1]:
                i += 1
            else:
                break
        if i+1 < n:
            if A[i] > A[i+1]:
                while i+1 < n:
                    if A[i] >= A[i+1]:
                        i += 1
                    else:
                        break
            elif A[i] < A[i+1]:
                while i+1 < n:
                    if A[i] <= A[i+1]:
                        i += 1
                    else:break
        cnt += 1
        i += 1
    print(cnt)
if __name__ == '__main__':
  main()

Submission status: Accepted

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

for i in range(1,n):
    temp = a[i] - a[i-1]
    if x == 0:
        x = temp
    elif x * temp<0:
        cnt += 1
        x =0      

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())

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

ans = 1
inc = None
for i in range(N-1):
    if inc is None:
        if A[i] != A[i+1]:
            inc = A[i] < A[i+1]
            continue

    if inc:
        if A[i] > A[i+1]:
            ans += 1
            inc = None
    else:
        if A[i] < A[i+1]:
            ans += 1
            inc = None

print(ans)

Submission status: Accepted

Solution code:
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt, ceil
from functools import lru_cache, reduce
INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)

# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

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

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# 2数の最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

# リストの要素の最小公倍数
def lcm_list(numbers):
    return reduce(lcm, numbers, 1)

# リストの要素の最大公約数
def gcd_list(numbers):
    return reduce(gcd, numbers)

# 素数判定
def is_prime(n):
    if n <= 1:
        return False
    p = 2
    while True:
        if p ** 2 > n:
            break
        if n % p == 0:
            return False
        p += 1
    return True


# limit以下の素数を列挙
def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P

# 同じものを含む順列
def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        # set（集合）型で重複を削除、ソート
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret


# ここから書き始める
n = int(input())
a = list(map(int, input().split()))
ans = 1
up = None
for i in range(1, n):
    if a[i] == a[i - 1]:
        continue
    if up == None:
        if a[i] > a[i - 1]:
            up = True
        else:
            up = False
        continue
    if a[i] > a[i - 1]:
        if up == None:
            up = True
        elif up == False:
            ans += 1
            up = None
    else:
        if up == None:
            up = False
        elif up == True:
            ans += 1
            up = None
    # print("i = %d, ans = %d" % (i, ans), up) 
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=1
now=a[0]
start=1

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

while now==a[start]:
    start+=1
if a[0]<a[start]:#増加
    jugh=1
else:#現象
    jugh=2
#print(start,jugh)
i=start
while i<n:
    if jugh==0:
        while now==a[start]:
            start+=1
        if now<a[start]:#増加
            jugh=1
        else:#現象
            jugh=2
    if jugh==1:
        if a[i-1]>a[i]:
            jugh=0
            ans+=1
            now=a[i]
            start=i+1
            #print(a[i-1],a[i])
                
    else:
        if a[i-1]<a[i]:
            jugh=0
            ans+=1
            now=a[i]
            start=i+1
            #print(a[i-1],a[i])
            
    i+=1   

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
if N == 1 or N == 2:
    print(1)
    exit()
else:
    ans = 1
    if A[1] - A[0] > 0:
        inc = 1
    elif A[1] - A[0] == 0:
        inc = 0
    else:
        inc = -1
    for i in range(2, N):
        if inc == 1:
            if A[i] - A[i - 1] >= 0:
                pass
            else:
                ans += 1
                inc = 0
        elif inc == 0:
            if A[i] - A[i - 1] > 0:
                inc = 1
            elif A[i] - A[i - 1] == 0:
                inc = 0
            else:
                inc = -1
        else:
            if A[i] - A[i - 1] <= 0:
                pass
            else:
                ans += 1
                inc = 0
print(ans)

Submission status: Accepted

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

mode="?"
cnt=0
last=a[0]
for num in a:
    if mode=="a":
        if last > num:
            cnt+=1
            mode="?"
    elif mode=="d":
        if last < num:
            cnt+=1
            mode="?"
    else:
        if last > num:
            mode="d"
        if last < num:
            mode="a"
    last=num
print(cnt+1)

Submission status: Accepted

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

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n = int(input())
    A = readInts()
    res = 0
    # 同じおなじやつは同じ奴でずっと続く
    i = 0
    while i < n:
        # 同じもの
        while i+1 < n:
            if A[i] == A[i+1]:
                i += 1
            else:
                break
        # 無くなったら、
        # もし先に上のものを見つけたら、ずっと上を探索する
        if i+1 < n:
            if A[i] < A[i+1]:
                while i+1 < n:
                    if A[i] <= A[i+1]:
                        i+=1
                    else:
                        break
        # どちらかなので、elifで。
            elif i + 1 < n:
                if A[i] > A[i+1]:
                    while i + 1 < n:
                        if A[i] >= A[i+1]:
                            i += 1
                        else:
                            break
        # ここで1ループは終わりなので、カウントに+1する
        #print(i)
        res += 1
        i += 1
    print(res)
if __name__ == '__main__':
  main()

Submission status: Accepted

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

print(ans+1)

Submission status: Accepted

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

Submission status: Accepted

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