Problem:


Score : 300 points

Problem Statement

You are given an array A of length N.
Your task is to divide it into several contiguous subarrays.
Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order.
At least how many subarrays do you need to divide A into?

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Each A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible number of subarrays after division of A.

Sample Input 1

6
1 2 3 2 2 1

Sample Output 1

2

One optimal solution is to divide the array into [1,2,3] and [2,2,1].

Sample Input 2

9
1 2 1 2 1 2 1 2 1

Sample Output 2

5

Sample Input 3

7
1 2 3 2 1 999999999 1000000000

Sample Output 3

3



Sample input:
6
1 2 3 2 2 1


Sample output:
2

Submission status: Accepted

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

ans = 1
prev = A[0]
direction = 0
for a in A[1:]:
    if direction == 1:
        if prev > a:
            ans += 1
            direction = 0
            prev = a
            continue
    if direction == -1:
        if prev < a:
            ans += 1
            direction = 0
            prev = a
            continue
    if direction == 0:
        if prev < a:
            direction = 1
        elif prev > a:
            direction = -1
    prev = a
print(ans)

Submission status: Accepted

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

check = ''
ans = 0

for i in range(n-1):
    diff = A[i] - A[i+1]
    if check == '':
        if diff < 0:
            check = '+'
        elif diff > 0:
            check = '-'
    elif check != '':
        if check == '+' and diff > 0:
            ans += 1
            check = ''
        elif check == '-' and diff < 0:
            ans += 1
            check = ''

print(ans+1)

Submission status: Accepted

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

for i in range(N-1):
    d = A[i+1] - A[i]
    if direction == 0:
        if d > 0:
            direction = 1
        elif d < 0:
            direction = -1
    else:
        if direction == 1 and d < 0:
            cnt += 1
            direction = 0
        elif direction == -1 and d > 0:
            cnt +=1
            direction = 0
print(cnt)

Submission status: Accepted

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

direc = ""
ans = 0
for i in range(N-1):
  d = A[i+1] - A[i]
  if direc == "":
    if d > 0:
      direc = "up"
    elif d < 0:
      direc = "down"
  else:
    if direc == "up" and d < 0:
      ans += 1
      direc = ""
    elif direc == "down" and d > 0:
      ans += 1
      direc = ""
print(ans+1)

Submission status: Accepted

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

p = 0
count = 1

def start(L, A):
    global p
    if L[A+1] > L[A]:
        p=1
    elif L[A+1] < L[A]:
        p=2
    else:
        p=0

def upcount(L, A):
    global p, count
    if L[A+1] >= L[A]:
        p=1
    else:
        p=0
        count += 1

def downcount(L, A):
    global p, count
    if L[A+1] <= L[A]:
        p=2
    else:
        p=0
        count += 1

for i in range(n):
    if i == n-1:
        break
    elif p == 0:
        start(l, i)
    elif p == 1:
        upcount(l, i)
    else:
        downcount(l, i)

print(count)

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    ans = 0
    i = 0
    while i < N:
        pos1, pos2 = i, i
        while pos1+1 < N and A[pos1] <= A[pos1+1]:
            pos1 += 1
        while pos2+1 < N and A[pos2] >= A[pos2+1]:
            pos2 += 1
        ans += 1
        i = max(pos1, pos2) + 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    ans = 0
    i = 0
    while i < N:
        while i+1 < N and A[i] == A[i+1]:
            i += 1
        pos1, pos2 = i, i
        while pos1+1 < N and A[pos1] <= A[pos1+1]:
            pos1 += 1
        while pos2+1 < N and A[pos2] >= A[pos2+1]:
            pos2 += 1
        ans += 1
        i = max(pos1, pos2) + 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

count=1
flag=True
for i in range(n-1):
    if i==0:
        ans=a[i+1]-a[i]

    if not flag:
        ans=a[i+1]-a[i]
        flag=True

    if (ans<=0 and a[i+1]-a[i]<=0) or (ans>=0 and a[i+1]-a[i]>=0):
        if ans!=0:
            continue
        elif a[i+1]-a[i]!=0:
            ans=a[i+1]-a[i]
        else:
            continue
            


    else:
        count+=1
        flag=False

print(count)

Submission status: Accepted

Solution code:
n = int(input())
ans = list(map(int,input().split()))
key = ans[0]
up_key = True
down_key = True
count = 0
for i in range(n):
    if key < ans[i]:
        down_key = False
    if key > ans[i]:
        up_key = False
    if up_key == False and down_key == False:
        count += 1
        up_key = True
        down_key = True
    key = ans[i]
print(count+1)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split(' ')))
count = 0
tmp_arr = []
inc = -1
for i, a in enumerate(A):
    if N-1 == i:
        count += 1
    if len(tmp_arr) == 0:
        tmp_arr.append(a)
        continue
    if inc == 0 and a > tmp_arr[-1]:
        tmp_arr = []
        inc = -1
        count += 1
    elif inc == 1 and a < tmp_arr[-1]:
        tmp_arr = []
        inc = -1
        count += 1
    else:
        if a > tmp_arr[-1]:
            inc = 1
        elif a < tmp_arr[-1]:
            inc = 0
    tmp_arr.append(a)
print(count)

Submission status: Accepted

Solution code:
#n = int(input())
#n,k = map(int,input().split())
#x = list(map(int,input().split()))

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

ans = 1
mm = 0
i = 0
flg = 0
while(i<N-1):
    if A[i]>A[i+1]:
        if mm>0:
            ans += 1
            flg = 1
            
            # print(i,A[i])
        mm = -1
        if flg:
            flg = 0
            mm = 0

    elif A[i]<A[i+1]:
        if mm<0:
            ans += 1
            flg = 1
            
            # print(i,A[i])

        mm = 1
        if flg:
            flg = 0
            mm = 0
    i+=1
    
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
i = 0
count = 1
while i < N-1:
    while i < N-1 and A[i] == A[i+1]:
        i += 1
    # Up
    if A[i] < A[i+1]:
        while i < N-1 and A[i] <= A[i+1]:
            i += 1
        if i < N-1:
            count += 1
            i += 1
    # Down
    elif A[i] > A[i+1]:
        while i < N-1 and A[i] >= A[i+1]:
            i += 1
        if i < N-1:
            count += 1
            i += 1
print(count)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
i = 0
ret = 1
while i < N-1:
  while i < N-1 and A[i] == A[i+1]:
  	i += 1
  if A[i] < A[i+1]:
    while i < N-1 and A[i] <= A[i+1]:
      i += 1
    if i < N-1:
      ret += 1
      i += 1
  elif A[i] > A[i+1]:
    while i < N-1 and A[i] >= A[i+1]:
      i += 1
    if i < N-1:
      ret += 1
      i += 1
print(ret)

Submission status: Accepted

Solution code:
N=int(input())
f=0
fnew=0
A=list(map(int,input().split()))
cnt=0
i=0
l=0
r=0
downflag=0
upflag=0
ma=A[i]
mi=A[i]
while i<N:
 if ma>A[i]:
  downflag=1
 elif ma<A[i]:
  upflag=1
 if (upflag==1)and(A[i]>ma):
  ma=A[i]
 if (downflag==1)and(mi>A[i]):
  mi=A[i]
 if (ma+mi)!=(A[l]+A[i]):
  ma=A[i]
  mi=ma
  cnt=cnt+1
  l=i
  i=i-1
  upflag=0
  downflag=0
 i=i+1
print(cnt+1)

Submission status: Accepted

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

ans = 0
flagu = 0
flagd = 0

for i in range(n):
	if i == 0:
		continue
	elif ls[i - 1] < ls[i]:
		if flagd == 1:
			ans += 1
			flagu = 0
			flagd = 0
		elif flagu == 0:
			flagu = 1
	elif ls[i - 1] > ls[i]:
		if flagu == 1:
			ans += 1
			flagu = 0
			flagd = 0
		elif flagd == 0:
			flagd = 1
	
	if i == n - 1:
		ans += 1

if n == 1:
	print(1)
else:
	print(ans)

Submission status: Accepted

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

UNIA = []
b = ''
for i in range(N):
    if A[i] == b:
        continue
    else:
        UNIA.append(A[i])
        b = A[i]

# print(UNIA)

# if UNIA[0] < UNIA[1]:
#     up = True
# else:
#     up = False

up = None
ans = 1
for i in range(len(UNIA)-1):
    if up is None:
        if UNIA[i] < UNIA[i+1]:
            up = True
        else:
            up = False
    elif up:
        if UNIA[i] < UNIA[i+1]:
            continue
        else:
            up = None
            ans += 1
    else:
        if UNIA[i] < UNIA[i+1]:
            up = None
            ans += 1

print(ans)

Submission status: Accepted

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

flg = 0 # 1 increase -1 decrease
prev = -1

cnt = 0
for a in A:
  if flg == 0 and prev==-1:
    prev = a
    cnt += 1
  elif flg==0 and not prev==-1:
    if a > prev:
      flg = 1
      prev = a
    elif a < prev:
      flg = -1
      prev = a
  elif flg == 1:
    if a < prev:
      flg = 0
      prev = a
      cnt +=1
    else:
      prev = a
  elif flg == -1:
    if a > prev:
      flg = 0
      prev = a
      cnt +=1
    else:
      prev = a
  #print(a, flg, prev, cnt)
print(cnt)

Submission status: Accepted

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

flg = None
cur = a[0]
cnt = 1

for v in a[1:]:
  if v > cur:
    if flg is None:
      flg = True
    if not flg:
      flg = None
      cnt += 1
  if v < cur:
    if flg is None:
      flg = False
    if flg:
      cnt += 1
      flg = None
  cur = v
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
lst = list(map(int,input().split()))
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: Accepted

Solution code:
# A - Sorted Arrays

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

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

increase_flg = False
decrease_flg = False
ans = 1

for i in range(1, N):
    if A[i] > A[i-1] and not decrease_flg:
        increase_flg = True
    elif A[i] > A[i-1] and decrease_flg:
        decrease_flg = False
        increase_flg = False
        ans += 1
    elif A[i] < A[i-1] and not increase_flg:
        decrease_flg = True
    elif A[i] < A[i-1] and increase_flg:
        increase_flg = False
        decrease_flg = False
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int, input().split()))
now = As[0]
shape = 0
ans = 1
for i in As:
    if shape == 0:
        if i  > now:
            shape = 1
            now = i
        elif i < now:
            shape = 2
            now = i
    elif shape == 1:
        if i < now:
            shape = 0
            now = i
            ans += 1
        else:
            now = i
    else:
        if i > now:
            shape = 0
            now = i
            ans += 1
        else:
            now = i
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=1
b=[]
for i in range(n-1):
    a0=a[i+1]-a[i]
    if a0!=0:
        b.append(a0)
if len(b)>1:
    i=0
    while i<len(b)-1:
        if b[i]*b[i+1]<0:
            ans+=1
            i+=1
        i+=1
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
a = list(map(int,input().split()))
bit = 0
ans = 1
i = 0
while i < n-1:
    if a[i+1] == a[i]: pass
    if bit == 0:
        bit = a[i+1]-a[i]
        #ans += 1
    else:#bit が立っているなら符号が一致するかを見て一致しないならans +=  1
        if bit * (a[i+1]-a[i]) < 0:#符号不一致
            bit = 0 
            ans += 1 
    i += 1
print(ans)

Submission status: Accepted

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:
    print(cnt)

Submission status: Accepted

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:
            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)
if n <= 2:
    print(1)
else:
    print(ans(n,l))

Submission status: Accepted

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

count = 1
is_up = False
is_down = False
for i in range(1, n):
    if a[i] < a[i-1] and is_up:
        count += 1
        is_up = False
    elif a[i] > a[i-1] and is_down:
        count += 1
        is_down = False
    elif a[i] < a[i-1]:
        is_down = True
    elif a[i] > a[i-1]:
        is_up = True
print(count)

Submission status: Accepted

Solution code:
N = int(input())
array = list(map(int, input().split()))
ans = 1
rels = []
if N == 1:
    print(1)
    exit()
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: Accepted

Solution code:
n = int(input())
dat = list(map(int, input().split()))
mode = 0 #  0: None, 1: add, 2: sub
res = 1
for i in range(1, n):
    if mode == 0:
        if dat[i-1] == dat[i]:
            mode = 0
        if dat[i-1] > dat[i]:
            mode = 2
        if dat[i-1] < dat[i]:
            mode = 1
    if mode == 1:
        if dat[i-1] == dat[i]:
            pass
        if dat[i-1] > dat[i]:
            res += 1
            mode = 0
        if dat[i-1] < dat[i]:
            pass
    if mode == 2:
        if dat[i-1] == dat[i]:
            pass
        if dat[i-1] > dat[i]:
            pass
        if dat[i-1] < dat[i]:
            res += 1
            mode = 0
print(res)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 1
inc = False
dec = False
before = A[0]
for a in A:
    if not inc and not dec:
        if a > before:
            inc = True
        if a < before:
            dec = True
    elif inc:
        if a < before:
            ans += 1
            inc = False
    elif dec:
        if a > before:
            ans += 1
            dec = False
    before = a
print(ans)

Submission status: Accepted

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

count = 1
sign = 0
for i in range(N-1):
    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: Accepted

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

count = 1
sign = 0
for i in range(n - 1):
    if sign == 0:
        sign = (ass[i + 1] - ass[i]) 
    else:
        if (ass[i + 1] - ass[i]) * sign < 0:
            count += 1
            sign = 0
print(count)

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int,input().split()))
pops = []


ans = 0

mnd = True
first = True
for i in range(1,len(b)):
    if b[i-1] == b[i]:
        continue
    if first and b[i] != b[i-1]:
        mnd = b[i-1] < b[i]
        first = False
    if mnd and b[i-1] > b[i]:
        ans += 1
        first = True
    elif (not mnd) and b[i-1]<b[i]:
        ans += 1
        first = True
print(ans+1)

Submission status: Accepted

Solution code:
N,*A=map(int, open(0).read().split())
st=0
ans=1
for i,a in enumerate(A):
    if st==1:
        if A[i-1]>a:
            st=0
            ans+=1
    elif st==-1:
        if A[i-1]<a:
            st=0
            ans+=1
    elif st==0 and i!=0:
        p=A[i-1]
        if p<a:
            st=1
        elif p>a:
            st=-1
print(ans)

Submission status: Accepted

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

ans = 0
if 2 <= N:
    isBigger = A[0] < A[1] 
    isEqual = A[0] == A[1]
    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: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
f = None
count = 1
for i in range(N - 1):
    if f is None:
        if a[i] < a[i + 1]:
            f = 1
        elif a[i] > a[i + 1]:
            f = -1
        continue

    if (f == 1 and a[i] > a[i + 1]) or (f == -1 and a[i] < a[i + 1]):
        count += 1
        f = None

print(count)

Submission status: Accepted

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

c = 1
m = a[0]

s = 1 # 0:- 1:_ 2:+

for i in a:
    if m == i:
        continue
    elif m > i:
        if s == 2:
            s = 1
            c += 1
        else:
            s = 0
    else:
        if s == 0:
            s = 1
            c += 1
        else:
            s = 2
    m = i

print(c)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
plus=0
minus=0
ans=1
for i in range(N-1):
    plus += A[i]<A[i+1]
    minus += A[i]>A[i+1]
    if plus and minus:
        ans += 1
        plus = 0
        minus = 0
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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-3:
      count-=1
      break
    j+=1    
  count+=1
  j+=2
print(count)

Submission status: Accepted

Solution code:
import sys

input=sys.stdin.readline

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

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

Submission status: Accepted

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

state_plus = False
state_minus = False
state_flat = True
count = 0

for i in range(N-1):
    # print(str(A[i]) + ',' + str(A[i + 1]))
    if state_flat:
        if A[i + 1] > A[i]:
            state_flat = False
            state_plus = True
        elif A[i + 1] < A[i]:
            state_flat = False
            state_minus = True
    elif state_plus:
        if A[i] > A[i + 1]:
            state_plus = False
            state_flat = True
            count += 1
    elif state_minus:
        if A[i] < A[i + 1]:
            state_minus = False
            state_flat = True
            count += 1
    # print(count)

print(count + 1)

Submission status: Accepted

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

ans = 0
pos = 0
pos1 = 0
pos2 = 0

while True:
    if pos==N:
        break
    pos1 = pos
    pos2 = pos
    while pos1<N-1 and A[pos1] <= A[pos1+1]:
        pos1 +=1
#     print(pos1)
    while pos2<N-1 and A[pos2] >= A[pos2+1]:
        pos2 +=1
#     print(pos2)
    large = max(pos1, pos2)
    pos = large+1
    ans += 1
print(ans)

Submission status: Accepted

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

con = 0
state = "FLAT"

for i in range(0,N - 1):
  if state == "FLAT":
    if A[i + 1] > A[i]:
      state = "INCREASE"
    elif A[i + 1] < A[i]:
      state = "DECREASE"
  elif state == "INCREASE":
    if A[i + 1] < A[i]:
      con += 1
      state = "FLAT"
  elif state == "DECREASE":
    if A[i + 1] > A[i]:
      con += 1
      state = "FLAT"
  
      
print(con + 1)

Submission status: Accepted

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

ans = 1
last = A[0]
n = 0
for i in A:
    if n == 0:
        if last < i:
            n = 1
        elif last > i:
            n = -1
        else:
            n = 0
    elif n == 1:
        if last >  i:
            ans += 1
            n = 0
    else:
        if last < i:
            ans += 1
            n = 0
    last = i

print(ans)

Submission status: Accepted

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

ans = 1
A0 = list_A[0]
flag = 0

for i in range(1,len(list_A)) :
    if list_A[i] > list_A[i-1] :
        if flag == 0 :
            flag = 1
        elif flag == 1 :
            pass
        else :
            ans += 1
            flag = 0
    elif list_A[i] < list_A[i-1] :
        if flag == 0 :
            flag = -1
        elif flag == 1 :
            ans += 1
            flag = 0
        else :
            pass
    else :
        pass

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def main():
    N = int(input())
    a_list = list(map(int, input().split()))
    inc_flag = None
    dec_flag = None
    ans = 0
    for i in range(1, N):
        if inc_flag is None and dec_flag is None and a_list[i-1] < a_list[i]:
            inc_flag = True
        elif inc_flag is None and dec_flag is None and a_list[i-1] > a_list[i]:
            dec_flag = True
        elif inc_flag is True and a_list[i-1] > a_list[i]:
            ans += 1
            inc_flag = None
        elif dec_flag is True and a_list[i-1] < a_list[i]:
            ans += 1
            dec_flag = None
    print(ans + 1)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int,input().split()))
    now = A[0]
    count = 0
    upflag = False
    downflag = False
    for i in range(1,N):
        if upflag:
            if A[i] - now < 0:
                upflag = False
                downflag = False
        elif downflag:
            if A[i] - now > 0:
                downflag = False
                upflag = False
        else:
            if A[i] - now > 0:
                upflag = True
                count += 1
            elif A[i] - now < 0:
                downflag = True
                count += 1
        now = A[i]
    if not (upflag or downflag):
        count += 1
    print(count)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def d(i, j):
    if i == j:
        return "z"
    if i < j:
        return "p"
    else:
        return "m"

n = int(input())
a = [int(x) for x in input().split()]
if n < 3: print(1)
else:
    ans = 1
    s = d(a[0], a[1])
    for i in range(1, n):
        tmp = d(a[i-1], a[i])
        if tmp == "z": continue
        elif s == tmp: continue
        else:
            if s == "z":
                s = tmp
                continue
            else:
                s = "z"
                ans += 1
    print(ans)

Submission status: Accepted

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


#----------
N = int(input().strip())
A_list = list(map(int, input().rstrip().split()))
#----------
flag_plus_minus = ""
count = 0
if len(A_list) == 1:
    count = 1
else:

    for i in range(len(A_list)-1):
        diff = A_list[i+1] - A_list[i]
    
        if flag_plus_minus == "" :
            flag_plus_minus = str((diff > 0) - (diff < 0))
            count += 1

        if flag_plus_minus == "0" :
            flag_plus_minus = str((diff > 0) - (diff < 0))
        
        if flag_plus_minus != "" and A_list[i] != A_list[i+1] :
            if ( diff * int(flag_plus_minus) ) < 0:
                flag_plus_minus = ""
                
                if i == (len(A_list)-1) - 1:
                    count += 1
        
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

cnt = 0
mode = 0

for i in range(len(A)):
    if i == 0:
        continue
    
    if (A[i] - A[i-1] >= 0 and mode >=0) or (A[i] - A[i-1] <= 0 and mode <= 0):
        if A[i] - A[i-1] > 0:
            mode = 1
        if A[i] - A[i-1] < 0:
            mode = -1
    else:
        mode = 0
        cnt += 1

print(cnt+1)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
r = 1
if n>2:
    d = A[1]-A[0]
    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: Accepted

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

ans = 1

tp = a[0]
flag = 0  # 1:inc 2:dec
for i in range(1, n):
    t = a[i]
    if (flag == 0):
        if (t > tp):
            flag = 1
        elif (t < tp):
            flag = 2
        else:
            flag = 0
    elif(flag == 1):
        if (t < tp):
            ans += 1
            flag = 0
    elif(flag == 2):
        if (t > tp):
            ans += 1
            flag = 0
    tp = t

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#入力（後でいじる）
def pin(type=int):
    return map(type,input().split())
#どっかで最悪計算量の入力データを用意する関数を作ろう？
"""
方針
#差分をとって、0を抜いた数列をつくる
    これにて非単調減少・増加を単調増加、単調減少として捉えられる
#隣り合う2つ正負が違うなら積は負になるので
#負になった2つの間で切ればよい
"""
#solution:
#潜 影 蛇 手 ! !
N=int(input())
A=list(pin())
B=[(A[j+1]-A[j])for j in range(len(A)-1)]
C=[]
for i in range(len(B)):
    if B[i]!=0:
        C.append(B[i])

k=0
ans=0
#print(A)
#print("",B)
#print(C)
while(k<len(C)-1):
    #print(k)
    if C[k+1]*C[k]<0:
        #print("crak is right of A[",k+1,"]")
        k+=1#１っことばす
        ans+=1
    k+=1
print(ans+1)

Submission status: Accepted

Solution code:
'''
先頭から値を確認していき、値が増加/減少した後に値が減少/増加した場合に数列の分割数を増やす
'''

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

ans = 1
flg = 0
for i in range(N-1):
    if flg == 0:
        if A[i+1] > A[i]:
            flg = 1
        elif A[i+1] < A[i]:
            flg = -1
        else:
            pass
    elif flg == 1:
        if A[i+1] > A[i]:
            pass
        elif A[i+1] < A[i]:
            flg = 0
            ans += 1
        else:
            pass
    else:
        if A[i+1] > A[i]:
            flg = 0
            ans += 1
        elif A[i+1] < A[i]:
            pass
        else:
            pass

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ais = list(map(int, input().split()))
if len(ais) == 1:
  print(1)
  quit()
if ais[0] < ais[1]:
  tend = 1
elif ais[0] > ais[1]:
  tend = -1
else:
  tend = 0
pref = ais[1]
ans = 1
for a in ais[2:]:
  if pref < a and tend == -1:
    ans += 1
    tend = 0
  elif pref > a and tend == 1:
    ans += 1
    tend = 0
  elif tend == 0:
    if pref < a:
      tend = 1
    elif pref > a:
      tend = -1
    elif pref == a:
      pass
  pref = a
print(ans)

Submission status: Accepted

Solution code:

N = int(input())

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

ans = 1
q = []

for i,a in enumerate(A):

    if len(q) != 0 and ( (q[0] < q[-1] and q[-1] > a) or (q[0] > q[-1] and q[-1] < a) ):
        q = [a]
        ans += 1

    else:
        q.append(a)

print (ans)