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: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = 1
re = 0
for i in range(N-1):
    if A[i+1] > A[i]:
        dire = '<'
        break
    if A[i+1] < A[i]:
        dire = '>'
        break
    else:
        dire = '='
if dire != '=':
    for i in range(2,N,1):
        #print()
        
        if re == 0:
            if dire == '>':

                if A[i] <= A[i-1] <= A[i-2]:
                    re = 0
                    dire =  '>'

                else:
                    ans += 1
                    re = 1
                    dire = '<'
            elif dire == '<':
                if A[i-2] <= A[i-1] <= A[i]:
                    re = 0
                    dire = '<'
                else:
                    ans += 1
                    dire = '>'
                    re = 1
        else:
            re = 0

            



print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
a = [int(i) for i in 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!= ndelta:
        result += 1
        delta = 0
    elif not delta:
        delta = ndelta
    
print(result)

Submission status: Runtime Error

Solution code:
N = int(input())
a = [int(i) for i in 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: Runtime Error

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
count = 0
i = 0
while i < N-2:
    if a[i] <= a[i+1]:
        if a[i+1] > a[i+2]:
            count += 1
            i += 1
    
    if a[i] >= a[i+1]:
        if a[i+1] < a[i+2]:
            count += 1
            i += 1
    i += 1
print(count+1)

Submission status: Runtime Error

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

cnt = 1

increase = a[1] - a[0];
for i in range(2, len(a)):
    new_inc = a[i] - a[i-1];
    if increase * new_inc < 0:
        cnt += 1
        increase = 0
    elif increase == 0:
        increase = new_inc
print(cnt)

Submission status: Runtime Error

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

m = -1 #m=1:増加、m=2:減少
if n==1 or n==2:
    print(1)
else:
    cnt = 0
    num = lst[0]
    mode = 0
    for i in range(1,n):
        if mode==0:　# 待機
            if lst[i]==num:
                continue            
            elif lst[i] > num:
                mode = 1 # 増加
            elif lst[i] < num:
                mode = 2 # 減少
        elif mode == 1:
            if lst[i] < num:
                cnt += 1
                mode = 0
        elif mode == 2:
            if lst[i] > num:
                cnt += 1
                mode = 0
        num = lst[i]
    print(cnt + 1)

Submission status: Runtime Error

Solution code:
n=int(input())
*a,=map(int,input().split())
cnt=0
mode=0
for i in range(n-1):
    if mode==0:
        if a[i]<a[i+1]:
            cnt+=1
            mode="inc"
        elif a[i]>a[i+1]:
            cnt+=1
            mode="dec"
    if a[i]<a[i+1]:
        if mode=="inc":
            pass
        else:
            mode=0
    elif a[i]>a[i+1]:
        if mode=="dec":
            pass
        else:
            mode=0
if mode==0:
    print(cnt+1)
else:
    prnt(cnt)

Submission status: Runtime Error

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

def ans(x,L):
    ans = 1
    flag = 0
    tmp = L[0:2]
    if tmp[0] < tmp[1]:
        flag  = 1
    elif tmp[0] > tmp[1]:
        flag = -1
    for i in range(2,n):
        tmp.append(L[i])
        if len(tmp) == 1 and i == n-1:
            pass
        elif (flag == -1 and tmp[-2] < tmp[-1]) or (flag == 1 and tmp[-2] > tmp[-1]):
            ans += 1
            flag = 0
            tmp = [tmp[-1]]
        elif flag == 0 and tmp[-2] < tmp[-1]:
            flag = 1
        elif flag == 0 and tmp[-2] > tmp[-1]:
            flag = -1
    return(ans)
print(ans(n,l))

Submission status: Runtime Error

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

def ans(x,L):
    ans = 1
    flag = 1
    tmp = L[0:3]
    if (tmp[0]<tmp[1] and tmp[2]<tmp[1]) or (tmp[0]>tmp[1] and tmp[2]>tmp[1]):
        ans += 1
        flag = 0
    for i in range(3,x):
        tmp = tmp[1:3]
        tmp.append(L[i])
        if (tmp[0]<tmp[1] and tmp[2]<tmp[1] and flag > 0) or (tmp[0]>tmp[1] and tmp[2]>tmp[1] and flag > 0):
            ans += 1
            flag = 0
        else:
            flag += 1
    return(ans)
        
print(ans(n,l))

Submission status: Runtime Error

Solution code:
N = int(input())
array = list(map(int, input().split()))
ans = 1
rels = []
for i in range(1, N):
    a, b = array[i], array[i-1]
    if b < a:
        rels.append("<")
    if b > a:
        rels.append(">")
    if a == b:
        rels.append("=")
now = rels[0]
skip = False
for i in range(N-1):
    rel = rels[i]
    if skip:
        skip = False
        now = rel
        continue
    if rel == now or rel == "=":
        continue
    elif now == "=":
        now = rel
    else:
        skip = True
        ans += 1


print(ans)

Submission status: Runtime Error

Solution code:
#AGC 013 A - Sorted Arrays
#増加⇔減少に転じる際、Ai+1 - Aiは必ず符号反転することを利用
N = int(input())
A = list(map(int,input().split()))

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

Submission status: Runtime Error

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

isBigger = A[0] < A[1] 
isEqual = A[0] == A[1]
ans = 0
for i in range(1,N):
    if isEqual:
        if A[i] < A[i-1]:
            isBigger = False
            isEqual = False
        elif A[i-1] < A[i]:
            isBigger = True
            isEqual = False
        else:
            continue
    elif isBigger:
        if A[i] < A[i-1]:
            ans += 1
            if i < N-1:
                isBigger = A[i] < A[i+1]
                isEqual = A[i] == A[i+1]
            else:
                break
    else:
        if A[i-1] < A[i]:
            ans += 1
            if i < N-1:
                isBigger = A[i] < A[i+1]
                isEqual = A[i] == A[i+1]
            else:
                break

ans += 1
print(ans)

Submission status: Runtime Error

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

ans = 1
up,down = False,False
for i in range(N-1):
  if A[i] < A[i+1]:
    up = True
  elif A[i] > A[i+1]:
    down = True
  if up and dn:
    ans +=1
    up,down = False,False
    
print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
count=1
size=1
p=list(map(int,input().split()))
q=[p[0]]
for i in range (1,N):
  if p[i]!=p[i-1]:
    q.append(p[i])
    size+=1
j=0
while True:
  if j>=size-2:
    break
  while (q[j]-q[j+1])*(q[j+1]-q[j+2])>=0:
    if j>=size-2:
      break
    j+=1    
  count+=1
  j+=2
print(count)

Submission status: Runtime Error

Solution code:
N=int(input())
count=1
size=1
p=list(map(int,input().split()))
q=[p[0]]
for i in range (1,N):
  if p[i]!=p[i-1]:
    q.append(p[i])
    size+=1
j=0
while True:
  if j>=size-3:
    break
  while (q[j]-q[j+1])*(q[j+1]-q[j+2])>=0:
    j+=1
  count+=1
  j+=2
print(count)

Submission status: Runtime Error

Solution code:
N=int(input())
count=1
size=1
p=list(map(int,input().split()))
q=[p[0]]

j=0
while True:
  if j>=N-2:
    break
  while (q[j]-q[j+1])*(q[j+1]-q[j+2])>=0:
    j+=1
  count+=1
  j+=2
print(count)

Submission status: Runtime Error

Solution code:
N=int(input())
count=1
size=1
p=list(map(int,input().split()))
q=[p[0]]
for i in range (1,N):
  if p[i]!=p[i-1]:
    q.append(p[i])
    size+=1
j=0
while True:
  if j>=size-2:
    break
  while (q[j]-q[j+1])*(q[j+1]-q[j+2])>=0:
    j+=1
  count+=1
  j+=2
print(count)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
n = int(input())
a = list(map(int,input().split()))
if (a[1]-a[0]) < 0: flag = -1
else: flag = 1
ans = 1
i = 0
while True:
    if flag * (a[i+1]-a[i]) < 0:#増加
        ans += 1
        flag = -1 * flag
        i += 1
    i += 1
    if i == n-1:
        break
    elif i == n:
        ans += 1
        break
print(ans)

Submission status: Runtime Error

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

que = A[0]
list_plus = False
list_minus = False
que_list = []

for i in A:
    if que < i:
        list_plus = True
        break
    elif que > i:
        list_minus = True
        break

if list_plus:
    for i in A:
        if A[i] < A[i-1]:
            len = i
            break
else:
    for i in A:
        if A[i] > A[i-1]:
            len = i
            break

# print(len)
s = N // len
a = N % len

print(s + a)

Submission status: Runtime Error

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

que = 0
list_plus = False
list_minus = False
que_list = []

for i in A:
    if que == 0:
        que = i
        que_list.append(i)
    elif que < i:
        list_plus = True
        que = i
        que_list.append(i)
    elif que > i:
        list_minus = True
        que = i
        que_list.append(i)

    if list_plus:
        if list_minus:
            len = len(que_list) - 1
            break
    else:
        if list_plus:
            len = len(que_list) - 1
            break

s = N // len
a = N % len

print(s + a)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

count = 0
flag = False

if a[0] >= a[1]:
  flag = True
else:
  flag = False
  
for i in range(1, n-1):
  if a[i] == a[i+1]:
    break
  elif a[i] > a[i+1]:
    if not flag:
      count+=1
      flag = False
  else:
    if flag:
      count+=1
      flag = True
print(count+1)

Submission status: Runtime Error

Solution code:
N = int(input())
A_lst = [int(x) for x in input().split()]
B_lst =[]
for i in range(N - 1):
    B_lst.append(A_lst[i + 1] - A_lst[i])

def my_sign(n):
    return (n > 0) - (n < 0)

cnt = 1
i = 0
pm = my_sign(B_lst[0])
while True:
    if i >= len(B_lst) - 2:
        break
    else:
        if my_sign(B_lst[i + 1]) == 0:
            i += 1
            continue
        elif pm == my_sign(B_lst[i + 1]) or pm == 0:
            pm = my_sign(B_lst[i + 1])
            i += 1
            continue
        else:
            cnt += 1
            pm = my_sign(B_lst[i + 1])
            i +=2
print(cnt)

Submission status: Runtime Error

Solution code:
N = int(input())
A_lst = [int(x) for x in input().split()]
B_lst =[]
for i in range(N - 1):
    B_lst.append(A_lst[i + 1] - A_lst[i])

def my_sign(n):
    return (n > 0) - (n < 0)

cnt = 1
i = 0
pm = my_sign(B_lst[0])
while True:
    if i >= len(B_lst) - 1:
        break
    else:
        if my_sign(B_lst[i + 1]) == 0:
            i += 1
            continue
        elif pm == my_sign(B_lst[i + 1]) or pm == 0:
            pm = my_sign(B_lst[i + 1])
            i += 1
            continue
        else:
            cnt += 1
            pm = my_sign(B_lst[i + 1])
            i +=2
print(cnt)

Submission status: Runtime Error

Solution code:
N = int(input())
A_lst = [int(x) for x in input().split()]
B_lst =[]
for i in range(N - 1):
    B_lst.append(A_lst[i + 1] - A_lst[i])

def my_sign(n):
    return (n > 0) - (n < 0)

cnt = 1
i = 0
pm = my_sign(B_lst[0])
while True:
    if i >= len(B_lst) - 1:
        break
    else:
        if my_sign(B_lst[i + 1]) == 0:
            i += 1
            continue
        elif pm == my_sign(B_lst[i + 1]) or pm == 0:
            pm = my_sign(B_lst[i + 1])
            i += 1
            continue
        else:
            cnt += 1
            i +=2
print(cnt)

Submission status: Runtime Error

Solution code:
N = int(input())
Alist = list(map(int, input().split()))
if N < 3:
    print(1)
else:
    ans = 1
    last = Alist[0]
    new_a = [last]
    for i in range(N):
        now = Alist[i]
        if now != last:
            new_a.append(now)
        last = now
    now = "up" if new_a[0] < new_a[1] else "down
    f = now
    for i in range(1, len(new_a)):
        if new_a[i - 1] == new_a[i]:
            continue
        elif new_a[i - 1] < new_a[i]:
            if now == "down":
                now = "up"
        elif new_a[i - 1] > new_a[i]:
            if now == "up":
                now = "down"
        if now != f:
            ans += 1
        # print(i, now, ans)
    print(ans + 1)

Submission status: Runtime Error

Solution code:
N = int(input())
P = list(map(int,input().split()))
cnt = 1
flag = 0 
for i in range(1,N+1):
    if P[i-1]!=P[i] and flag == 0:
        flag = 1
    elif flag == 1 and ((P[i-1] > P[i]) != (P[i-2]>P[i-1])):
        cnt += 1
        flag = 0
print(cnt)

Submission status: Runtime Error

Solution code:
N = int(input())
P = list(map(int,input().split()))
cnt = 1
flag = 0 
for i in range(1,N+1):
    if P[i-1]!=P[i] and flag == 0:
        flag = 1
    elif flag == 1 and ((P[i-1] > P[i]) != (P[i-2]>P[i-1])):
        cnt += 1
        flag = 0
print(cnt)

Submission status: Runtime Error

Solution code:
N = int(input())
P = list(map(int,input().split()))
cnt = 1
flag = 0 #flag 0=,1=+,2=-
for i in range(1,N+1):
    if P[i-1]!=P[i] and flag == 0:
        flag = 1
    elif flag == 1 and ((P[i-1] > P[i]) != (P[i-2]>P[i-1])):
        cnt += 1
        flag = 0
print(cnt)

Submission status: Runtime Error

Solution code:
N = int(input())
P = list(map(int,input().split()))
cnt = 1
flag = 0 #flag 0=,1=+,2=-
for i in range(1,N+1):
    if P[i-1]!=P[i] and flag == 0:
        flag = 1
    elif flag == 1 and ((P[i-1] > P[i]) != (P[i-2]>P[i-1])):
        cnt += 1
        flag = 0
print(cnt)

Submission status: Runtime Error

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

ans = 1
x = 0

for i in range(n-1):
    if A[i] < A[i+1]:
        if x == -1:
            ans += 1
            x = 0
        elif x == 0:
            x = 1
    elif A[i] > A[i+1]:
        if x == 1:
            ans += 1
            x = 0
        elif x == 0:
            x = -1
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
i=0
ans=0
while i<n-1:
    if a[i]<a[i+1]:
        i+=1
        while i<n-1:
            if a[i]<=a[i+1]:
                i+=1
            else:
                break
        #print(i)
        i+=1
        ans+=1
    elif a[i]>a[i+1]:
        i+=1
        while i<n-1:
            if a[i]>=a[i+1]:
                i+=1
            else:
                break
        #print(i)
        i+=1
        ans+=1
    else:
        i+=1
if (a[n-2]<a[n-1])!=(a[n-3]<a[n-2]):
    ans+=1
print(ans)

Submission status: Runtime Error

Solution code:
num = 0
kazu = 0
N = int(input())
data = list(map(int, input().split()))
kazu = len(data)
kai = 0
toku = kazu - 1
i = 1
if toku <= i:
    kai += 1
elif data[i] <= data[i - 1]:
    n = 0
else:
    n = 1
tane = n
i = 2
while i <= kazu - 1:
    if data[i] <= data[i - 1]:
        n = 0
    else:
        n = 1
    if tane != n:
        kai += 1
        i += 1
        if i == kazu:
            kai += 1
            break
        if data[i] <= data[i - 1]:
            n = 0
            tane = n
        else:
            n = 1
            tane = n
        continue
    i += 1
    tane = n
    if i >= kazu:
        kai += 1
        break
print(kai)

Submission status: Runtime Error

Solution code:
num = 0
kazu = 0
N = int(input())
data = list(map(int, input().split()))
kai = 0
toku = kazu - 1
i = 1
if toku <= i:
    kai += 1
elif data[i] <= data[i - 1]:
    n = 0
else:
    n = 1
tane = n
i = 2
while i <= kazu - 1:
    if data[i] <= data[i - 1]:
        n = 0
    else:
        n = 1
    if tane != n:
        kai += 1
        i += 1
        if i == kazu:
            kai += 1
            break
        if data[i] <= data[i - 1]:
            n = 0
            tane = n
        else:
            n = 1
            tane = n
        continue
    i += 1
    tane = n
    if i >= kazu:
        kai += 1
        break
print(kai)

Submission status: Runtime Error

Solution code:
data = []
num = 0
kazu = 0
N = int(input())
data = list(map(int, input().split()))
kai = 0
toku = kazu - 1
i = 1
if toku <= i:
    kai += 1
elif data[i] <= data[i - 1]:
    n = 0
else:
    n = 1
tane = n
i = 2
while i <= kazu - 1:
    if data[i] <= data[i - 1]:
        n = 0
    else:
        n = 1
    if tane != n:
        kai += 1
        i += 1
        if i == kazu:
            kai += 1
            break
        if data[i] <= data[i - 1]:
            n = 0
            tane = n
        else:
            n = 1
            tane = n
        continue
    i += 1
    tane = n
    if i >= kazu:
        kai += 1
        break
print(kai)

Submission status: Runtime Error

Solution code:
N = int(input())
data = list(map(int, input().split()))
num = 0
kazu = 0
kai = 0
toku = kazu - 1
i = 1
flag = 1
if toku <= i:
    kai += 1
    flag = 0
elif data[i] <= data[i - 1]:
    n = 0
else:
    n = 1
tane = n
i = 2
while i <= kazu - 1:
    if data[i] <= data[i - 1]:
        n = 0
    else:
        n = 1
    if tane != n:
        kai += 1
        i += 1
        if i == kazu:
            kai += 1
            break
        if data[i] <= data[i - 1]:
            n = 0
            tane = n
        else:
            n = 1
            tane = n
        continue
    i += 1
    tane = n
    if i >= kazu:
        kai += 1
        break
print(kai)

Submission status: Runtime Error

Solution code:
data = list(map(int, input().split()))
num = 0
kazu = 0
kai = 0
toku = kazu - 1
i = 1
flag = 1
if toku <= i:
    kai += 1
    flag = 0
elif data[i] <= data[i - 1]:
    n = 0
else:
    n = 1
tane = n
i = 2
while i <= kazu - 1:
    if data[i] <= data[i - 1]:
        n = 0
    else:
        n = 1
    if tane != n:
        kai += 1
        i += 1
        if i == kazu:
            kai += 1
            break
        if data[i] <= data[i - 1]:
            n = 0
            tane = n
        else:
            n = 1
            tane = n
        continue
    i += 1
    tane = n
    if i >= kazu:
        kai += 1
        break
print(kai)

Submission status: Runtime Error

Solution code:
num = 0
kazu = 0
n = int(input())
data = list(map(int, input().split()))
kazu = len(data)
kai = 0
toku = kazu - 1
i = 1
if toku <= i:
    kai += 1
if data[i] <= data[i - 1]:
    n = 0
else:
    n = 1
tane = n
i = 2
while i <= kazu - 1:
    if data[i] <= data[i - 1]:
        n = 0
    else:
        n = 1
    if tane != n:
        kai += 1
        i += 1
        if i == kazu:
            kai += 1
            break
        if data[i] <= data[i - 1]:
            n = 0
            tane = n
        else:
            n = 1
            tane = n
        continue
    i += 1
    tane = n
    if i >= kazu:
        kai += 1
        break
print(kai)

Submission status: Runtime Error

Solution code:
num = 0
kazu = 0
n = int(input())
data = list(map(int, input().split()))
kazu = len(data)
kai = 0
toku = kazu - 1
i = 1
if toku == i:
    kai += 1
if data[i] <= data[i - 1]:
    n = 0
else:
    n = 1
tane = n
i = 2
while i <= kazu - 1:
    if data[i] <= data[i - 1]:
        n = 0
    else:
        n = 1
    if tane != n:
        kai += 1
        i += 1
        if i == kazu:
            kai += 1
            break
        if data[i] <= data[i - 1]:
            n = 0
            tane = n
        else:
            n = 1
            tane = n
        continue
    i += 1
    tane = n
    if i >= kazu:
        kai += 1
        break
print(kai)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

if n == 1:
    print(1)

# 0減少　1そのまま　2増加
flg = [-1] * n

if a[0] < a[1]:
    flg[1] = 2
elif a[0] == a[1]:
    flg[1] = 1
elif a[0] > a[1]:
    flg[1] = 0

cnt = 0

for i in range(2,n):
    if a[i-1] < a[i]:
        if flg[i-1] == 0:
            flg[i] = 100
            cnt += 1
        elif flg[i-1] == 1 or flg[i-1] == 2 or flg[i-1] == 100:
            flg[i] = 2
    elif a[i-1] == a[i]:
        flg[i] = 1
    elif a[i-1] > a[i]:
        if flg[i-1] == 0 or flg[i-1] == 1 or flg[i-1] == 100:
            flg[i] = 0
        elif flg[i-1] == 2:
            flg[i] = 100
            cnt += 1

# print(flg)
print(cnt+1)

Submission status: Runtime Error

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

# 0減少　1そのまま　2増加
flg = [-1] * n
flg.append(-1)
if a[0] < a[1]:
    flg[1] = 2
elif a[0] == a[1]:
    flg[1] = 1
elif a[0] > a[1]:
    flg[1] = 0

cnt = 0

for i in range(2,n):
    if a[i-1] < a[i]:
        if flg[i-1] == 0:
            flg[i] = 100
            cnt += 1
        elif flg[i-1] == 1 or flg[i-1] == 2 or flg[i-1] == 100:
            flg[i] = 2
    elif a[i-1] == a[i]:
        flg[i] = 1
    elif a[i-1] > a[i]:
        if flg[i-1] == 0 or flg[i-1] == 1 or flg[i-1] == 100:
            flg[i] = 0
        elif flg[i-1] == 2:
            flg[i] = 100
            cnt += 1

# print(flg)
print(cnt+1)

Submission status: Runtime Error

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


diff = [A[i+1] - A[i] for i in range(N-1) if A[i+1]-A[i] != 0]
ans = 1
prev = diff[0]
id = 0
while(id < len(diff)-2):
    if (diff[id]*diff[id+1]) < 0:
        ans += 1
        id += 2
    else:
        id += 1

if diff[-1]*diff[-2] < 0 and diff[-2]*diff[-3]:
    ans += 1
print(ans)

Submission status: Runtime Error

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


diff = [A[i+1] - A[i] for i in range(N-1) if A[i+1]-A[i] != 0]
ans = 1
prev = diff[0]
id = 0
while(id < len(diff)-2):
    if (diff[id]*diff[id+1]) < 0:
        ans += 1
        id += 2
    else:
        id += 1

if diff[-1]*diff[-2] < 0:
    ans += 1
print(ans)

Submission status: Runtime Error

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


diff = [A[i+1] - A[i] for i in range(N-1) if A[i+1]-A[i] != 0]
ans = 1
prev = diff[0]
id = 0
while(id < len(diff)-1):
    if (diff[id]*diff[id+1]) < 0:
        ans += 1
        id += 2
    else:
        id += 1
print(ans)

Submission status: Runtime Error

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


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

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

# i = 0 の処理
if A[0] > A[1]:
    state = "lower"

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

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())
l = list(map(int,input().split()))
if l[-1] > l[-2]:
    l += [float('Inf')]
else:
    l += [-float('Inf')]
ans = 1
i = 1
while i <= n - 1:
    if l[i-1] <= l[i] and l[i] <= l[i+1]:
        i += 1
    elif l[i-1] >= l[i] and l[i] >= l[i+1]:
        i += 1
    else:
        #print(l[i-1],l[i],l[i+1])
        ans += 1
        i += 2
        
print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int, input().split()))
m=[]
m.append([A[0]])
j=0
f=True
if A[0] > A[1]:
  f=False
for i in range(1,N):
  if A[i-1] <= A[i] and f:
    m[j].append(A[i])
  elif A[i-1] >= A[i] and not f:
    m[j].append(A[i])
  else:
    j+=1
    m.append([])
    m[j].append(A[i])
    if i != N-1:
      f=True if A[i] < A[i+1] else False
print(len(m))

Submission status: Runtime Error

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

last = next(iter_)
try:
    while True:
        ans += 1
        now = next(iter_)
        while now == last:
            now = next(iter_)
        laststate = (last < now)
        state = laststate
        print(f"out: {last}, {now}")
        while state == laststate:
            last = now
            now = next(iter_)
            while now == last:
                now = next(iter_)
            state = (last < now)
            print(f"in: {last}, {now}")
        last = now
except StopIteration:
    print(ans)

Submission status: Runtime Error

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

last = next(iter_)
try:
    while True:
        ans += 1
        now = next(iter_)
        while now == last:
            now = next(iter_)
        laststate = (last < now)
        state = laststate
        print(f"out: {last}, {now}")
        while state == laststate:
            last = now
            now = next(iter_)
            while now == last:
                now = next(iter_)
            state = (last < now)
            print(f"in: {last}, {now}")
        last = now
except StopIteration:
    print(ans)

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

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

ans=0
pre=A[1]-A[0]
cool=True
for i in range(N-2):
    temp=A[i+2]-A[i+1]
    if temp==0:
        cool=True
        continue
    elif pre*temp<0 and cool:
        ans+=1
        cool=False
    else:
        cool=True
    pre=temp


print(ans+1)

Submission status: Runtime Error

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

ans=0
pre=A[1]-A[0]
cool=True
for i in range(N-2):
    temp=A[i+2]-A[i+1]
    if pre*temp<0 and cool:
        ans+=1
        cool=False
    else:
        cool=True
    pre=temp

print(ans+1)

Submission status: Runtime Error

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

i = 0
def w(i):
    # print(i)
    if A[i] - A[i-1] == 0:
        return 0
    else:
        return (A[i]-A[i-1]) / abs(A[i]-A[i-1])
v = w(1)
ans = 1

i = 2
while i < n:
    nv = w(i)
    if nv * v < 0:
        ans += 1
        i += 2
        if i == n+1:
            ans += 1
    else:
        i += 1
    if nv != 0:
        v = nv
print(ans)

Submission status: Runtime Error

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

v = (A[1]-A[0]) / abs(A[1]-A[0])
ans = 1
i = 2
while i < n:
    if (A[i] - A[i-1]) * v < 0:
        v *= -1
        ans += 1
        i += 2
        if i == n+1:
            ans += 1
    else:
        i += 1
print(ans)

Submission status: Runtime Error

Solution code:
N, L, T = list(map(int, input().split()))
data = [list(map(int, input().split())) for _ in range(N)]

d = {1: 1, 2: -1}

# 蟻が原点を通過する回数
n_origins = 0

positions = []
for i, (x, w) in enumerate(data):
    _x = (x + d[w] * T) % L
    n_origin = int((x + d[w] * T) // L)
    n_origins += n_origin
    positions.append(_x)

positions.sort()
positions = positions[n_origins:] + positions[:n_origins]
print('\n'.join(map(str, positions)))

Submission status: Runtime Error

Solution code:
N, L, T = list(map(int, input().split()))
data = [list(map(int, input().split())) for _ in range(N)]

d = {1: 1, 2: -1}

# 蟻が原点を通過する回数
n_origins = 0

positions = []
for i, (x, w) in enumerate(data):
    _x = (x + d[w] * T) % L
    n_origin = int((x + d[w] * T) // L)
    n_origins += n_origin
    positions.append(_x)

positions.sort()
positions = positions[n_origins:] + positions[:n_origins]
print(positions)