Problem: 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?

Submission attempts (in order):
N, M = list(map(int, input().split()))
line_list = list()
for _ in range(M):
    a, b = list(map(int, input().split()))
    line_list.append((a, b))

path_points = [ line_list[0][0], line_list[0][1] ]

for i in range(1, M):
    if line_list[i][0] not in path_points and line_list[i][1] not in path_points:
        break

    if   line_list[i][0] in path_points and line_list[i][1] not in path_points:
        path_points.append(line_list[i][1])
    elif line_list[i][1] in path_points and line_list[i][0] not in path_points:
        path_points.insert(0, line_list[i][0])

for point in path_points:
    print(point)




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

num_split = 1
is_increase = None
len_A_list = len(A_list)

for i in range(len_A_list-1):
    if is_increase == None:
        if A_list[i] < A_list[i+1]:
            is_increase = True
        else:
            is_increase = False

    if is_increase and A_list[i] < A_list[i+1]:
        continue
    if (not is_increase) and A_list[i] > A_list[i+1]:
        continue

    num_split += 1
    is_increase = None

print(num_split)


import math

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

num_split = 1
sign = None
len_A_list = len(A_list)

for i in range(len_A_list-1):
    if sign == None:
        sign = (A_list[i+1] - A_list[i]) / math.fabs(A_list[i+1] - A_list[i])

    if (A_list[i+1] - A_list[i]) *  sign >= 0:
        continue

    num_split += 1
    sign = None

print(num_split)


N, L, T = list(map(int, input().split()))

X = []
W = []
X_after = []
for _ in range(N):
    x, w = list(map(int, input().split()))
    if w == 1:
        x_after = (x + T) % L
    else:
        x_after = (x - T) % L
        x_after = x_after if x_after >= 0 else L + x_after
    X.append(x)
    W.append(w)
    X_after.append(x_after)

n_colide = 0
for i in range(1, N):
    if W[0] == 1 and W[i] == 2:
        dist = X[i] - X[0]
        n_colide += 2 * (T - (dist / 2)) // L + 1
    elif W[0] == 2 and W[i] == 1:
        dist = (L + X[0]) - X[i]
        n_colide += 2 * (T - (dist / 2)) // L + 1

if W[0] == 1:
    num_after = n_colide % N
else:
    num_after = N - (n_colide % N)

num_after = int(num_after)

x_after_first = X_after[0]
X_after = sorted(X_after)
i = X_after.index(x_after_first)
X_after = X_after[i-num_after:] + X_after[:i-num_after]

for x_after in X_after:
    print(x_after)



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

num_split = 1
diff = None
len_A_list = len(A_list)

for i in range(len_A_list-1):
    if diff == None:
        diff = A_list[i+1] - A_list[i]

    if A_list[i+1] - A_list[i] == diff:
        continue

    num_split += 1
    diff = None

print(num_split)


import math

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

num_split = 1
sign = None
len_A_list = len(A_list)

for i in range(len_A_list-1):
    if sign == None:
        if A_list[i+1] - A_list[i] == 0:
            continue
        sign = (A_list[i+1] - A_list[i]) / math.fabs(A_list[i+1] - A_list[i])

    if (A_list[i+1] - A_list[i]) *  sign >= 0:
        continue

    num_split += 1
    sign = None

print(num_split)


Verdicts: Runtime Error

Wrong Answer

Runtime Error

Runtime Error

Wrong Answer

Accepted