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()))
result = 1
delta = 0
for i in range(1, n):
    if a[i - 1] < a[i]:
        ndelta = +1
    elif a[i - 1] > a[i]:
        ndelta = -1
    else:
        ndelta = 0
    if delta and ndelta and delta != ndelta:
        result += 1
        delta = 0
    elif not delta:
        delta = ndelta
print(result)

Submission status: Accepted

Solution code:
def solve(n, a):
    ans = 1
    b = [a[i+1] - a[i] for i in range(n-1)]
    c = list(filter(lambda _: _ != 0, b))
    i = 0
    m = len(c)
    while i < m-1:
        if c[i] * c[i+1] < 0:
            ans += 1
            i += 1
        i += 1
    return ans

n = int(input())
a = list(map(int, input().split()))
print(solve(n, a))

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
if(n<=2):
    print(1)
    quit()
h=0
ans=1
for i in range(n-1):
    if((l[i+1]-l[i])*h<0):
        ans+=1
        h=0
        continue
    if(l[i+1]-l[i]<0):
        h=-1
    elif(l[i+1]-l[i]>0):
        h=1
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
status=0#0:不明、1:増加、2:減少
count=0
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
        else:
            pass
    elif status==1:
        if A[i-1]>A[i]:
            count+=1
            status=0
    else:
        if A[i-1]<A[i]:
            count+=1
            status=0
print(count+1)

Submission status: Accepted

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

result = 0
increase = None

for i in range(1, n):
    if a[i] < a[i - 1]:
        if increase is None:
            increase = False
        elif increase:
            result += 1
            increase = None
    elif a[i] > a[i - 1]:
        if increase is None:
            increase = True
        elif not increase:
            result += 1
            increase = None
result += 1
print(result)

Submission status: Accepted

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

ans = 1
updown = False
up = False

for i in range(N-1):
    if updown:
        if (up and A[i] > A[i+1]) or (not up and A[i] < A[i+1]):
            ans += 1
            updown = False
            up = False
    else:
        if A[i] < A[i+1]:
            updown = True
            up = True
        elif A[i] > A[i+1]:
            updown = True

print(ans)

Submission status: Accepted

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

#単調増加か単調減少かを表す、1で増加、0でどちらでもない、-1で減少
flag = 0
ans = 1
for i in range(n-1):
  #print(a[i+1] - a[i], flag)
  #これまで増加でも減少でもない場合
  if flag == 0:
    if a[i+1] - a[i] > 0:
      #増加
      flag = 1
    elif a[i+1] - a[i] == 0:
      continue
    elif a[i+1] - a[i] < 0:
      # 減少
      flag = -1
  elif flag == 1:
    # これまで非減少だった場合
    if a[i+1] - a[i] >= 0:
      continue
    else:
      ans += 1
      flag = 0
  elif flag == -1:
    #これまで非増加
    if a[i+1] - a[i] <= 0:
      continue
    else:
      ans += 1
      flag = 0
  
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
*A, = map(int, input().split())
ans = 0
mode = 0 # neutral
for i in range(1, N):
    if A[i] - A[i-1] > 0:
        if mode == 2:
            ans += 1
            mode = 0
        elif mode == 0:
            mode = 1 # non-decreasing
    elif A[i] - A[i-1] < 0:
        if mode == 1:
            ans += 1
            mode = 0
        elif mode == 0:
            mode = 2 # non-increasing
ans += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
seq_len = int(input())
seq_a = [int(x) for x in input().split()]
if seq_len == 1:
    print(1)
    exit()
b = 0; c = 0; ans = 1
for i in range(seq_len-1):
    if seq_a[i] < seq_a[i+1]:
        b = 1
    if seq_a[i] > seq_a[i+1]:
        c = 1
    if b == 1 and c == 1:
        ans += 1
        b = 0 ; c = 0
print(ans)

Submission status: Accepted

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

ans = 1

flag = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

cnt = 1
flag = 0

for i in range(1,N):
    if flag == 0:
        if A[i-1] < A[i]:
            flag = 1
        elif A[i-1] > A[i]:
            flag = -1
    if flag == 1:
        if A[i-1] > A[i]:
            flag = 0
            cnt += 1
    if flag == -1:
        if A[i-1] < A[i]:
            flag = 0
            cnt +=1

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
inc = False
dec = False
ans = 1

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

print(ans)

Submission status: Accepted

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

incFrag = False
decFrag = False

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

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

Submission status: Accepted

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

#copy

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
# 0: 1:増加 2:減少
state = 0
count = 1
for i in range(1, n):
  if state == 0:
    if a[i-1] < a[i]:
      state = 1
    elif a[i-1] > a[i]:
      state = 2
  elif state == 1:
    if a[i-1] > a[i]:
      state = 0
      count += 1
  else:
    if a[i-1] < a[i]:
      state = 0
      count += 1
print(count)

Submission status: Accepted

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

conti_flg = 0  # {-1: '-', 0: '=', 1: '+'}
fst = A[0]
for i in A:
    snd = i
    if fst > snd:
        conti_flg = -1 if conti_flg <= 0 else 2
    elif fst < snd:
        conti_flg = 1 if conti_flg >= 0 else 2

    if conti_flg == 2:
        count += 1
        conti_flg = 0
    fst = snd

print(count)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
cnt,st,d = 0,-1,0
for i in range(n):
  if st < 0:
    st = i
    continue
  elif d == 0:
    if a[i]-a[st] != 0:
      r = i
      d = a[r]-a[st]
    continue
  else:
    if (a[i]-a[i-1])*d < 0:
      cnt += 1
      st,d = i,0
if st != -1:
  cnt += 1
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from numpy import sign

n = int(input())
a = list(map(int,input().split()))
b = [sign(a[i+1]-a[i]) for i in range(n-1)]

c = 1
f = 0
for t in b:
  if f == 0:
    f = t
  elif f*t == -1:
    c += 1
    f = 0
print(c)

Submission status: Accepted

Solution code:
from numpy import sign

n = int(input())
a = list(map(int,input().split()))
b = [sign(a[i+1]-a[i]) for i in range(n-1)]

c = 1
f = 0
for t in b:
  if f == 0:
    f = t
  elif f == 1 and t == -1:
    c += 1
    f = 0
  elif f == -1 and t == 1:
    c += 1
    f = 0
print(c)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
cnt=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
    elif flag==1:
        if a[i]>a[i+1]:
            cnt+=1
            flag=0
    else:
        if a[i]<a[i+1]:
            cnt+=1
            flag=0
print(cnt)

Submission status: Accepted

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

    flag = 'flat'

    counter = 1

    for i in range(1, N):
        old = A[i-1]
        new = A[i]

        if flag == 'inc':
            if old <= new:
                continue
            else:
                counter += 1
                flag = 'flat'

        elif flag == 'dec':
            if old >= new:
                continue
            else:
                counter += 1
                flag = 'flat'
        else:
            if old < new:
                flag = 'inc'
            elif old > new:
                flag = 'dec'
            else:
                continue

    print(counter)

Submission status: Accepted

Solution code:
_ = int(input())
A = [int(a) for a in input().split()]
selector = 0
count = 1
pre_a = A[0]
for a in A[1:]:
    if selector == 0:
        selector = a - pre_a
    elif selector * (a - pre_a) < 0:
        count += 1
        selector = 0
    pre_a = a
print(count)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
import string
import sys
from itertools import chain, dropwhile, takewhile


def read(
    *shape, f=int, it=chain.from_iterable(sys.stdin), whitespaces=set(string.whitespace)
):
    def read_word():
        w = lambda c: c in whitespaces
        nw = lambda c: c not in whitespaces
        return f("".join(takewhile(nw, dropwhile(w, it))))

    if not shape:
        return read_word()
    elif len(shape) == 1:
        return [read_word() for _ in range(shape[0])]
    elif len(shape) == 2:
        return [[read_word() for _ in range(shape[1])] for _ in range(shape[0])]


def readi(*shape):
    return read(*shape)


def readi1(*shape):
    return [i - 1 for i in read(*shape)]


def readf(*shape):
    return read(*shape, f=float)


def reads(*shape):
    return read(*shape, f=str)


def arr(*shape, fill_value=0):
    if len(shape) == 1:
        return [fill_value] * shape[fill_value]
    elif len(shape) == 2:
        return [[fill_value] * shape[1] for _ in range(shape[0])]


def dbg(**kwargs):
    print(
        ", ".join("{} = {}".format(k, repr(v)) for k, v in kwargs.items()),
        file=sys.stderr,
    )


def main():
    N = readi()
    a = readi(N)
    ans = 1
    c = a[0]
    g, z = False, False
    for i in range(1, N):
        if c < a[i] and g:
            ans += 1
            g, z, = False, False
        elif c < a[i] and not g:
            z = True
        elif c > a[i] and z:
            ans += 1
            g, z, = False, False
        elif c > a[i] and not z:
            g = True
        c = a[i]

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = [int(s) for s in input().split()]
inc_flag = 0
dec_flag = 0
ans = 1
for i in range(N-1):
    if  A[i] < A[i+1]:
        if dec_flag == 1:
            ans += 1
            inc_flag = 0
            dec_flag = 0
        else:
            inc_flag = 1
    elif A[i] > A[i+1]:
        if inc_flag == 1:
            ans += 1
            inc_flag = 0
            dec_flag = 0
        else:
            dec_flag = 1
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
A - Sorted Arrays
https://atcoder.jp/contests/agc013/tasks/agc013_a

"""
import sys


def solve(N, array):
    ans = 1
    prev = None
    direction = None

    for a in array:
        if prev == None:
            pass
        elif a > prev:
            if direction == 'd':
                ans += 1
                direction = None
            else:
                if a > prev:
                    direction = 'u'
        elif a < prev:
            if direction == 'u':
                ans += 1
                direction = None
            else:
                if a < prev:
                    direction = 'd'
        prev = a
    return ans


def main(args):
    N = int(input())
    array = [int(a) for a in input().split()]
    ans = solve(N, array)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
sub = 0
count = 1
A = [int(i) for i in input().split()]
if N > 1:
	pre_num = A[1]
	pre_sub = A[1] - A[0]
for num in A[2:]:
    if not pre_num == num:
        if pre_sub * (num - pre_num) < 0:
            count += 1
            pre_sub = 0
        else:
          	pre_sub = num - pre_num
        pre_num = num
print(count)

Submission status: Accepted

Solution code:
N = int(input())
selector = 0
count = 1
A = [int(i) for i in input().split()]
pre_num = A[0]
for num in A[1:]:
    if selector == 0:
        selector = num - pre_num
    if selector > 0:
        if num < pre_num:
            selector = 0
            count += 1
    if selector < 0:
        if num > pre_num:
            selector = 0
            count += 1
    pre_num = num
print(count)

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
A = deque([int(i) for i in input().split()])
dec = False
inc = False
cnt = 1
antenum = A.popleft()

for _ in range(N - 1):
    if not inc and not dec:
        if A[0] < antenum:
            dec = True
        elif A[0] > antenum:
            inc = True
    elif inc:
        if A[0] < antenum:
            cnt += 1
            inc = False
    elif dec:
        if A[0] > antenum:
            cnt += 1
            dec = False
    antenum = A.popleft()

print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def calc(a_cur, a_prev, vec, ans):
    if a_cur == a_prev:
        pass
    elif a_cur > a_prev:
        if vec == -1:
            ans += 1
            vec = 0
        else:
            vec = 1

    elif a_cur < a_prev:
        if vec == 1:
            ans += 1
            vec = 0
        else:
            vec = -1

    return a_cur, vec, ans

def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    ans = 0
    vec = 0
    a_prev = a[0]
    for i in range(1, n):
        a_cur = a[i]
        a_prev, vec, ans = calc(a_cur, a_prev, vec, ans)

    print(ans+1)

main()

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
n=1
mode=0
for i in range(1,N):
	if mode==0:
		mode = A[i]-A[i-1]
	else:
		if mode * (A[i]-A[i-1]) >=0:
			continue
		else:
			n+=1
			mode=0
print(n)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split(" ")))
inc = 0
dec = 0
s = 1
for i in range(N-1):
    inc += A[i] < A[i+1]
    dec += A[i] > A[i+1]
    if inc and dec:
        s += 1
        inc = 0
        dec = 0
print(s)

Submission status: Accepted

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

D = [A[i + 1] - A[i] for i in range(N - 1)]

pre = 0
cnt = 1
for da in D:
    if pre == 0:
        pre = da
    elif pre * da < 0:
        pre = 0
        cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
N=int(input())
s=list(map(int,input().split(" ")))
r=1
before=None
if N<=2:
  pass
else:
  flag=None
  for i,z in enumerate(s):
    if flag==None:
      if before == None:
        before=z
      if before<z:
        flag=True
        before=z
      elif before>z:
        flag=False
        before=z
    elif flag==True:
      if z<before:
        flag=None
        before=z
        r+=1
      else:
        before=z
    else:
      if z>before:
        flag=None
        before=z
        r+=1
      else:
        before=z

print(r)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
A=[int(i) for i in input().split()]
slope=0
count=1
for i in range(1,n):
  d=A[i]-A[i-1]
  if slope==0:
    slope=d
  elif slope*d<0:
    count+=1
    slope=0
print(count)

Submission status: Accepted

Solution code:
n=int(input())
arr=list(map(int,input().split()))
cnt=0
flag1=False
flag2='x'
for i in range(n-1):
  if arr[i]<arr[i+1]:
    if flag1==False:
      flag1=True
      flag2='p'
    if flag2=='n':
      flag1=False
      flag2='x'
      cnt+=1
  if arr[i]>arr[i+1]:
    if flag1==False:
      flag1=True
      flag2='n'
    if flag2=='p':
      flag1=False
      flag2='x'
      cnt+=1
print(cnt+1)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
ans = 1
length = 1
k = 0
for i in range(1,n):
    if k==0:
        if a[i]>a[i-1]:
            k = 2
        elif a[i]<a[i-1]:
            k = 1
        else:
            continue
    if k==2 and a[i]>=a[i-1] or k==1 and a[i]<=a[i-1]:
        continue
    k = 0
    ans += 1
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())
A = list(map(int, input().split()))
ans = 1
flag = 0
sa = A[0]

for a in A:
    if a > sa:
        if flag != 2 :
            flag = 1
        else:
            ans += 1
            flag = 0
            
    elif a < sa:
        if flag != 1:
            flag = 2
        else:
            ans += 1
            flag = 0
    
    sa = a

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())

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

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

Submission status: Accepted

Solution code:

def splitter(n, a):
    count = 1
    mode = 'none'
    for i in range(1, n):
        if mode == 'incr':
            if a[-i-1] > a[-i]:
                # print('{} | {}'.format(a[-i-1], a[-i:]))
                count += 1
                # print('count:', count)
                mode = 'none'
            else:
                continue
        elif mode == 'decr':
            if a[-i-1] < a[-i]:
                # print('{} | {}'.format(a[-i-1], a[-i:]))
                count += 1
                # print('count:', count)
                mode = 'none'
        else:
            if a[-i-1] == a[-i]:
                # print('{} = {}'.format(a[-i-1], a[-i:]))
                continue
            elif a[-i-1] > a[-i]:
                # print('{} > {}'.format(a[-i-1], a[-i:]))
                mode = 'decr'
            elif a[-i-1] < a[-i]:
                # print('{} > {}'.format(a[-i-1], a[-i:]))
                mode = 'incr'
    return count


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

    print(splitter(n, a))


main()

Submission status: Accepted

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

Submission status: Accepted

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

trend = 0
groups = 1

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

Submission status: Accepted

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

total = 1
trend = 0
prev = A[0]
for a in A[1:]:
    if a > prev and trend < 0:
        total += 1
        trend = 0
    elif a < prev and trend > 0:
        total += 1
        trend = 0
    elif trend == 0:
        trend = a - prev
    prev = a

print(total)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    As = list(map(int, input().split()))
    ct = 1
    inc = False
    dec = False
    bef = As.pop(0)

    for Ai in As:
        if (not inc) and (not dec):
            if Ai < bef:
                dec = True
            elif Ai > bef:
                inc = True
        elif inc:
            if Ai < bef:
                ct += 1
                inc = False
        elif dec:
            if Ai > bef:
                ct += 1
                dec = False
        bef = Ai
    
    print(ct)

main()

Submission status: Accepted

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

Submission status: Accepted

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

prev=a[0]
state=0 # 0:判断がつかない 1:単調増加 2:単調減少

ans=1

for cur in a:
  #この２パターンのときのみ、区切ることができる。
  if state==1 and prev>cur:
    ans+=1
    state=0
  elif state==2 and prev<cur:
    ans+=1
    state=0
  else:
    if prev>cur:
      state=2
    elif prev<cur:
      state=1
    #5 5など並んでいたもののみを見てもstateは決められない、ゆえに何もしない。
    #前の方でstateが決まっていてそれが続いている
    #または、
    #この時点では分からず後ろを見ていくとわかるの2パターンがあり得る
  prev=cur

print(ans)
#正確に問題を表現。

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
d = [0 for _ in range(n-1)]
for i in range(n-1):
    d[i] = a[i+1] - a[i]
cnt = 1
l = r = cur = 0
while l < n-1:
    while r < n-1 and cur * d[r] >= 0:
        if cur == 0:
            cur = d[r]
        r += 1
    if r < n-1 and cur * d[r] < 0:
        cnt += 1
    r += 1
    l = r
    cur = 0
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
#greedyで解く
upflag = True
downflag = True
answer = 1
for i in range(N - 1):
    if A[i] < A[i + 1]:
        downflag = False
    if A[i] > A[i + 1]:
        upflag = False
    if not (upflag or downflag):
        answer += 1
        upflag = True
        downflag = True
print(answer)

Submission status: Accepted

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

count = 1
increasing = None

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

  elif increasing:
    if a[i] - a[i+1] < 0:
      increasing = None
      count += 1

  elif not increasing:
    if a[i] - a[i+1] > 0:
      increasing = None
      count += 1
print(count)

Submission status: Accepted

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

count = 0
state = 0
beforeA = As[0]
for A in As:
    if A > beforeA:
        if state == 0 or state == 1:
            state = 1
        else:
            count += 1
            state = 0
    if A < beforeA:
        if state == 0 or state == 2:
            state = 2
        else:
            count += 1
            state = 0
    beforeA = A

print(count+1)

Submission status: Accepted

Solution code:
n = int(input())
s = map(int, input().split())
s = list(s)
num = 0
ASC, DES, EQ, UND = 0, 1, 2, 3
state = UND
if len(s) <= 2:
    print(1)
    exit()
i = s[0]
for j in s[1:]:
    if state == UND:
        if i > j:
            state = DES
        elif i < j:
            state = ASC
        else:
            state = EQ
        num += 1
    elif state == EQ:
        if i > j:
            state = DES
        elif i < j:
            state = ASC
    elif state == DES:
        if i >= j:
            pass
        else:
            state = UND
    else:
        if i <= j:
            pass
        else:
            state = UND
    i = j
if state == UND:
    num += 1

print(num)

Submission status: Accepted

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

I = [x>y for x,y in zip([0]+A,A+[10**10])]          
D = [x<y for x,y in zip([10**10]+A,A+[0])]          

ci = False
cd = False
ans = 1
for i in range(N+1): 
    ci += I[i]
    cd += D[i]
    if ci and cd:
        ans += 1
        ci = False
        cd = False
print(ans)

Submission status: Accepted

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

flag = False
isIncrease = True
count = 1

for i in range(len(A) - 1) :
    if not flag :
        if A[i] < A[i + 1] :
            isIncrease = True
            flag = True
        elif A[i] > A[i + 1] :
            isIncrease = False
            flag = True
    else :
        if A[i] > A[i + 1] :
            if isIncrease :
                count += 1
                flag = False
        elif A[i] < A[i + 1] :
            if not isIncrease :
                count += 1
                flag = False

print(count)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solution(num, list):
    flag = "default"
    count = 1
    for i in range(num - 1):
        diff = l[i+1] - l[i]
        if flag == "plus":
            if diff < 0:
                flag = "default"
                count += 1
        elif flag == "minus":
            if diff > 0:
                flag = "default"
                count += 1
        else:
            if diff > 0:
                flag = "plus"
            elif diff < 0:
                flag = "minus"
    return count

if __name__ == "__main__":
    n = int(input())
    l = list(map(int, input().split()))
    print(solution(n, l))