AtCoder Beginner Contest 132 - Ordinary Number

We have a permutation p = {p_1,\ p_2,\ ...,\ p_n} of {1,\ 2,\ ...,\ n}.
Print the number of elements p_i (1 < i < n) that satisfy the following condition:
 - p_i is the second smallest number among the three numbers p_{i - 1}, p_i, and p_{i + 1}.

-----Constraints-----
 - All values in input are integers.
 - 3 \leq n \leq 20
 - p is a permutation of {1,\ 2,\ ...,\ n}.

-----Input-----
Input is given from Standard Input in the following format:
n
p_1 p_2 ... p_n

-----Output-----
Print the number of elements p_i (1 < i < n) that satisfy the condition.

-----Sample Input-----
5
1 3 5 4 2

-----Sample Output-----
2

p_2 = 3 is the second smallest number among p_1 = 1, p_2 = 3, and p_3 = 5. Also, p_4 = 4 is the second smallest number among p_3 = 5, p_4 = 4, and p_5 = 2. These two elements satisfy the condition.

Source: atcoder | Difficulty: EASY


Solutions:
n = int(input())
p = list(map(int, input().split()))
cnt = 0
for i in range(n - 2):
	if p[i] < p[i + 1] and p[i + 1] < p[i + 2] or (p[i] > p[i + 1] and p[i + 1] > p[i + 2]):
		cnt += 1
print(cnt)


(n, *p) = map(int, open(0).read().split())
cnt = 0
for i in range(1, n - 1):
	if min(p[i - 1], p[i + 1]) <= p[i] <= max(p[i - 1], p[i + 1]):
		cnt += 1
print(cnt)


n = int(input())
p = list(map(int, input().split()))
count = 0
for i in range(1, n - 1):
	(p1, p2, p3) = (p[i - 1], p[i], p[i + 1])
	if p1 < p2 < p3 or p3 < p2 < p1:
		count += 1
print(count)


n = int(input())
l = list(map(int, input().split()))
sum = 0
for i in range(n - 2):
	if l[i] < l[i + 1] < l[i + 2] or l[i] > l[i + 1] > l[i + 2]:
		sum += 1
print(sum)


n = int(input())
p = list(map(int, input().split()))
answer = 0
for i in range(n - 2):
	if p[i] < p[i + 1] < p[i + 2]:
		answer += 1
	if p[i] > p[i + 1] > p[i + 2]:
		answer += 1
print(answer)


n = int(input())
P = list(map(int, input().split()))
ans = 0
for i in range(1, n - 1):
	ans += P[i - 1] < P[i] < P[i + 1] or P[i - 1] > P[i] > P[i + 1]
print(ans)


n = int(input())
arr = list(map(int, input().split()))
count = 0
for (i, num) in enumerate(arr):
	if i == 0 or i == n - 1:
		continue
	target_arr = [arr[i - 1], arr[i], arr[i + 1]]
	if sorted(target_arr)[1] == arr[i]:
		count += 1
print(count)


N = int(input())
P = list(map(int, input().split()))
print(sum((1 for (a, b, c) in zip(P, P[1:], P[2:]) if b == sorted((a, b, c))[1])))


n = int(input())
l = list(map(int, input().split()))
c = 0
for i in range(n - 2):
	if sorted(l[i:i + 3])[-2] == l[i + 1]:
		c += 1
print(c)


n = int(input())
p = list(map(int, input().split()))
l = 0
a = 0
for i in range(1, n - 1):
	l = sorted([p[i - 1], p[i], p[i + 1]])
	if l[1] == p[i]:
		a += 1
print(a)


n = int(input())
p = list(map(int, input().split()))
ans = 0
for i in range(1, n - 1):
	if (p[i] - p[i - 1]) * (p[i] - p[i + 1]) < 0:
		ans += 1
print(ans)


(_, p) = open(0)
(*p,) = map(int, p.split())
print(sum((sorted(a)[1] == a[1] for a in zip(p, p[1:], p[2:]))))


N = int(input())
a = list(map(int, input().split()))
b = 0
for i in range(N - 2):
	if a[i] < a[i + 1] < a[i + 2] or a[i] > a[i + 1] > a[i + 2]:
		b = b + 1
print(b)


n = int(input())
p = list(map(int, input().split()))
c = 0
for i in range(n - 2):
	c += 1 if (p[i + 1] - p[i]) * (p[i + 1] - p[i + 2]) < 0 else 0
print(c)


n = int(input())
L = list(map(int, input().split()))
cnt = 0
for i in range(n - 2):
	s = L[i:i + 3][1]
	A = sorted(L[i:i + 3])
	t = A[1]
	if s == t:
		cnt += 1
print(cnt)


n = int(input())
p = list(map(int, input().split()))
count = 0
for i in range(n - 2):
	if sorted(p[i:i + 3])[1] == p[i + 1]:
		count += 1
print(count)


s = 0
x = int(input())
y = list(map(int, input().split()))
for i in range(x - 2):
	if y[i] < y[i + 1] < y[i + 2] or y[i + 2] < y[i + 1] < y[i]:
		s += 1
print(s)


n = int(input())
p = list(map(int, input().split()))
s = [x for x in range(1, n - 1) if p[x - 1] < p[x] < p[x + 1] or p[x - 1] > p[x] > p[x + 1]]
print(len(s))


input()
ls = list(map(int, input().split()))
p = 0
for i in range(1, len(ls) - 1):
	a = sorted(ls[i - 1:i + 2])
	p += a[1] == ls[i]
print(p)


n = int(input())
s = list(map(int, input().split()))
c = 0
for i in range(n - 2):
	if (s[i + 1] - s[i]) * (s[i + 1] - s[i + 2]) < 0:
		c += 1
print(c)


n = int(input())
p = list(map(int, input().split()))
ans = 0
for i in range(1, n - 1):
	if p[i] != min(p[i - 1], p[i], p[i + 1]) and p[i] != max(p[i - 1], p[i], p[i + 1]):
		ans += 1
print(ans)


n = int(input())
perm_list = list(map(int, input().split()))
ans = 0
for i in range(n - 2):
	a = perm_list[i]
	b = perm_list[i + 1]
	c = perm_list[i + 2]
	if a < b < c or c < b < a:
		ans += 1
print(ans)


n = int(input())
p = list(map(int, input().split()))
o = 0
for i in range(1, n - 1):
	if p[i - 1] > p[i] > p[i + 1] or p[i + 1] > p[i] > p[i - 1]:
		o += 1
print(o)


a = input()
n = int(a)
p = list(map(int, input().split()))
count = 0
for i in range(1, n - 1):
	if p[i - 1] < p[i] and p[i] <= p[i + 1] or (p[i + 1] < p[i] and p[i] <= p[i - 1]):
		count = count + 1
print(count)


def c(x, s):
	return [x[i:i + s] for i in range(len(x) - s + 1)]
input()
print([sorted(i)[1] == i[1] for i in c(list(map(int, input().split())), 3)].count(True))


n = int(input())
l_n = list(map(int, input().split()))
ans_count = 0
for i in range(n - 2):
	a = l_n[i]
	b = l_n[i + 1]
	c = l_n[i + 2]
	if a <= b <= c or c <= b <= a:
		ans_count += 1
print(ans_count)


n = int(input())
x = list(map(int, input().split()))
ans = 0
for i in range(1, n - 1):
	if x[i - 1] <= x[i] <= x[i + 1] or x[i + 1] <= x[i] <= x[i - 1]:
		ans += 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n - 2):
	if a[i + 1] == sorted(a[i:i + 3])[1]:
		ans += 1
print(ans)


n = int(input())
p = [int(x) for x in input().split()]
print(sum([1 if min(p[i:i + 3]) < p[i + 1] < max(p[i:i + 3]) else 0 for i in range(n - 2)]))


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


n = int(input())
P = tuple(map(int, input().split()))
c = 0
for x in zip(P, P[1:], P[2:]):
	c += int(x[1] == sorted(x)[1])
print(c)


n = int(input())
p = tuple(map(int, input().split()))
c = 0
for i in range(n - 2):
	tmp = sorted(p[i:i + 3])
	if tmp[1] == p[i + 1]:
		c += 1
print(c)


N = int(input())
P = list(map(int, input().split()))
count = 0
for i in range(0, N - 2):
	max_P = max(P[i], P[i + 1], P[i + 2])
	min_P = min(P[i], P[i + 1], P[i + 2])
	if P[i + 1] != max_P and P[i + 1] != min_P:
		count += 1
print(count)


n = int(input())
p = list(map(int, input().split()))
print(sum([1 for i in range(n - 2) if not (min(p[i:i + 3]) == p[i + 1] or max(p[i:i + 3]) == p[i + 1])]))


n = int(input())
l = list(map(int, input().split()))
j = 0
for i in range(1, n - 1):
	if l[i - 1] > l[i] >= l[i + 1] or l[i - 1] <= l[i] < l[i + 1]:
		j += 1
print(j)


n = int(input())
p = [int(x) for x in input().split()]
c = 0
for i in range(1, n - 1):
	if (p[i - 1] - p[i]) * (p[i + 1] - p[i]) < 0:
		c += 1
print(c)


N = int(input())
P = list(map(int, input().split()))
answer = sum((x > y > z or x < y < z for (x, y, z) in zip(P[:-2], P[1:-1], P[2:])))
print(answer)


input()
a = list(map(int, input().split()))
print(sum((s[1] == sorted(s)[1] for s in zip(a, a[1:], a[2:]))))


input()
s = map(int, input().split(' '))
s = list(s)
c = 0
for i in range(1, len(s) - 1):
	ss = s[i - 1:i + 2]
	ss = sorted(ss)
	if ss[1] == s[i]:
		c += 1
print(c)


N = int(input())
P = list(map(int, input().split()))
ans = 0
for i in range(N - 2):
	p = P[i:i + 3]
	p.sort()
	if p[1] == P[i + 1]:
		ans += 1
print(ans)


n = int(input())
p = [int(s) for s in input().split()]
count = 0
for i in range(len(p) - 2):
	if p[i] < p[i + 1] < p[i + 2] or p[i] > p[i + 1] > p[i + 2]:
		count += 1
print(count)


n = int(input())
l = list(map(int, input().split()))
a = 0
for i in range(n - 2):
	m = l[i:i + 3]
	j = m[1]
	m.sort()
	if j == m[1]:
		a += 1
print(a)


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


X = int(input())
N = [int(i) for i in input().split()]
a = 0
for i in range(X - 2):
	if sorted(N[i:i + 3])[1] == N[i:i + 3][1]:
		a += 1
print(a)


N = int(input())
(*A,) = map(int, input().split())
ans = [1 for i in range(1, N - 1) if A[i - 1] < A[i] < A[i + 1] or A[i + 1] < A[i] < A[i - 1]]
print(sum(ans))


n = int(input())
p = [int(_) for _ in input().split()]
a = 0
for i in range(n - 2):
	if sorted(p[i:i + 3])[1] == p[i + 1]:
		a += 1
print(a)


n = int(input())
p = list(map(int, input().split()))
a = 0
for i in range(n - 2):
	a += int((p[i + 1] - p[i]) * (p[i + 2] - p[i + 1]) > 0)
print(a)


n = int(input())
p = list(map(int, input().split()))
print(sum([1 for i in range(1, n - 1) if p[i] == sorted(p[i - 1:i + 2])[1]]))


import collections
n = int(input())
p = list(map(int, input().split()))
c = 0
for i in range(n - 2):
	if p[i] < p[i + 1] and p[i + 1] < p[i + 2] or (p[i] > p[i + 1] and p[i + 1] > p[i + 2]):
		c += 1
print(c)


n = int(input())
data = list(map(int, input().split()))
point = 0
i = 1
while i <= len(data) - 2:
	if data[i - 1] < data[i] and data[i] < data[i + 1]:
		point += 1
	if data[i - 1] > data[i] and data[i] > data[i + 1]:
		point += 1
	i += 1
print(point)


input()
a = list(map(int, input().split()))
print(sum([sorted(a[i:i + 3])[1] == a[i + 1] for i in range(len(a) - 2)]))


N = int(input())
P = list(map(int, input().split()))
res = 0
for i in range(N - 2):
	p = P[i:i + 3]
	if p[1] == sorted(p)[1]:
		res += 1
print(res)


n = int(input())
p = list(map(int, input().split()))
cnt = 0
for i in range(1, n - 1):
	second = p[i]
	scope = sorted([p[i - 1], p[i], p[i + 1]])
	if scope[0] < second and second < scope[2]:
		cnt += 1
print(cnt)


N = int(input())
L = list(map(int, input().split()))
C = 0
for i in range(1, N - 1):
	C += 1 if sorted(L[i - 1:i + 2])[1] == L[i] else 0
print(C)


n = int(input())
li = list(map(int, input().split()))
cnt = 0
for i in range(1, n - 1):
	cnt += li[i - 1] < li[i] < li[i + 1] or li[i + 1] < li[i] < li[i - 1]
print(cnt)


n = int(input())
l = list(map(int, input().split()))
cnt = 0
for i in range(1, n - 1):
	if (l[i - 1] - l[i]) * (l[i + 1] - l[i]) < 0:
		cnt += 1
print(cnt)


n = int(input())
P = list(map(int, input().split()))
K = 0
for i in range(n - 2):
	if P[i] < P[i + 1] and P[i + 1] < P[i + 2] or (P[i + 2] < P[i + 1] and P[i + 1] < P[i]):
		K += 1
print(K)


n = int(input())
P = [int(i) for i in input().split()]
c = 0
for i in range(n - 2):
	if P[i] < P[i + 1] < P[i + 2] or P[i] > P[i + 1] > P[i + 2]:
		c += 1
print(c)


n = int(input())
lst = list(map(int, input().split()))
if lst[0] > lst[1]:
	di = 1
else:
	di = -1
ans = 0
now = lst[1]
for i in range(2, n):
	if di == 1 and now > lst[i]:
		ans += 1
	elif di == -1 and now < lst[i]:
		ans += 1
	else:
		di = -di
	now = lst[i]
print(ans)


N = int(input())
L = list(map(int, input().split()))
ans = 0
for i in range(N - 2):
	A = L[i:i + 3]
	t = A[1]
	A.sort()
	ans += 1 if t == A[1] else 0
print(ans)


n = int(input())
p = list(map(int, input().split()))
c = 0
for i in range(n - 2):
	t = p[i:i + 3]
	u = sorted(t)
	if t[1] == u[1]:
		c += 1
print(c)


n = int(input())
P = list(map(int, input().split()))
cnt = 0
for i in range(1, n - 1):
	if min(P[i - 1], P[i], P[i + 1]) < P[i] and max(P[i - 1], P[i], P[i + 1]) > P[i]:
		cnt += 1
print(cnt)


n = int(input())
a = [int(num) for num in input().split()]
count = 0
for i in range(1, n - 1):
	if a[i - 1] < a[i] and a[i] < a[i + 1]:
		count += 1
	elif a[i - 1] > a[i] and a[i] > a[i + 1]:
		count += 1
print(count)


n = int(input())
p = list(map(int, input().split()))
cnt = 0
for i in range(n - 2):
	l = [p[i], p[i + 1], p[i + 2]]
	l.sort()
	if p[i + 1] == l[1]:
		cnt += 1
print(cnt)


n = int(input())
a = list(map(int, input().split()))
t = 0
for i in range(1, n - 1):
	if sorted(a[i - 1:i + 2]).index(a[i]) == 1:
		t += 1
print(t)


n = int(input())
p = list(map(int, input().split()))
ans = sum((1 for i in range(n - 2) if p[i] < p[i + 1] < p[i + 2] or p[i] > p[i + 1] > p[i + 2]))
print(ans)


input()
P = list(map(int, input().split()))
print(sum((A < B < C or C < B < A for (A, B, C) in zip(P, P[1:], P[2:]))))


n = int(input())
pn = list(map(int, input().split()))
count = 0
for x in range(1, n - 1):
	if pn[x + 1] > pn[x] > pn[x - 1] or pn[x - 1] > pn[x] > pn[x + 1]:
		count += 1
print(count)


N = int(input())
p = list(map(int, input().split()))
cnt = 0
for i in range(1, N - 1):
	if p[i - 1] < p[i] < p[i + 1] or p[i - 1] > p[i] > p[i + 1]:
		cnt += 1
print(cnt)


N = int(input())
P = list(map(int, input().split()))
ans = 0
for (a, b, c) in zip(P, P[1:], P[2:]):
	if a < b < c or c < b < a:
		ans += 1
print(ans)


n = int(input())
P = [int(p) for p in input().split()]
print(sum([1 for n in range(1, n - 1) if min(P[n - 1], P[n + 1]) < P[n] < max(P[n - 1], P[n + 1])]))


(n, *ps) = map(int, open(0).read().split())
cou = 0
for (x, y, z) in zip(ps[:-2], ps[1:-1], ps[2:]):
	if (x - y) * (z - y) < 0:
		cou += 1
print(cou)


N = int(input())
P = list(map(int, input().split(' ')))
print(sum([1 for i in range(1, N - 1) if P[i - 1] < P[i] < P[i + 1] or P[i - 1] > P[i] > P[i + 1]]))


n = int(input())
a = list(map(int, input().split()))
print(sum([1 for i in range(1, n - 1) if a[i - 1] < a[i] and a[i] < a[i + 1] or (a[i + 1] < a[i] and a[i] < a[i - 1])]))


N = int(input())
P = list(map(int, input().split()))
cnt = 0
for i in range(1, N - 1):
	l = [P[i - 1], P[i], P[i + 1]]
	l.sort()
	if l[1] == P[i]:
		cnt += 1
print(cnt)


input()
p = [int(x) for x in input().split()]
q = [x - y for (x, y) in zip(p[1:], p[:])]
r = [1 for (x, y) in zip(q[1:], q[:]) if x * y > 0]
print(len(r))


N = int(input())
(*A,) = map(int, input().split())
ans = 0
for i in range(1, N - 1):
	if sorted(A[i - 1:i + 2])[1] == A[i]:
		ans += 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
print(len([True for i in range(1, n - 1) if max(a[i - 1:i + 2]) != a[i] and min(a[i - 1:i + 2]) != a[i]]))


input()
P = list(map(int, input().split()))
count = 0
for i in range(0, len(P) - 2):
	count += sorted([P[i], P[i + 1], P[i + 2]])[1] == P[i + 1]
print(count)


n = int(input())
p = [int(x) for x in input().split()]
ans = 0
for i in range(1, n - 1):
	if p[i - 1] < p[i] < p[i + 1] or p[i + 1] < p[i] < p[i - 1]:
		ans += 1
print(ans)


N = int(input())
P = list(map(int, input().split()))
ans = 0
for i in range(1, N - 1):
	if sorted(P[i - 1:i + 2])[1] == P[i]:
		ans += 1
print(ans)


n = int(input())
li = list(map(int, input().split()))
cnt = 0
for i in range(1, n - 1):
	if li[i - 1] < li[i] and li[i] < li[i + 1]:
		cnt += 1
	elif li[i - 1] > li[i] and li[i] > li[i + 1]:
		cnt += 1
print(cnt)


n = int(input())
list_p = [int(i) for i in input().split()]
count = 0
for i in range(1, n - 1):
	if min(list_p[i - 1], list_p[i], list_p[i + 1]) != list_p[i] and max(list_p[i - 1], list_p[i], list_p[i + 1]) != list_p[i]:
		count += 1
print(count)


n = int(input())
p = list(map(int, input().split()))
count = 0
for i in range(1, n - 1):
	a_list = [p[i - 1], p[i], p[i + 1]]
	b_list = sorted(a_list)
	if a_list[1] == b_list[1]:
		count += 1
print(count)


n = int(input())
l = list(map(int, input().split()))
a = 0
for i in range(n - 2):
	(p, q, r) = l[i:i + 3]
	if p < q < r or r < q < p:
		a += 1
print(a)


(N, *P) = map(int, open(0).read().split())
i = 1
ans = 0
while i < N - 1:
	if P[i - 1] < P[i] < P[i + 1] or P[i - 1] > P[i] > P[i + 1]:
		ans += 1
	i += 1
print(ans)


n = int(input())
p = [int(i) for i in input().split()]
print(sum([p[i] < p[i + 1] < p[i + 2] for i in range(n - 2)]) + sum([p[i] > p[i + 1] > p[i + 2] for i in range(n - 2)]))


from typing import Tuple

def answer(n: int, p: Tuple[int]) -> int:
	count = 0
	for i in range(1, n - 1):
		max_ = max(p[i - 1], p[i + 1])
		min_ = min(p[i - 1], p[i + 1])
		if min_ < p[i] < max_:
			count += 1
	return count

def main():
	n = int(input())
	p = tuple(map(int, input().split()))
	print(answer(n, p))

def __starting_point():
	main()
__starting_point()


N = int(input())
L = list(map(int, input().split()))
ans = 0
for i in range(N - 2):
	l = sorted(L[i:i + 3])
	if l[1] == L[i + 1]:
		ans += 1
print(ans)


n = int(input())
p = list(map(int, input().split()))
ans = 0
for i in range(1, n - 1):
	pref = sorted(p[i - 1:i + 2])
	if pref[1] == p[i]:
		ans += 1
print(ans)


n = int(input())
p = [int(c) for c in input().split()]
print(len([i for i in range(0, n - 2) if p[i] < p[i + 1] < p[i + 2] or p[i] > p[i + 1] > p[i + 2]]))


N = int(input())
n = list(map(int, input().split()))
cnt = 0
for i in range(N - 2):
	if max(n[0 + i], n[1 + i], n[2 + i]) > n[1 + i] > min(n[0 + i], n[1 + i], n[2 + i]):
		cnt += 1
print(cnt)


input()
(*p,) = map(int, input().split())
print(sum([p[i] == sorted(p[i - 1:i + 2])[1] for i in range(1, len(p) - 1)]))


a = int(input())
x = list(map(int, input().split()))
s = 0
for (i, j, k) in zip(x, x[1:], x[2:]):
	if i < j < k or k < j < i:
		s += 1
print(s)


n = int(input())
l = [int(u) for u in input().split()]
res = 0
for i in range(1, n - 1):
	if sorted(l[i - 1:i + 2])[1] == l[i]:
		res += 1
print(res)


_ = input()
nums = list(map(int, input().split()))
print(len([a for (a, b, c) in zip(nums, nums[1:], nums[2:]) if sorted([a, b, c])[1] == b]))


n = int(input())
p = list(map(int, input().split()))
ans_list = []
for i in range(1, n - 1):
	if p[i - 1] < p[i] < p[i + 1] or p[i + 1] < p[i] < p[i - 1]:
		ans_list.append(i)
print(len(ans_list))


n = int(input())
P = list(map(int, input().split()))
print(sum((p0 < p1 < p2 or p0 > p1 > p2 for (p0, p1, p2) in zip(P, P[1:], P[2:]))))


n = int(input())
p = input().split(' ')
for i in range(len(p)):
	p[i] = int(p[i])
cnt = 0
for i in range(1, n - 1):
	if p[i - 1] < p[i] and p[i] < p[i + 1]:
		cnt += 1
	elif p[i + 1] < p[i] and p[i] < p[i - 1]:
		cnt += 1
print(cnt)


n = int(input())
P = str(input())

def ans132(n: int, P: str):
	P = list(map(int, P.split()))
	jugde_count = 0
	for i in range(1, n - 1):
		n_list = sorted([P[i - 1], P[i], P[i + 1]])
		if n_list[1] == P[i]:
			jugde_count += 1
	return jugde_count
print(ans132(n, P))


permutation_number = int(input())
permutation = list(map(int, input().split()))
conditional_permutation = 0
for i in range(permutation_number - 2):
	if permutation[i] < permutation[i + 1] < permutation[i + 2] or permutation[i] > permutation[i + 1] > permutation[i + 2]:
		conditional_permutation += 1
print(conditional_permutation)


(_, p) = open(0)
(*p,) = map(int, p.split())
print(sum((a < b < c or c < b < a for (a, b, c) in zip(p, p[1:], p[2:]))))


(n, *p) = map(int, open(0).read().split())
c = 0
for i in range(2, n):
	t = p[i - 2:i + 1]
	c += sorted(t)[1] == t[1]
print(c)


input()
p = input().split()
print(sum((p[i + 1] == sorted(p[i:i + 3], key=int)[1] for i in range(len(p) - 2))))


n = int(input())
p = list(map(int, input().split()))
s = sum([p[i] < p[i + 1] < p[i + 2] or p[i] > p[i + 1] > p[i + 2] for i in range(n - 2)])
print(s)


n = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(n - 2):
	if A[i] < A[i + 1] < A[i + 2] or A[i] > A[i + 1] > A[i + 2]:
		ans += 1
print(ans)


n = int(input())
p = list(map(int, input().split()))
cnt = 0
for i in range(1, n - 1):
	if p[i - 1] > p[i]:
		if p[i] > p[i + 1]:
			cnt = cnt + 1
	elif p[i + 1] > p[i]:
		if p[i] > p[i - 1]:
			cnt = cnt + 1
print(cnt)


n = int(input())
p = list(map(int, input().split()))
Y = 0
i = 0
while i < n - 2:
	nums = p[i:i + 3]
	if sorted(nums)[-2] == nums[1]:
		Y += 1
	i += 1
print(Y)


n = int(input())
a = list(map(int, input().split()))
c = 0
for i in range(1, n - 1):
	l = a[i - 1:i + 2]
	if a[i] != max(l) and a[i] != min(l):
		c += 1
print(c)


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


n = int(input())
p = [int(i) for i in input().split()]
ans = 0
for i in range(n - 2):
	(a, b, c) = p[i:i + 3]
	if a < b < c or a > b > c:
		ans += 1
print(ans)


N = int(input())
p = list(map(int, input().split()))
s = 0
for i in range(1, N - 1):
	s += p[i - 1] < p[i] < p[i + 1] or p[i - 1] > p[i] > p[i + 1]
print(s)


a = int(input())
b = list(map(int, input().split()))
c = 0
for i in range(a - 2):
	if (b[i + 1] - b[i]) * (b[i + 2] - b[i + 1]) > 0:
		c = c + 1
print(c)


n = int(input())
P = list(map(int, input().split()))
assert n == len(P)
m = sum((1 for p in zip(P, P[1:], P[2:]) if p[1] == sorted(p)[1]))
print(m)


n = int(input())
ans = 0
p = list(map(int, input().split()))
for i in range(n - 2):
	a = p[i:i + 3]
	b = a[1]
	a.sort()
	if b == a[1]:
		ans += 1
print(ans)


n = int(input())
p = list(map(int, input().split()))
c = 0
for i in range(n - 2):
	if p[i + 1] != max(p[i:i + 3]) and p[i + 1] != min(p[i:i + 3]):
		c += 1
print(c)


(n, P) = (int(input()), list(map(int, input().split())))
print(sum([P[i + 1] == sorted(P[i:i + 3])[1] for i in range(n - 2)]))


n = int(input())
l = [int(k) for k in input().split()]
cont = 0
for i in range(1, n - 1):
	if l[i - 1] < l[i] < l[i + 1] or l[i + 1] < l[i] < l[i - 1]:
		cont += 1
print(cont)


input()
p = list(map(int, input().split()))
print(sum((pi < pj < pk or pk < pj < pi for (pi, pj, pk) in zip(p, p[1:], p[2:]))))


n = int(input())
P = list(map(int, input().split()))
count = 0
for i in range(1, len(P) - 1):
	p_ = [P[i - 1], P[i], P[i + 1]]
	if P[i] != min(p_) and P[i] != max(p_):
		count += 1
print(count)


n = int(input())
a = list(map(int, input().split()))
h = 0
for i in range(n - 2):
	if a[i] < a[i + 1] < a[i + 2] or a[i] > a[i + 1] > a[i + 2]:
		h += 1
print(h)


input()
v = list(map(int, input().split()))
c = 0
for (x, y, z) in zip(v[:-2], v[1:-1], v[2:]):
	if x < y < z or x > y > z:
		c += 1
print(c)


N = int(input())
P = list(map(int, input().split()))
a = 0
for n in range(N - 2):
	if P[n] < P[n + 1] < P[n + 2] or P[n + 2] < P[n + 1] < P[n]:
		a += 1
print(a)


n = int(input())
P = list(map(int, input().split()))
c = 0
for i in range(1, n - 1):
	if P[i - 1] < P[i] < P[i + 1] or P[i - 1] > P[i] > P[i + 1]:
		c += 1
print(c)


N = int(input())
P = list(map(int, input().split()))
count = 0
for n in range(2, N):
	count += P[n - 1] == sorted(P[n - 2:n + 1])[1]
print(count)


n = int(input())
list01 = list(map(int, input().split()))
list02 = []
list03 = []
a = 0
for i in range(n - 2):
	list02 = [list01[i], list01[i + 1], list01[i + 2]]
	list03 = sorted(list02)
	if list02[1] == list03[1]:
		a += 1
print(a)


n = int(input())
p = [int(i) for i in input().split()]
print(sum((p[i - 1] < p[i] and p[i] < p[i + 1] or (p[i - 1] > p[i] and p[i] > p[i + 1]) for i in range(1, n - 1))))


input()
a = list(map(int, input().split()))
b = 0
for i in range(1, len(a) - 1):
	b += a[i - 1] < a[i] and a[i] < a[i + 1]
	b += a[i - 1] > a[i] and a[i] > a[i + 1]
print(b)


n = int(input())
p = list(map(int, input().split()))
ans = 0
for i in range(n):
	if i == 0:
		pass
	elif i == n - 1:
		pass
	else:
		a = p[i - 1]
		b = p[i]
		c = p[i + 1]
		lst = [a, b, c]
		lst.sort()
		if lst[1] == b:
			ans = ans + 1
print(ans)


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


n = int(input())
S = map(int, input().split())
Number = list(S)
count_number = 0
for i in range(n - 2):
	if Number[0] > Number[1] > Number[2] or Number[2] > Number[1] > Number[0]:
		count_number += 1
	del Number[0]
print(count_number)


n = int(input())
p = list(map(int, input().split()))
cnt = 0
for i in range(n - 2):
	(a, b, c) = p[i:i + 3]
	if a < b < c or c < b < a:
		cnt += 1
print(cnt)


n = int(input())
p = list(map(int, input().split()))
c = 0
for i in range(n - 2):
	l = []
	l2 = []
	l.append(p[i])
	l.append(p[i + 1])
	l.append(p[i + 2])
	l2.append(p[i])
	l2.append(p[i + 1])
	l2.append(p[i + 2])
	l2.remove(max(l))
	l2.remove(min(l))
	if l2[0] == l[1]:
		c += 1
print(c)


n = int(input())
a = list(map(int, input().split()))
b = 0
for i in range(1, n - 1):
	if a[i - 1] <= a[i] <= a[i + 1] or a[i - 1] >= a[i] >= a[i + 1]:
		b += 1
print(b)


n = int(input())
p = list(map(int, input().split()))
ans = 0
for i in range(n - 2):
	List = p[i:i + 3]
	P = List[1]
	num = sorted(List)[1]
	if P == num:
		ans += 1
print(ans)


(n, *p) = map(int, open(0).read().split())
cnt = 0
for i in range(1, n - 1):
	if sorted([p[i - 1], p[i], p[i + 1]])[1] == p[i]:
		cnt += 1
print(cnt)


(n, *p) = map(int, open(0).read().split())
c = 0
for i in range(n - 2):
	if p[i] < p[i + 1] < p[i + 2] or p[i] > p[i + 1] > p[i + 2]:
		c += 1
print(c)


n = int(input())
p = list(map(int, input().split()))
c = 0
for i in range(n - 2):
	x = p[i:i + 3]
	s = sorted(x)
	if x[1] == s[1]:
		c += 1
print(c)


n = int(input())
P = list(map(int, input().split()))
ans = 0
for i in range(1, n - 1):
	if sorted([P[i - 1], P[i], P[i + 1]]).index(P[i]) == 1:
		ans += 1
print(ans)


n = int(input())
x = list(map(int, input().split()))
k = 0
for i in range(2, n):
	if x[i - 2] < x[i - 1] < x[i]:
		k = k + 1
	elif x[i - 2] > x[i - 1] > x[i]:
		k = k + 1
print(k)


(n, *p) = map(int, open(0).read().split())
print(sum((sorted(p[i - 1:i + 2])[1] == p[i] for i in range(1, n - 1))))