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()))
t=1
p=a[0]
m=0
for i in a:
  if(p==i):
    continue
  if(m>0):
    if(i<p):
      t+=1
      m=0
  elif(m<0):
    if(i>p):
      t+=1
      m=0
  else:
    if(i>p):
      m=1
    elif(i<p):
      m=-1
  p=i
print(t)

Submission status: Accepted

Solution code:
N = int(input())

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

ans = 1

now = a[0]

f = 0

for i in range(1, N):
    if now == a[i]:
        now = a[i]
    elif a[i]>now:
        if f == 0:
            f = 1
            now = a[i]
        elif f == 1:
            now = a[i]
        else:
            f = 0
            ans = ans + 1
            now = a[i]
    else:
        if f == 0:
            f = -1
            now = a[i]
        elif f == -1:
            now = a[i]
        else:
            f = 0
            ans = ans + 1
            now = a[i]

        

    
print(ans)

Submission status: Accepted

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

flag = -1
ans = 1

for i in range(1, N):
    first = inputs[i-1]
    latter = inputs[i]
    if flag == -1:
        if first < latter:
            flag = 1
        elif first > latter:
            flag = 0
    elif flag == 1:
        if first > latter:
            flag = -1
            ans += 1
    else:
        if first < latter:
            flag = -1
            ans += 1

print(ans)

Submission status: Accepted

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

start = inputs[0]
flag = 0
ans = 1

for i in range(1, N):
    if flag == 0:
        if start < inputs[i]:
            flag = 1
        elif start > inputs[i]:
            flag = -1
    elif flag == 1:
        if start > inputs[i]:
            flag = 0
            ans += 1
    else:
        if start < inputs[i]:
            flag = 0
            ans += 1
    start = inputs[i]

print(ans)

Submission status: Accepted

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

start = inputs[0]
flag = 0
ans = 1

for i in range(1, N):
    if flag == 0:
        if start < inputs[i]:
            flag = 1
        elif start > inputs[i]:
            flag = -1
        start = inputs[i]
    elif flag == 1:
        if start > inputs[i]:
            flag = 0
            ans += 1
        start = inputs[i]
    else:
        if start < inputs[i]:
            flag = 0
            ans += 1
        start = inputs[i]

print(ans)

Submission status: Accepted

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

orient=0
count=0
prev=A[0]

for i in range(1,N):
    if A[i]<prev:
        if orient==1:
            count+=1
            prev=A[i]
            orient=0
        elif orient==-1:
            prev=A[i]
        else:
            orient=-1
            prev=A[i]
    elif A[i]>prev:
        if orient==-1:
            count+=1
            prev=A[i]
            orient=0
        elif orient==1:
            prev=A[i]
        else:
            orient=1
            prev=A[i]

       
print(count+1)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
cond = 0
# 1: 上昇, -1: 減少, 0: 一緒
res = 1
for i in range(1,len(a)):
    if a[i] > a[i-1]:
        # 上昇
        if cond != 1:
            if cond == -1:
                res += 1
                cond = 0
            else:
                cond = 1
    elif a[i] < a[i-1]:
        # 減少
        if cond != -1:
            if cond == 1:
                res += 1
                cond = 0
            else:
                cond = -1
print(res)

Submission status: Accepted

Solution code:
# coding:utf-8


def inpl(): return list(map(int, input().split()))


N = int(input())
A = inpl()

ans = 1
prev = A[0]
state = 0  # 1: 増加, 0: 横ばい, -1: 減少
for i in range(1, N):
    if state == 0:
        if A[i] > prev:
            state = 1
        elif A[i] < prev:
            state = -1

    elif state == 1:
        if A[i] < prev:
            ans += 1
            state = 0

    else:
        if A[i] > prev:
            ans += 1
            state = 0

    prev = A[i]

print(ans)

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
from collections import Counter


# input = sys.stdin.readline
INF = float('inf')
MOD = 10 ** 9 + 7


def inpl(): return list(map(int, input().split()))


N = int(input())
A = inpl()

ans = 1
prev = A[0]
state = 0
for i in range(1, N):
    if state == 0:
        if A[i] > prev:
            state = 1
        elif A[i] < prev:
            state = -1

    elif state == 1:
        if A[i] < prev:
            ans += 1
            state = 0

    else:
        if A[i] > prev:
            ans += 1
            state = 0

    prev = A[i]

print(ans)

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
from collections import Counter


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


def inpl(): return list(map(int, input().split()))


N = int(input())
A = inpl()

ans = 1
prev = A[0]
state = 0
for i in range(1, N):
    if state == 0:
        if A[i] > prev:
            state = 1
        elif A[i] < prev:
            state = -1

    elif state == 1:
        if A[i] < prev:
            ans += 1
            state = 0

    else:
        if A[i] > prev:
            ans += 1
            state = 0

    prev = A[i]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
state = 0
count = 1
for i in range(1,N):
    note = state
    d = A[i] - A[i-1]
    if d > 0:
        state = 1
    elif d == 0:
        pass
    elif d < 0:
        state = -1
    if (state,note) == (1,-1) or (state,note) == (-1,1):
        count += 1
        state = 0
print(count)

Submission status: Accepted

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

base=A[0]
cnt=1
mode="N"
for i in range(N):
    if mode=="N":
        if A[i]>base:
            mode="P"
        elif A[i]<base:
            mode="M"
        else:
            pass
    elif mode=="P":
        if A[i]>=A[i-1]:
            pass
        else:
            mode="N"
            cnt+=1
            base=A[i]
    elif mode=="M":
        if A[i]<=A[i-1]:
            pass
        else:
            mode="N"
            cnt+=1
            base=A[i]
print(cnt)

Submission status: Accepted

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

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

i = 0
tmp = []
while i < N:

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

        i += 1
    else:
        if pm*(A[i] - A[i-1]) >= 0:
            i += 1
            if pm == 0 and A[i] != A[i-1]:
                if A[i-1] < A[i]:
                    pm = 1
                else:
                    pm = -1
        else:
            i += 1
            if N <= i:
                ans += 1
                break

            if A[i-1] == A[i]:
                pm = 0
            elif A[i-1] < A[i]:
                pm = 1
            else:
                pm = -1

            ans += 1


print(ans)

Submission status: Accepted

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


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

    counter = 1
    flag, last = 0, a[0]
    for i in range(n-1):
        temp = a[i+1]
        if flag == 0:
            if temp == last:
                pass
            elif temp > last:
                flag = 1
            elif temp < last:
                flag = -1

        if flag == 1:
            if temp == last:
                pass
            elif temp > last:
                pass
            elif temp < last:
                counter += 1
                flag = 0

        if flag == -1:
            if temp == last:
                pass
            elif temp > last:
                counter += 1
                flag = 0
            elif temp < last:
                pass

        last = temp

    print(counter)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

flag = None
s = 1
for i, x in enumerate(numbers):
    if i == 0:
        continue
    if x == numbers[i-1]:
        continue
    if x > numbers[i-1]:
        if flag is None:
            flag = 1
        if flag == 0:
            s += 1
            flag = None
    elif x < numbers[i-1]:
        if flag is None:
            flag = 0
        if flag == 1:
            s += 1
            flag = None
print(s)

Submission status: Accepted

Solution code:

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)

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


def solve(i, ret, t):
    if i == N:
        return ret
    else:
        if t == '?':
            if A[i] > A[i - 1]:
                return solve(i + 1, ret, 'up')
            elif A[i] < A[i - 1]:
                return solve(i + 1, ret, 'down')
            else:
                return solve(i + 1, ret, '?')

        elif t == 'up' and A[i] < A[i - 1]:
            return solve(i + 1, ret + 1, '?')
        elif t == 'down' and A[i] > A[i - 1]:
            return solve(i + 1, ret + 1, '?')
        else:
            return solve(i + 1, ret, t)


print(solve(1, 1, '?'))

Submission status: Accepted

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

Submission status: Accepted

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

increase = False
decrease = False
ans = 1
for i in range(N-1):
    dif = a[i+1]-a[i]
    if dif>0:
        if not decrease:
            increase = True
        else:
            ans += 1
            increase, decrease = False, False
    if dif<0:
        if not increase:
            decrease = True
        else:
            ans += 1
            increase, decrease = False, False
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import pdb


def main():
    num = int(input())
    array = []
    tmp = input()
    tmp = tmp.split()
    for i in range(num):
        array.append(int(tmp[i]))
        
    ans = 1
    div = 0
    prev = 0
    count = 0
    #pdb.set_trace()
    for tmp in array:
        count += 1
        if count == 1:
            prev = tmp
            continue

        if div == 0:
            if prev < tmp:
                div = 1
            elif prev > tmp:
                div = -1
        else:
            if prev < tmp and div == -1:
                ans += 1
                div = 0
            elif prev > tmp and div == 1:
                ans += 1
                div = 0
                
        prev = tmp
        
    print(ans)

        
if __name__ == '__main__':
    main()

Submission status: Accepted

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

# 単調非減少か単調非増加
# 単調非減少 = 上昇 or 0
# 単調非増加 = 減少 or 0

up = False
down = False
count = 0
i = 1
while i < len(a):
    # print("\nup:{} down:{}".format(up, down))
    if a[i] - a[i - 1] > 0 and down:
        # print("+counted a[{}]:{} a[{}]:{}".format(i, a[i], i-1, a[i-1]))
        up = False
        down = False
        count += 1
    elif a[i] - a[i - 1] < 0 and up:
        # print("-counted a[{}]:{} a[{}]:{}".format(i, a[i], i - 1, a[i - 1]))
        up = False
        down = False
        count += 1
    elif a[i] - a[i-1] > 0:
        up = True
    elif a[i] - a[i-1] < 0:
        down = True
    i += 1
print(count + 1)

Submission status: Accepted

Solution code:
N = int(input())
A = ['head']
for x in map(int, input().split()):
    if A[-1] != x:
        A.append(x)
if len(A) == 2:
    print(1)
    exit()
cnt = 1
inc = A[1] < A[2]
skip = False
for x, y in zip(A[2:], A[3:]):
    if skip:
        skip = False
        inc = x < y
        continue
    if inc != (x < y):
        skip = True
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(_) for _ in input().split()]
increasing = False
decreasing = False
count = 1
for i in range(N - 1):
    if A[i] < A[i + 1]:
        if decreasing:
            count += 1
            increasing = False
            decreasing = False
        else:
            increasing = True
    elif A[i] > A[i + 1]:
        if increasing:
            count += 1
            increasing = False
            decreasing = False
        else:
            decreasing = True
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys


def main() -> None:
    way = 0
    pre = 0
    ans = 1

    n = next_int()

    for i in range(n):
        now = next_int()
        if i == 0 or now - pre == 0:
            pass
        elif way == 0:
            way = sgn(now - pre)
        elif sgn(now - pre) != way:
            ans += 1
            way = 0

        pre = now

    print(ans)


def sgn(x: int) -> int:
    if x == 0:
        return 0
    if x > 0:
        return 1
    return -1

def next_int() -> int:
    return int(next_str())


def next_str() -> str:
    result = ""
    while True:
        tmp = sys.stdin.read(1)
        if tmp.strip() != "":
            result += tmp
        elif tmp != '\r':
            break
    return result


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
res = 1
cur = None
np = None
for a in list(map(int, input().split())):
    # print(res,cur,np,a)
    if cur is None:
        pass
    elif np is None:
        if a==cur:
            np = 0
        elif a>cur:
            np = 1
        else:
            np = -1
    else:
        if np==0:
            if a==cur:
                pass
            elif a>cur:
                np = 1
            else:
                np = -1
        elif np>0:
            if a>=cur:
                pass
            else:
                res += 1
                np = None
        else:
            if a<=cur:
                pass
            else:
                res += 1
                np = None
    cur = a

print(res)

Submission status: Accepted

Solution code:

def main():
    buf = input()
    N = int(buf)
    buf = input()
    buflist = buf.split()
    A = list(map(int, buflist))
    split_count = 1
    increasing = None
    for i in range(1, N):
        if increasing == None:
            if A[i-1] < A[i]:
                increasing = True
            elif A[i-1] > A[i]:
                increasing = False
            # unsure if A[i-1] == A[i]
        elif increasing and A[i-1] > A[i]:
            split_count += 1
            increasing = None # unsure if it increases or not at this point
        elif not increasing and A[i-1] < A[i]:
            split_count += 1
            increasing = None # unsure if it increases or not at this point
    print(split_count)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def inpl(): return [int(i) for i in input().split()]

N = int(input())
A = inpl()
ans = 1
first = True
for i in range(N-1):
    if first:
        if A[i+1] == A[i]:
            continue
        else:
            c = (A[i+1]-A[i])//abs((A[i+1]-A[i]))
            first = False
            continue
    if c*A[i] > c*A[i+1]:
        ans += 1
        first = True
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

arrays = 1
ratio = ""
for i in range(1, N):
    if ratio == "+":
        if A[i] < A[i-1]:
            arrays += 1
            ratio = ""

    elif ratio == "-":
        if A[i] > A[i-1]:
            arrays += 1
            ratio = ""
    else:
        if A[i] > A[i-1]:
            ratio = "+"
        elif A[i] < A[i-1]:
            ratio = "-"
print(arrays)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
dist = []
for a in A:
    if dist and dist[-1] == a:
        continue
    dist.append(a)
ans = 1
i = 2
while i < len(dist):
    if (dist[i-2] < dist[i-1]) != (dist[i-1] < dist[i]):
        ans += 1
        i += 1
    i += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
dist = []
for a in A:
    if dist and dist[-1] == a:
        continue
    dist.append(a)
ans = 0
next = 0
for i in range(1, len(dist)-1):
    if (dist[i-1] < dist[i]) == (dist[i] > dist[i+1]) and i != next:
        next = i + 1
        ans += 1
print(ans + 1)

Submission status: Accepted

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

for i in range(len(a)):
    if len(tmp) == 0:
        tmp.append(a[i])
    elif flag == 0:
        flag = 1 if tmp[-1] < a[i] else -1 if tmp[-1] > a[i] else 0
        tmp.append(a[i])
    elif (flag == 1 and tmp[-1] <= a[i]) or (flag == -1 and tmp[-1] >= a[i]):
        tmp.append(a[i])
    else:
        ans.append(tmp)
        tmp = [a[i]]
        flag = 0

print(len(ans) + 1)

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 1
prev = -1
dir = 0

for i in range(N):
  if prev == -1:
    prev = A[i]
    continue
    
  if dir == 0:
    if prev < A[i]:
      dir = 1
    elif prev > A[i]:
      dir = -1
  elif dir > 0:
    if prev <= A[i]:
      pass      
    else:
      dir = 0
      cnt += 1
  else:
    if prev >= A[i]:
      pass
    else:
      dir = 0
      cnt += 1
  prev = A[i]
  
print(cnt)

Submission status: Accepted

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

count = 1
flag = ""

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

    if flag == "":
        if d > 0:
            flag = "P"
        elif d < 0:
            flag = "M"

    elif (flag == "P" and d < 0) or (flag == "M" and d > 0):

        count += 1
        flag = ""

print(count)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int, input().split()))
ans,c,d=0,1,True
while c<n:
    if a[c]==a[c-1]:
        c+=1
        continue
    if d:
        f=a[c]>a[c-1]
        d=False
    elif f:
        if a[c]<a[c-1]:
            ans+=1
            d=True
    else:
        if a[c]>a[c-1]:
            ans+=1
            d=True
    c+=1
print(ans+1)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = [a[0]]
now = a[0]
for i in range(n-1):
    if now == a[i+1]:
        continue
    else:
        b.append(a[i+1])
        now = a[i+1]
n = len(b)
if n == 1:
    print(1)
    exit()
i = 0
ans = 0
while i < n-1:
    s = b[i]
    t = b[i+1]
    if s < t:
        state = 1
    else:
        state = 0
    temp = state
    while True:
        if state:
            if b[i] < b[i+1]:
                i += 1
            else:
                ans += 1
                i += 1
                break
        else:
            if b[i] > b[i+1]:
                i += 1
            else:
                ans += 1
                i += 1
                break
        if i == n-1:
            break

print(ans+1)

Submission status: Accepted

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

count = 0
c_list = []
increment = False
decrement = False

for i, a in enumerate(a_list):
    if i == 0:
        c_list.append(a)

    elif not increment and not decrement:
        if a > c_list[0]:
            c_list.append(a)
            increment = True
        elif a < c_list[0]:
            c_list.append(a)
            decrement = True
        else:
            continue
    else:
        if increment:
            if a >= c_list[-1]:
                c_list.append(a)
            else:
                c_list = [a]
                increment = False
                decrement = False
                count += 1
        else:
            if a <= c_list[-1]:
                c_list.append(a)
            else:
                c_list = [a]
                increment = False
                decrement = False
                count += 1

print(count+1)

Submission status: Accepted

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

if N == 1:
	print(1)

elif N == 2:
	print(1)

else:
	L = []
	for i in range(1, N):
		x = A[i] - A[i - 1]
		if x > 0:
			L.append("+")
		elif x == 0:
			L.append(".")
		else:
			L.append("-")

	c = 1
	judge = L[0]
	for i in range(1,N - 1):
		if L[i] == ".":
			pass
		elif L[i] == "+":
			if judge == "+" or judge == ".":
				judge = "+"
			else:
				c += 1
				judge = "."
		else:
			if judge == "-" or judge == ".":
				judge = "-"
			else:
				c += 1
				judge = "."

	print(c)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 1
flg = ''
for i in range(N - 1):
    if (flg == 'LE' and As[i] > As[i + 1]) or (flg == 'GE' and As[i] < As[i + 1]):
        ans += 1
        flg = ''
    else:
        if As[i] < As[i + 1]: flg = 'LE'
        elif As[i] > As[i + 1]: flg = 'GE'

print(ans)

Submission status: Accepted

Solution code:
import math
n=input()
a=map(int, raw_input().split())
a=[a[i]-a[i-1] for i in range(1, len(a))]
a=[value/abs(value) for value in a if value]
cur=0
result=0
for value in a:
	if cur and cur!=value:
		cur=0
		result+=1
	else:
		cur=value
print result+1

Submission status: Accepted

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

ass = 0
k = a[0]
w = 0 #zouka gennshou
for i in range(n):
    if w == -1:
        if a[i] > a[i-1]:
            ass += 1
            w = 0
        
    elif w == 1:
        if a[i] < a[i-1]:
            ass += 1
            w = 0
    if w == 0:
        if i != n-1:
            if a[i] > a[i+1]:
                w = -1
            elif a[i] < a[i+1]:
                w = 1
print(ass+1)

Submission status: Accepted

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

Submission status: Accepted

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

def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    ans = 1
    flag = 0
    for i in range(1, n):
        if a_list[i] - a_list[i-1] > 0 and flag == 0:
            flag = 1
        elif a_list[i] - a_list[i-1] < 0 and flag == 0:
            flag = 2
        if a_list[i] - a_list[i-1] > 0 and flag == 2:
            ans += 1
            flag = 0
        elif a_list[i] - a_list[i-1] < 0 and flag == 1:
            ans += 1
            flag = 0
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
list_a = list(map(int, input().split()))
state = 0
count = 1
for i in range(n-1):
    if state == 0:
        if list_a[i] == list_a[i+1]:
            continue
        elif list_a[i] < list_a[i+1]:
            state = "plus"
        else:
            state = "minus"
    if state == "plus":
        if list_a[i] > list_a[i+1]:
            count += 1
            state = 0
        else:
            continue
    if state == "minus":
        if list_a[i] < list_a[i+1]:
            count += 1
            state = 0

print(count)

Submission status: Accepted

Solution code:
n = int(input())
lis = list(map(int,input().split()))
cou = 0
ans = 1
for i in range(n-1):
  r = lis[i+1]-lis[i]
  if r * cou < 0:
    ans += 1
    cou = 0
  elif r != 0:
    cou = r
print(ans)

Submission status: Accepted

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

result = 1
d = 0
for i in range(1, n):
    diff = a[i] - a[i - 1]
    if d == 0:
        d = diff
    elif d * diff < 0:
        result += 1
        d = 0

print(result)

Submission status: Accepted

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

res = 1
flag = 0
prev = a[0]
for i in a:
    # 前と値が変わらない
    if prev==i:
        tmp = 0
    # 前より大きい
    elif prev<i:
        tmp = 1
    # 前より小さい
    else:
        tmp = -1

    if tmp==1:
        if flag==-1:
            flag = 0
            res += 1
        else:
            flag = tmp
    elif tmp==-1:
        if flag==1:
            flag = 0
            res += 1
        else:
            flag = tmp

    #print(i,res)
    prev = i

print(res)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

def li(): return [int(x) for x in stdin.readline().split()]
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return [float(x) for x in stdin.readline().split()]
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(ns())
def nf(): return float(ns())

n = ni()
a = li()

a_diff = []
ans = 1
for i in range(n-1):
    a_diff.append(a[i+1] - a[i])
    
cur = 0
for i in range(n-1):
    if cur == 0:
        if a_diff[i] == 0:
            continue
        elif a_diff[i] > 0:
            cur = +1
        else:
            cur = -1
            
    elif cur == +1:
        if a_diff[i] >= 0:
            continue
        else:
            cur = 0
            ans += 1
            
    elif cur == -1:
        if a_diff[i] <= 0:
            continue
        else:
            cur = 0
            ans += 1
            
    else:
        print("バグってます／(^o^)＼")
            
        
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
*m,=map(int,input().split())
l=[]
l=[m[0]]+[m[i] for i in range(1,n) if m[i]!=m[i-1]]

c=1
i=2

while i<len(l):
  if (l[i-2]<l[i-1] and l[i-1]>l[i]) or (l[i-2]>l[i-1] and l[i-1]<l[i]):
    c+=1
    i+=1
  i+=1

print(c)

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int, input().split()))
a = [b[0]]
for i in range(1, n):
    if a[-1] != b[i]:
        a.append(b[i])

ans = 1
inc = a[0] < a[min(1, n-1)]

n = len(a)
i = 1
while i < n:
    if (a[i-1] < a[i]) ^ inc:
        ans += 1
        inc = a[i] < a[min(i+1, n-1)]
        i += 2
    else:
        i += 1
print(ans)

Submission status: Accepted

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

t = a[0]
ge, le = False, False
i = 0
ans = 1
while i < n:
    if a[i] == t:
        pass
    if a[i] > t:
        if ge:
            pass
        else:
            if le:
                le = False
                ans += 1
            else:
                ge = True
    if a[i] < t:
        if le:
            pass
        else:
            if ge:
                ge = False
                ans += 1
            else:
                le = True
#   print(i, ge, le, ans)
    t = a[i]
    i += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(x) for x in input().split()]
# st=["non","inc","dec"]
st = "non"
ans = 1
for i in range(1, N):
    if st == "non":
        if a[i-1] < a[i]:
            st = "inc"
        if a[i-1] > a[i]:
            st = "dec"
    elif st == "inc":
        if a[i-1] > a[i]:
            st = "non"
            ans += 1
    elif st == "dec":
        if a[i-1] < a[i]:
            st = "non"
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

b = 0
ans = 1

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