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: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
c=1
i=2

while i<n:
  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: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
c=1
i=2

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

print(c)

Submission status: Wrong Answer

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

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

print(c)

Submission status: Wrong Answer

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

count = 0
if N == 1:
    print(0)
else:
    if A[0] <= A[1]:
        plus = True
    else:
        plus = False
    for i in range(1,N-1):
        #次の値が自分より大きいか小さいかをtmpに入れていく。大きいときTrue
        if A[i] < A[i+1]:
            tmp = True
        elif A[i] == A[i+1]:
            tmp = plus
        else:
            tmp = False
        
        #区切りを数えていくよ
        if tmp != plus:
            count += 1
            if plus: #plus == Trueならば
                plus = False
            else:
                plus = True
    print(count+1)

Submission status: Wrong Answer

Solution code:
n = int(input())

arr = map(int,raw_input().split())

ans = 0
i = 1
while i < n-1:
	if arr[i-1] <= arr[i] <= arr[i+1]:
		i += 1
		next
	elif arr[i-1] >= arr[i] >= arr[i+1]:
		i += 1
		next

	else :
		ans += 1
		i += 2

print ans + 1

Submission status: Wrong Answer

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

flg=[]
for i in range(n-1):
    if a[i+1]>a[i]: flg.append(1)
    elif a[i+1]==a[i]: flg.append(0)
    else: flg.append(-1)

for i in range(len(flg)):
    if flg[i]==0:
        if 1<=i<=len(flg)-2:
            if flg[i-1]!=flg[i+1]:
                flg[i]=flg[i+1]
            else: flg[i]=int((flg[i+1]+flg[i-1])/2)
        elif i==0: flg[i]=flg[i+1]
        else: flg[i]=flg[i-1]

ans=1
skp=0
for i in range(len(flg)-1):
    if skp:
        skp=0
        continue
    if flg[i+1]!=flg[i]:
        ans+=1
        skp=1
print(ans)

Submission status: Wrong Answer

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

def solve(N, A):
    if N <= 2:
        return 1
    s = 0
    result = 1
    i = 0
    while i < N-1:
        r = A[i+1] - A[i]
        if (r > 0 and s < 0) or (r < 0 and s > 0):
            result += 1
            s = 0
        else:
            s = r
        i += 1
    return result

print(min(solve(N, A), solve(N, A[::-1])))

Submission status: Wrong Answer

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

def solve(N, A):
    if N <= 2:
        return 1
    s = 0
    result = 1
    i = 0
    while i < N-1:
        r = A[i+1] - A[i]
        if (r > 0 and s < 0) or (r < 0 and s > 0):
            result += 1
            s = 0
        else:
            s = r
        i += 1
    return result

print(solve(N, A))

Submission status: Wrong Answer

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

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

if N <= 2:
    print(1)
    sys.exit(0)

count = 0
status = 0
for i in range(1,N):
    # print(A[i], status)
    if A[i-1] == A[i]:
        new_status = 0
    if A[i-1] < A[i]:
        new_status = 1
    else:
        new_status = -1
    if status == 0:
        status = new_status
        continue
    if status == new_status:
        continue
    else:
        count += 1
        status = 0

print(count+1)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = list(map(int, input().split()))
a = 0
b = 0
up = True
i = 0
while i < n-1:
  if l[i+1] == l[i]:
    i += 1
    continue
  elif l[i+1] < l[i] and up:
    a += 1
    up = False
    i += 1
  elif up == False and l[i+1] > l[i]:
    up = True
    a += 1
    i += 2
  else:
    i += 1

up = False
i = 0
while i < n-1:
  if l[i+1] == l[i]:
    i += 1
    continue
  elif l[i+1] < l[i] and up:
    b += 1
    up = False
    i += 1
  elif up == False and l[i+1] > l[i]:
    up = True
    b += 1
    i += 2
  else:
    i += 1

print(min(a+1, b+1))

Submission status: Wrong Answer

Solution code:
def sign(x):
    if x < 0:
        return -1
    elif x == 0:
        return 0
    else:
        return 1


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

    cnt, idx = 1, 1
    if 1 < n:
        flg = sign(a[idx]-a[idx-1])
        while idx <= n - 1:
            if flg == 0:
                flg = a[idx] - a[idx-1]

            if sign(a[idx]-a[idx-1]):
                if flg != sign(a[idx]-a[idx-1]):
                    cnt += 1
                    idx += 1
                    if idx <= n-1:
                        flg = sign(a[idx]-a[idx-1])

            idx += 1

    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def sign(x):
    if x < 0:
        return -1
    elif x == 0:
        return 0
    else:
        return 1


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

    cnt, idx = 1, 1
    if 1 < n:
        flg = a[idx] - a[idx-1]
        while idx <= n - 1:
            if flg == 0:
                flg = a[idx] - a[idx-1]

            if sign(a[idx]-a[idx-1]):
                if flg != sign(a[idx]-a[idx-1]):
                    cnt += 1
                    idx += 1
                    if idx <= n-1:
                        flg = sign(a[idx]-a[idx-1])

            idx += 1

    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def sign(x):
    if x < 0:
        return -1
    elif x == 0:
        return 0
    else:
        return 1


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

    cnt, idx = 1, 1
    if 1 < n:
        flg = a[idx] - a[idx-1]
        while idx <= n - 1:
            if sign(a[idx]-a[idx-1]):
                if flg != sign(a[idx]-a[idx-1]):
                    cnt += 1
                    idx += 1
                    if idx < n:
                        flg = sign(a[idx]-a[idx-1])

            idx += 1

    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def sign(x):
    if x < 0:
        return -1
    elif x == 0:
        return 0
    else:
        return 1

def main():
    n = int(input())
    a = list(map(int, input().split()))
    
    cnt, i = 1, 1
    while i < n - 1:
        if sign(a[i]-a[i-1]) * (-1) == sign(a[i+1]-a[i]):
            cnt += 1
            i += 1
        i += 1
            
    print(cnt)
    
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def sign(x):
    if x < 0:
        return -1
    elif x == 0:
        return 0
    else:
        return 1

def main():
    n = int(input())
    a = list(map(int, input().split()))
    
    cnt, i = 1, 1
    while i < n - 1:
        if sign(a[i]-a[i-1]) != sign(a[i+1]-a[i]):
            cnt += 1
            i += 1
        i += 1
            
    print(cnt)
    
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
A = list(map(int, input().split()))
count = 1
upd = 0
for i in range(N-1):
  s0 = np.sign(A[i+1] - A[i])
  if upd == 0:
    upd = s0
  elif s0 != 0 and s0 != upd:
    upd = 0
    count += 1
    print(i,"番目")
print(count)

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
A = list(map(int, input().split()))
count = 1
upd = 0
for i in range(N-1):
  s0 = np.sign(A[i+1] - A[i])
  if upd == 0:
    upd = s0
  elif s0 != 0 and s0 != upd:
    upd = s0
    count += 1
print(count)

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
A = list(map(int, input().split()))
count = 1
upd = 0
for i in range(1,N-1):
  s0 = np.sign(A[i] - A[i-1])
  s1 = np.sign(A[i+1] - A[i])
  if upd != 0 and s0 != s1 and s0 != 0 and s1 != 0:
    upd = 0
    count +=1
  else:
    upd = np.sign(A[i] - A[i-1])
print(count)

Submission status: Wrong Answer

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

count = 0
ans = 1
bef = 0
buff = True
for i in A:
    if count == 0:
        count = 1
    elif count == 1:
        if bef < i:
            count += 1
        elif bef > i:
            count += 1
            buff = False
        else:
            pass
    else:
        if buff:
            if bef <= i:
                pass
            else:
                ans += 1
                count = 1
        else:
            if bef >= i:
                pass
            else:
                ans += 1
                count = 1
    bef = i
print(ans)

Submission status: Wrong Answer

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

count = 0
ans = 1
bef = 0
buff = True
for i in A:
    if count == 0:
        count = 1
    elif count == 1:
        if bef < i:
            count += 1
        elif bef > i:
            count += 1
            buff = False
        else:
            pass
    else:
        if buff:
            if bef <= i:
                pass
            else:
                ans += 1
                count = 1
        else:
            if bef >= i:
                pass
            else:
                ans += 1
                count = 1
    bef = i

Submission status: Wrong Answer

Solution code:
N = input()
state = 0
max = 0
min = 0
prev = 0
len = 0
ANS = 1
S = input()
A = list(map(int,S.split()))
 
for i in range(0,int(N)):
  len = len + 1
  if state in {0}:	# Initial state
      prev = A[i]
      state = 1
  elif state in {1}: # Newtral state
    if A[i] > prev:
      state = 2
    elif A[i] < prev:
      state = 3
  elif state in {2}: # Acsending state
    if A[i] < prev:
      state = 1
      ANS = ANS + 1
      len = 0
  elif state in {3}: # Descending state
    if A[i] > prev:
      state = 1
      ANS = ANS + 1
      len = 0
  prev = A[i]
  print('s=', state, 'in=', A[i], 'prev=', prev, 'ANS=', ANS, 'len=', len)
print(ANS)

Submission status: Wrong Answer

Solution code:
N = input()
state = 0
max = 0
min = 0
prev = 0
len = 0
ANS = 1
S = input()
A = list(map(int,S.split()))
 
for i in range(0,int(N)):
#  print('s=', state, 'in=', A[i], 'prev=', prev, 'ANS=', ANS, 'len=', len)
  len = len + 1
  if state in {0}:	# Initial state
      prev = A[i]
      state = 1
  elif state in {1}: # Newtral state
    if A[i] > prev:
      state = 2
#      print('0 -> 1')
    elif A[i] < prev:
      state = 3
#      print('0 -> 2')
  elif state in {2}: # Acsending state
    if A[i] < prev:
      state = 3
      if len >= 1:
        ANS = ANS + 1
        len = 0
#      print(prev, ', ', A[i], ', ', '1 -> 2')
  elif state in {3}: # Descending state
    if A[i] > prev:
      state = 2
      if len >= 1:
        ANS = ANS + 1
        len = 0
#      print(prev, ', ', A[i], ', ', '2 -> 1')
  prev = A[i]
#if len == 0:
#  ANS = ANS + 1
print(ANS)

Submission status: Wrong Answer

Solution code:
N = input()
state = 0
max = 0
min = 0
prev = 0
len = 0
ANS = 1
S = input()
A = list(map(int,S.split()))
 
for i in range(0,int(N)):
#  print('s=', state, 'in=', A[i], 'prev=', prev, 'ANS=', ANS, 'len=', len)
  len = len + 1
  if state in {0}:	# Initial state
      state = 1
  elif state in {1}: # Newtral state
    if A[i] > prev:
      state = 2
#      print('0 -> 1')
    elif A[i] < prev:
      state = 3
#      print('0 -> 2')
  elif state in {2}: # Acsending state
    if A[i] < prev:
      state = 3
      if len > 1:
        ANS = ANS + 1
        len = 0
#      print(prev, ', ', A[i], ', ', '1 -> 2')
  elif state in {3}: # Descending state
    if A[i] > prev:
      state = 2
      if len > 1:
        ANS = ANS + 1
        len = 0
#      print(prev, ', ', A[i], ', ', '2 -> 1')
  prev = A[i]
#if len == 0:
#  ANS = ANS + 1
print(ANS)

Submission status: Wrong Answer

Solution code:
N = input()
state = 0
max = 0
min = 0
prev = 0
len = 0
ANS = 1
S = input()
A = list(map(int,S.split()))
 
for i in range(0,int(N)):
#  print('s=', state, 'in=', A[i], 'prev=', prev, 'ANS=', ANS, 'len=', len)
  len = len + 1
  if state in {0}:	# Initial state
      state = 1
  elif state in {1}: # Newtral state
    if A[i] > prev:
      state = 2
#      print('0 -> 1')
    elif A[i] < prev:
      state = 3
#      print('0 -> 2')
  elif state in {2}: # Acsending state
    if A[i] < prev:
      state = 3
      if len > 1:
        ANS = ANS + 1
        len = 0
#      print(prev, ', ', A[i], ', ', '1 -> 2')
  elif state in {3}: # Descending state
    if A[i] > prev:
      state = 2
      if len > 1:
        ANS = ANS + 1
        len = 0
#      print(prev, ', ', A[i], ', ', '2 -> 1')
  prev = A[i]
if len == 0:
  ANS = ANS + 1
print(ANS)

Submission status: Wrong Answer

Solution code:
N = input()
state = 0
max = 0
min = 0
prev = 0
len = 0
ANS = 1
S = input()
A = list(map(int,S.split()))
 
for i in range(0,int(N)):
#  print('s=', state, 'in=', A[i], 'prev=', prev, 'ANS=', ANS, 'len=', len)
  len = len + 1
  if state in {0}:	# Initial state
      state = 1
  elif state in {1}: # Newtral state
    if A[i] > prev:
      state = 2
#      print('0 -> 1')
    elif A[i] < prev:
      state = 3
#      print('0 -> 2')
  elif state in {2}: # Acsending state
    if A[i] < prev:
      state = 3
      if len > 1:
        ANS = ANS + 1
        len = 0
#      print(prev, ', ', A[i], ', ', '1 -> 2')
  elif state in {3}: # Descending state
    if A[i] > prev:
      state = 2
      if len > 1:
        ANS = ANS + 1
        len = 0
#      print(prev, ', ', A[i], ', ', '2 -> 1')
  prev = A[i]
if len == 1:
  ANS = ANS + 1
print(ANS)

Submission status: Wrong Answer

Solution code:
N = input()
state = 0
max = 0
min = 0
prev = 0
ANS = 1
S = input()
A = list(map(int,S.split()))
 
for i in range(0,int(N)):
  if state in {0}:	# Initial state
    if A[i] > prev:
      state = 1
#      print('0 -> 1')
    elif A[i] < prev:
      state = 2
#      print('0 -> 2')
    prev = A[i]
  elif state in {1}: # Acsending state
    if A[i] < prev:
      state = 2
      ANS = ANS + 1
#      print(prev, ', ', A[i], ', ', '1 -> 2')
  elif state in {2}: # Descending state
    if A[i] > prev:
      state = 1
      ANS = ANS + 1
#      print(prev, ', ', A[i], ', ', '2 -> 1')
  prev = A[i]
print(ANS)

Submission status: Wrong Answer

Solution code:
N = input()
AD = True
EF = True
ANS = 1
S = input()
A = list(map(int,S.split()))

for i in range(0,int(N)):
	if i >= 1:
		if A[i] > A[i-1] and AD == False and EF == False:
			EF = True
			ANS += 1
		elif A[i] < A[i-1] and AD == True and EF == False:
			EF = True
			ANS += 1
		elif EF == True:
			EF = False
			if A[i] > A[i-1]:
				AD = True
			else:
				AD = False

print(ANS)

Submission status: Wrong Answer

Solution code:
N = input()
A = []
AD = True
EF = True
ANS = 1
S = input()
A = S.split()

for i in range(0,int(N)):
	if i >= 1:
		if A[i] > A[i-1] and AD == False and EF == False:
			EF = True
			ANS += 1
		elif A[i] < A[i-1] and AD == True and EF == False:
			EF = True
			ANS += 1
		elif EF == True:
			EF = False
			if A[i] > A[i-1]:
				AD = True
			else:
				AD = False

print(ANS)

Submission status: Wrong Answer

Solution code:
N = input()
A = input().split()
AD = True
EF = True
ANS = 1

for i in range(0,int(N)):
	if i >= 1:
		if A[i] == A[i - 1]:
			EF = False
		if A[i] > A[i-1] and AD == False and EF == False:
			EF = True
			ANS += 1
		elif A[i] < A[i-1] and AD == True and EF == False:
			EF = True
			ANS += 1
		elif EF == True:
			EF = False
			if A[i] == A[i - 1]:
				EF = True
			elif A[i] > A[i-1]:
				AD = True
			else:
				AD = False

print(ANS)

Submission status: Wrong Answer

Solution code:
N = input()
A = input().split()
AD = True
EF = True
ANS = 1

for i in range(0,int(N)):
	if i >= 1:
		if A[i] > A[i-1] and AD == False and EF == False:
			EF = True
			ANS += 1
		elif A[i] < A[i-1] and AD == True and EF == False:
			EF = True
			ANS += 1
		elif EF == True:
			EF = False
			if A[i] == A[i - 1]:
				EF = True
			elif A[i] > A[i-1]:
				AD = True
			else:
				AD = False

print(ANS)

Submission status: Wrong Answer

Solution code:
N = input()
A = input().split()
AD = True
EF = True
ANS = 0

for i in range(0,int(N)):
	if i >= 1:
		if A[i] >= A[i-1] and AD == False and EF == False:
			EF = True
			ANS += 1
		elif A[i] <= A[i-1] and AD == True and EF == False:
			EF = True
			ANS += 1
		if EF == True:
			if A[i] >= A[i-1]:
				AD = True
				EF = False
			elif A[i] <= A[i-1]:
				AD = False
				EF = False

print(ANS)

Submission status: Wrong Answer

Solution code:
N = input()
A = input().split()
AD = True
EF = True
ANS = 1

for i in range(0,int(N)):
	if i >= 1:
		if A[i] > A[i-1] and AD == False and EF == False:
			EF = True
			ANS += 1
		elif A[i] < A[i-1] and AD == True and EF == False:
			EF = True
			ANS += 1
		elif EF == True:
			EF = False
			if A[i] > A[i-1]:
				AD = True
			else:
				AD = False

print(ANS)

Submission status: Wrong Answer

Solution code:
N = input()
A = input().split()
AD = True
EF = True
ANS = 0

for i in range(0,int(N)):
	if i >= 1:
		if A[i] > A[i-1] and AD == False and EF == False:
			EF = True
			ANS += 1
		elif A[i] < A[i-1] and AD == True and EF == False:
			EF = True
			ANS += 1
		if EF == True:
			if A[i] > A[i-1]:
				AD = True
				EF = False
			elif A[i] < A[i-1]:
				AD = False
				EF = False

print(ANS)

Submission status: Wrong Answer

Solution code:
N = input()
A = input().split()
AD = True
EF = True
ANS = 1

for i in range(0,int(N)):
	if i >= 1:
		if A[i] > A[i-1] and AD == False and EF == False:
			EF = True
			ANS += 1
		elif A[i] < A[i-1] and AD == True and EF == False:
			EF = True
			ANS += 1
		if EF == True:
			if A[i] > A[i-1]:
				AD = True
				EF = False
			elif A[i] < A[i-1]:
				AD = False
				EF = False

print(ANS)

Submission status: Wrong Answer

Solution code:
N = input()
A = input().split()
AD = True
EF = True
ANS = 1

for i in range(0,int(N)):
	if i >= 1:
		if A[i] > A[i-1] and AD == False and EF == False:
			EF = True
			ANS += 1
		elif A[i] < A[i-1] and AD == True and EF == False:
			EF = True
			ANS += 1
		elif EF == True:
			if A[i] > A[i-1]:
				AD = True
			else:
				AD = False
				EF = False

print(ANS)

Submission status: Wrong Answer

Solution code:
N = input()
A = input().split()
AD = True
EF = True
ANS = 1



for i in range(0,int(N)):
	if i >= 1:
		if A[i] > A[i-1] and AD == False and EF == False:
			EF = True
			ANS += 1
		elif A[i] < A[i-1] and AD == True and EF == False:
			EF = True
			ANS += 1
		elif EF == True:
			EF = False
			if A[i] > A[i-1]:
				AD = True
			else:
				AD = False

print(ANS)

Submission status: Wrong Answer

Solution code:
N = input()
A = input().split()
AD = True
EF = False
ANS = 1

for i in range(0,int(N)):
	if i >= 1:
		if A[i] > A[i-1] and AD == False and EF == False:
			EF = True
			ANS += 1
		elif A[i] < A[i-1] and AD == True and EF == False:
			EF = True
			ANS += 1
		elif EF == True:
			EF = False
			if A[i] > A[i-1]:
				AD = True
			else:
				AD = False

print(ANS)

Submission status: Wrong Answer

Solution code:
N = input()
A = input().split()
AD = True
EF = False
ANS = 1



for i in range(0,int(N)):
	if i >= 1:
		if A[i] > A[i-1] and AD == False and EF == False:
			EF = True
			ANS += 1
		elif A[i] < A[i-1] and AD == True and EF == False:
			EF = True
			ANS += 1
		elif EF == True:
			EF = False
			if A[i] > A[i-1]:
				AD = True
			else:
				AD = False

print(ANS)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
src = list(map(int,input().split()))
asc = 0
ans = 1
for i in range(N-1):
    d = src[i+1] - src[i]
    if asc * d < 0:
        ans += 1
        asc = 0
    else:
        asc = d
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
INF = float("inf")


def main():
    N = int(input())
    a_list = list(map(int, input().split()))

    ans = 0
    state = None
    for i in range(N - 1):
        now = a_list[i] < a_list[i + 1]
        if state is None:
            state = now
            continue
        if state != now:
            ans += 1
            state = None

    print(ans + 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(x) for x in input().split(" ")]
A = [max(min(A[i + 1] - A[i], 1), -1) for i in range(N - 1) if A[i + 1] - A[i] != 0]
print((len([0 for i in range(len(A) - 1) if A[i] != A[i + 1]]) + 1) // 2 + 1)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(x) for x in input().split(" ")]
A = [max(min(A[i + 1] - A[i], 1), -1) for i in range(N - 1) if A[i + 1] - A[i] != 0]
print(len([0 for i in range(len(A) - 1) if A[i] != A[i + 1]]) + 1)

Submission status: Wrong Answer

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

count = 1
state = 'none'
b = [a[0]]

for i in range(1,n):
    if state=='none':
        before=b.pop()
        now = a[i]
        if before<now:
            b.append(now)
            state='incr'
        elif before>now:
            b.append(now)
            state='decr'
        else:
            b.append(now)
    elif state=='incr':
        before=b.pop()
        now = a[i]
        if before<now:
            b.append(now)
        else:
            b=[now]
            state='none'
            count+=1
    elif state=='decr':
        before=b.pop()
        now = a[i]
        if before>now:
            b.append(now)
        else:
            b=[now]
            state='none'
            count+=1
print(count)

Submission status: Wrong Answer

Solution code:
from sys import stdin

def main():
    global lista
    n = int(stdin.readline())
    lista = [int(x) for x in stdin.readline().strip().split()]

    inx = 0
    inx  = cuenta(inx)
    flag = inx < len(lista)-1 and lista[inx] >= lista[inx+1];
    cont = 0
    tempc = 0
    while(inx < len(lista)-1):
        if flag:
            if(lista[inx] >= lista[inx+1]):
                flag = True
                tempc+=1
            else:
                tempc=0
                cont+=1
                flag = inx < len(lista)-2 and lista[inx+1] >= lista[inx+2];
        else:
            if(lista[inx] <= lista[inx+1]):
                flag = False
                tempc+=1
            else:
                tempc=0
                cont+=1
                flag = inx < len(lista)-2 and lista[inx+1] >= lista[inx+2];
        inx+=1
    print(cont+1)

def cuenta(i):
    while(i < len(lista)-1 and lista[i] == lista[i+1]):
        i+=1
    return i

main()

Submission status: Wrong Answer

Solution code:
from sys import stdin

def main():
    n = int(stdin.readline())
    lista = [int(x) for x in stdin.readline().strip().split()]

    inx = 0
    flag = inx < len(lista)-1 and lista[inx] >= lista[inx+1];
    cont = 0
    tempc = 0
    while(inx < len(lista)-1):
        if flag:
            if(lista[inx] >= lista[inx+1]):
                flag = True
                tempc+=1
            else:
                tempc=0
                cont+=1
                flag = inx < len(lista)-2 and lista[inx+1] >= lista[inx+2];
        else:
            if(lista[inx] <= lista[inx+1]):
                flag = False
                tempc+=1
            else:
                tempc=0
                cont+=1
                flag = inx < len(lista)-2 and lista[inx+1] >= lista[inx+2];
        inx+=1

    print(cont+1)

main()

Submission status: Wrong Answer

Solution code:
from sys import stdin

def main():
	N = int(stdin.readline().strip())
	array = [int(x) for x in stdin.readline().strip().split()]
	ans,i= 0,0
	while i+1 < N:
		if array[i] < array[i+1]:
			ans+=1
			while i+1 < N and array[i] < array[i+1]:
				i+=1
			i+=1
		elif array[i] > array[i+1]:
			ans+=1
			while i+1 < N and array[i] > array[i+1]:
				i+=1
			i+=1
		else:
			i+=1
	if i+1 == N or ans == 0:
		ans+=1
	print(ans)
main()

Submission status: Wrong Answer

Solution code:
from sys import stdin

def main():
	N = int(stdin.readline().strip())
	array = [int(x) for x in stdin.readline().strip().split()]
	ans,i,ascending,decending = 0,0,False,False
	while i+1 < N:
		if array[i] < array[i+1]:
			ans+=1
			while i+1 < N and array[i] < array[i+1]:
				i+=1
			i+=1
		elif array[i] > array[i+1]:
			ans+=1
			while i+1 < N and array[i] > array[i+1]:
				i+=1
			i+=1
		else:
			i+=1
	if i+1 == N:
		ans+=1
	print(ans)
main()

Submission status: Wrong Answer

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

count=1
z1=0
for i in range(N-1):
    if A[i+1]-A[i]>0:
        z2 = 1
    elif A[i+1]-A[i]<0:
        z2 = -1
    elif A[i+1]-A[i]==0:
        z2 = 0
    if z2*z1<0:
        count+=1
    if z2!=0:
        z1=z2

print str(count)

Submission status: Wrong Answer

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

count=1
z1=0
for i in range(N-1):
    if A[i+1]-A[i]>0:
        z2=1
    elif A[i+1]-A[i]<0:
        z2=-1
    elif A[i+1]-A[i]==0:
        z2=0
    if z2*z1<0:
        count+=1
    if z2!=0:
        z1=z2

print str(count)

Submission status: Wrong Answer

Solution code:
# 整数の入力
n=int(input())
a=list(map(int, input().split()))

p=1
flag=0 #増加中なら1,減少中なら0

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

Submission status: Wrong Answer

Solution code:
import sys

def solve():
    n = int(input())
    a = [int(i) for i in input().split()]

    ans = 0

    mode = 0

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

    ans += 1

    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys

N=int(input())
if N<=2:
    print(1)
    sys.exit()

A=[int(i) for i in input().split(" ")]


count=1
i=1
while i<N-1:
    if A[i]>A[i+1] and A[i]>A[i-1]:
        count+=1
        i+=1
    elif A[i]<A[i+1] and A[i]<A[i-1]:
        count+=1
        i+=1
    i+=1

print(count)

Submission status: Wrong Answer

Solution code:
def slv(a):
    for i in reversed(range(1,len(a) + 1)):
        b = a[:i]
        b.sort()
        if len(set(b)) == len(b):
            a = a[i:]
            return a

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

s = 0

while True:
    a = slv(a)
    s += 1
    if len(a) == 0:
        break

print(s)

Submission status: Wrong Answer

Solution code:
def cutter(nums, idx):
    if len(nums) - idx <= 2:
        return len(nums)
    
    for i in xrange(idx, len(nums)):
        if int(nums[i]) != int(nums[i-1]):
            pos = i
            flag = int(nums[i]) > int(nums[i-1])  # is asc?
            # print flag
            break
    else:
        return len(nums)

    for i in xrange(pos, len(nums)):

        if flag:
            if int(nums[i]) < int(nums[i-1]):
                return i
        else:
            if int(nums[i]) > int(nums[i-1]):
                return i
    return len(nums)


n = int(raw_input())
idx = 1
arr = raw_input().split()
if n < 2:
    print n
else:
    count = 0
    while idx < n:
        # print arr
        count += 1
        idx = cutter(arr, idx)

    print count

Submission status: Wrong Answer

Solution code:
def cutter(nums, idx):
    if len(nums) < 2:
        return len(nums)
    
    for i in xrange(idx, len(nums)):
        if int(nums[i]) != int(nums[i-1]):
            pos = i
            flag = int(nums[i]) > int(nums[i-1])  # is asc?
            # print flag
            break
    else:
        return len(nums)

    for i in xrange(pos, len(nums)):

        if flag:
            if int(nums[i]) < int(nums[i-1]):
                return i
        else:
            if int(nums[i]) > int(nums[i-1]):
                return i
    return len(nums)


n = int(raw_input())
idx = 1
arr = raw_input().split()
if n < 2:
    print n
else:
    count = 0
    while idx < n:
        # print arr
        count += 1
        idx = cutter(arr, idx)

    print count

Submission status: Wrong Answer

Solution code:
def cutter(nums):
    if len(nums) < 2:
        return []
    
    for i in xrange(1, len(nums)):
        if nums[i] != nums[i-1]:
            pos = i
            flag = nums[i] > nums[i-1]  # is asc?
            # print flag
            break
    else:
        return []

    for i in xrange(pos, len(nums)):

        if flag:
            if nums[i] < nums[i-1]:
                return nums[i:]
        else:
            if nums[i] > nums[i-1]:
                return nums[i:]
    return []


n = int(raw_input())
arr = raw_input().split()
if n < 2:
    print n
else:
    count = 0
    while arr:
        # print arr
        count += 1
        arr = cutter(arr)

    print count

Submission status: Wrong Answer

Solution code:
def cutter(nums):
    if len(nums) < 2:
        return []
    
    for i in xrange(1, len(nums)):
        if nums[i] != nums[i-1]:
            pos = i
            flag = nums[i] > nums[i-1]  # is asc?
            # print flag
            break
    else:
        return []

    for i in xrange(pos, len(nums)):

        if flag:
            if nums[i] < nums[i-1]:
                return nums[i:]
        else:
            if nums[i] > nums[i-1]:
                return nums[i:]
    return []


n = int(raw_input())
arr = raw_input().split()
if n < 2:
    print n
count = 0
while arr:
    # print arr
    count += 1
    arr = cutter(arr)

print count