Problem:


Score : 200 points

Problem Statement

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 1

5
1 3 5 4 2

Sample Output 1

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.

Sample Input 2

9
9 6 3 2 5 8 7 4 1

Sample Output 2

5



Sample input:
5
1 3 5 4 2


Sample output:
2

Submission status: Accepted

Solution code:
#gets;a=0
#gets.split.map(&:to_i).each_cons(3){|_|a+=_.sort[1]==_[1]?1:0}
#p a

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

Submission status: Accepted

Solution code:
def solve():
    n, *p_l = map(int, open(0).read().split())
    ans = 0
    
    for i in range(1, n-1):
        if p_l[i-1] < p_l[i] < p_l[i+1] or p_l[i+1] < p_l[i] < p_l[i-1]:
            ans += 1
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

count = 0
for i in range(1,n-1):
    if pn[i-1] < pn[i] < pn[i+1] or pn[i-1] > pn[i] > pn[i+1]:
        count += 1
    
print(count)

Submission status: Accepted

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

cnt = 0

for i in range(1, n-1):
    if (ls_p[i] != min(ls_p[i-1], ls_p[i], ls_p[i+1])) and (ls_p[i] != max(ls_p[i-1], ls_p[i], ls_p[i+1])):
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int,(input().split())))
l= [[0]*3 for i in range(n-2)]
count = 0
j = 0

for i in range(n-2):
    l[i] = sorted([p[i],p[i+1],p[i+2]])

for i in range(1,n-1):
    if p[i]==l[j][1]:
        count += 1
    j += 1
print(count)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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]:
        cnt+=1
    elif p[i+1] < p[i] < p[i-1]:
        cnt+=1

print(cnt)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
P = np.array([int(x) for x in input().split()])
count = 0
for n in range(N-2):
    if P[n] < P[n+1] and P[n+1] < P[n+2]:
        count += 1
    if P[n+2] < P[n+1] and P[n+1] < P[n]:
        count += 1
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0

for i in range(n-2):
  if sorted([p[i], p[i+1], p[i+2]])[1] == p[i+1]:
    ans += 1

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    A=list(map(int,input().split()))
    ans=0
    for i in range(1,n-1):
        ans+=(A[i-1]<A[i]<A[i+1]) or (A[i-1]>A[i]>A[i+1])
    print(ans)
resolve()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

n = int(input())

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

ans = 0
for i in range(1,n-1):
    if pl[i-1] < pl[i] < pl[i+1]:
        ans += 1
    if pl[i+1] < pl[i] < pl[i-1]:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
import unittest


class TestB(unittest.TestCase):
    def test_1(self):
        self.assertEqual(think([1, 3, 5, 4, 2]), 2)

    def test_2(self):
        self.assertEqual(think([9, 6, 3, 2, 5, 8, 7, 4, 1]), 5)


def solve():
    p = read()
    result = think(p)
    write(result)


def read():
    n = read_int(1)[0]
    return read_int(n)


def read_int(n):
    return list(map(lambda x: int(x), read_line().split(' ')))[:n]


def read_float(n):
    return list(map(lambda x: float(x), read_line().split(' ')))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(data):
    n = len(data)
    count = 0
    for i in range(n):
        if i + 2 >= n:
            break
        if data[i] < data[i + 1] < data[i + 2] or data[i] > data[i + 1] > data[i + 2]:
            count += 1
    return count


def write(result):
    print(result)


if __name__ == '__main__':
    # unittest.main()
    solve()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
n = int( input() )
p_list = list( map( int, input().split() ) )
c = 0
for i in range(0,n - 2):
    b = p_list[i+1]
    c_list = [ p_list[i],p_list[i+1],p_list[i+2] ]
    c_list.sort()
    if b == c_list[1]:
        c += 1
print(c)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
c = 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]:
    c += 1
print(c)

Submission status: Accepted

Solution code:
n = int(input())
p = input().split()
p = [int(p_) for p_ in p]

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

Submission status: Accepted

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

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

Submission status: Accepted

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

ans = 0
for i in range(1, n-1):
  if li[i-1] > li[i] and li[i+1] < li[i]:
    ans += 1
  if li[i-1] < li[i] and li[i+1] > li[i]:
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

ans = 0

for i in range(n-2):
    if (P[i] < P[i+1] < P[i+2]) or (P[i+2] < P[i+1] < P[i]):
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
count = 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]:
        count += 1
    elif p[i] > p[i+1] and p[i+1] == p[i+2]:
        count += 1
print(count)

Submission status: Accepted

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

for i in range(n-2):
    l = []
    l.append(ls[i])
    l.append(ls[i+1])
    l.append(ls[i+2])
    l.sort()
    if l[1] == ls[i+1]:
        cnt += 1

print(cnt)

Submission status: Accepted

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

for i in range(n-2):
    a, b, c = ls[i], ls[i+1], ls[i+2]
    if c > b > a or a > b > c:
        cnt += 1

print(cnt)

Submission status: Accepted

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

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)

Submission status: Accepted

Solution code:
n=input()
l=list(map(int,input().strip().split()))
ans=0
for i in range(1,len(l)-1):
  gc=0;
  lc=0;
  if(l[i] >l[i-1] or l[i]>l[i+1]):
    gc=1
  if(l[i] <l[i-1] or l[i]<l[i+1]):
    lc=1
  ans+=(gc & lc)
  #print(gc,lc,l[i])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main(n, p):
    res = 0

    for i in range(1, n - 1):
        if p[i - 1] < p[i] < p[i + 1]:
            res += 1
        elif p[i - 1] > p[i] > p[i + 1]:
            res += 1

    print(res)


if __name__ == '__main__':
    n = int(input())
    p = list(map(int, input().split(' ')))

    main(n, p)

Submission status: Accepted

Solution code:
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]-p[i])<0:
        cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
N=int(input())
s=list(map(int,input().split(" ")))
co=0
i=0
while i<=N-3:
    if s[i]<s[i+1]<s[i+2] or s[i]>s[i+1]>s[i+2]:
        co+=1
    i+=1
print(co)

Submission status: Accepted

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

count = 0

for i in range(0, n-2):
    if p[i] < p[i+1] and p[i+1] < p[i+2]:
        count += 1
    elif p[i] > p[i+1] and p[i+1] > p[i+2]:
        count += 1

print(count)

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
ans=0
for i in range(1,len(p)-1):
  if (p[i]<p[i-1] and p[i]>p[i+1]) or (p[i]>p[i-1] and p[i]<p[i+1]):
    ans+=1
  else:
    ans+=0
    

print(ans)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
n = int(input())
Ps = list(map(int, input().split()))
ans = 0

for i in range(1, n-1):
    As = [Ps[i-1], Ps[i], Ps[i+1]]    
    if Ps[i] == sum(As) - max(As) - min(As):
        ans += 1


print(ans)

Submission status: Accepted

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

ans = 0
for i in range(1, N - 1):
	cnt = 0
	if P[i - 1] < P[i]: cnt += 1
	if P[i] > P[i + 1]: cnt += 1
	if cnt == 1: ans += 1

print(ans)

Submission status: Accepted

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

def main():
    N =int(input())
    ps = [int(s) for s in input().split()]
    result = 0
    for i, p in enumerate(ps):
        if i==0 or i==N-1:
            continue
        if (ps[i-1] > p and p > ps[i+1]) or (ps[i-1] < p and p < ps[i+1]):
            result += 1
    print(result)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
ans = 0
def is_order(a, b, c):
  if (a > b and b > c) or (a < b and b < c):
    return True
  else:
    return False

for i in range(n-2):
  if is_order(p[i], p[i+1], p[i+2]):
    ans += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for i in range(len(P) - 2):
    if (P[i] < P[i+1] < P[i+2]) or (P[i+2] < P[i+1] < P[i]):
        ans += 1
    else:
        ans = ans

print(ans)

Submission status: Accepted

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

ans = 0
for i in range(1, n - 1):
    if (a[i] - a[i - 1]) * (a[i] - a[i + 1]) < 0:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
suu = 0
for i in range(n - 2):
  han = []
  han.append(p[i])
  han.append(p[i+1])
  han.append(p[i+2])
  sor = sorted(han)
  if sor[1] == p[i+1]:
    suu += 1
print(suu)

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
ans = 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]):
    ans += 1
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
B - Ordinary Number
https://atcoder.jp/contests/abc132/tasks/abc132_b

"""
import sys


def solve(numbers):
    ans = 0
    for a, b, c in zip(numbers, numbers[1:], numbers[2:]):
        t = sorted([a, b, c])
        ans += t[1] == b
    return ans


def main(args):
    _ = input()
    numbers = [int(x) for x in input().split()]
    ans = solve(numbers)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
n=int(input())
d=list(map(int,input().split()))
s=[]
count=0
for i in range(1,n-1):
  s.append(d[i])
  s.append(d[i-1])
  s.append(d[i+1])
  s.sort()
  if s[1]==d[i]:
    count+=1
  s=[]
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
count = 0

for i in range(1,n-1):
  if p[i] > min(p[i-1],p[i+1]) and p[i] < max(p[i-1],p[i+1]):
    count += 1
    
print(count)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    array = [int(x) for x in input().split()]

    count = 0
    for i in range(1,n-1):
        if (array[i-1]<array[i] and array[i]<array[i+1]) or (array[i-1]>array[i] and array[i]>array[i+1]):
            count += 1
    print(count)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(1, n - 1):
    if p[i-1] < p[i] and p[i] < p[i + 1] or p[i] < p[i -1] and p[i + 1] < p[i]:
        ans += 1
print(ans)

Submission status: Accepted

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

count = 0
diff = p_list[1] - p_list[0]
for idx in range(2, n):
    if (0 < diff):
        if (p_list[idx-1] < p_list[idx]):
            count += 1
    elif (diff == 0):
        if (p_list[idx] != p_list[idx-1]):
            count += 1
    else:
        if (p_list[idx] < p_list[idx-1]):
            count += 1
    
    diff = p_list[idx] - p_list[idx-1]

print(count)

Submission status: Accepted

Solution code:
N= int(input())

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

Submission status: Accepted

Solution code:
S = int(input())
A = list(map(int, input().split()))  # 入力のときに N を使う必要はありません

preAns = A[0] < A[1]
count = 0
for a in zip(A[1:], A[2:]):
  temp = a[0] < a[1]
  if (preAns and temp) or (not preAns and not temp):
    count += 1
  preAns = temp
print(count)

Submission status: Accepted

Solution code:
n = int(input())

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

cnt = 0
i = 1
for _ in range(n):
    if n-2 < i:
        break
    cond = 0
    if P[i] < P[i-1]:
        cond += 1
    if P[i] < P[i+1]:
        cond += 1
    if cond == 1:
        cnt += 1
    i += 1

print(cnt)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
a = [input().split() for l in range(2)]
n = int(a[0][0])
count=0
for i in range(n-2):
    p1=int(a[1][i])
    p2=int(a[1][i+1])
    p3=int(a[1][i+2])
    p=[p1,p2,p3]
    p.sort()
    if p.index(p2)==1:
        count=count+1

print(count)

Submission status: Accepted

Solution code:
n=int(input())
P=[int(x) for x in input().split()]
ans=0

for i in range(0,n-2):
    X=[P[i],P[i+1],P[i+2]]
    if min(X)!=P[i+1] and max(X) !=P[i+1]:
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
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+1]<p[i] and p[i+2]<p[i+1]):
        c+=1
print(c)

Submission status: Accepted

Solution code:
s = int(input())
arr = list(map(int,input().split()))
count = 0
for i in range(s-2):
    if arr[i] < arr[i+1] < arr[i+2] or arr[i] > arr[i+1] > arr[i+2]:
        count += 1
    else:
        continue
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
p = tuple(map(int, input().split()))

print(len(tuple(
    filter(lambda x: (x[0] < x[1] < x[2]) or (x[0] > x[1] > x[2])
           , zip(p, p[1:], p[2:])
           )
)))

Submission status: Accepted

Solution code:
def solve():
    n = int(input())
    l = [int(i) for i in input().split()]
    count = 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]:
            count += 1
    return count
    
if __name__ == '__main__':
    print(solve())

Submission status: Accepted

Solution code:
n = int(input())
p = str(input()).split(' ')
p = [int(i) for i in p]
counter = 0

for index in range(2, len(p)):
    if p[index - 2] < p[index - 1] and p[index - 1] < p[index]:
        counter = counter + 1
    elif p[index - 2] > p[index - 1] and p[index - 1] > p[index]:
        counter = counter + 1

print(counter)

Submission status: Accepted

Solution code:
n = int(input())
p = [int(x) for x in input().split()]

count = 0
for i in range(len(p) - 2):
    x = p[i:i+3]
    if x[1] == sorted(x)[1]:
        count += 1
print(count)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
N = int(input())
S = input().split()
series = []
left = 0
mid = 0
right = 0
counter = 0
for i in range(N):
    series.append(int(S[i]))
for i in range(N-1):
    if i == 0:
        left = series[i]
        continue
    left = series[i-1]
    mid = series[i]
    right = series[i+1]
    if (left > mid and right < mid) or (left < mid and right > mid):
        counter += 1
print(counter)

Submission status: Accepted

Solution code:
n = int(input())
s = input().split()
ans = 0

for i in range(n - 2):
    if int(s[i]) < int(s[i + 1]) and int(s[i + 1]) < int(s[i + 2]):
        ans += 1
    elif int(s[i]) > int(s[i + 1]) and int(s[i + 1]) > int(s[i + 2]):
        ans += 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int,input().split()))
count = 0

for i in range(n):
  if i != 0 and i != n-1:
    if p[i] > p[i-1]:
      if p[i] < p[i+1]:
        count += 1
    elif p[i] < p[i-1]:
      if p[i] > p[i+1]:
        count += 1
        
print(count)

Submission status: Accepted

Solution code:
N=int(input())
p=[i for i in map(int,input().split())]
ans=0
for i in range(N-2):
    if min(p[i:i+3])<p[i:i+3][1]<max(p[i:i+3]):
        ans+=1
        
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
P = list(map(int,input().split()))
p = [0]*3
cnt = 0
for i in range(1,n-1):
    for j in range(0,len(p)):
        p[j] = P[i+(j-1)] - P[i]
    p.sort()
    if p.index(0) == 1:
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
total = 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]:
    total += 1
print(total)

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int,input().split()))
total = 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]:
        total += 1
print(total)

Submission status: Accepted

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

ans = 0

for n in range(1, N-1):
    if P[n-1] < P[n] & P[n] < P[n+1]:
        ans += 1
    elif P[n-1] > P[n] & P[n] > P[n+1]:
        ans +=1

print(ans)

Submission status: Accepted

Solution code:
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]]
    if p[i+1]!=max(L) and p[i+1]!=min(L):
        cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
p = [int(x) for x in input().split()]

cnt = 0
for i in range(1,n-1):
  if (max(p[i-1],p[i],p[i+1]) != p[i])and(min(p[i-1],p[i],p[i+1]) != p[i]):
    cnt += 1
print(cnt)

Submission status: Accepted

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

ans = 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]):
    ans += 1
    
print(ans)

Submission status: Accepted

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

ans = 0
for i in range(1, n-1):
    if p[i] < max(p[i-1], p[i+1]) and p[i] > min(p[i-1], p[i+1]):
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N,*P = map(int,read().split())

answer = sum(x<y<z or x>y>z for x,y,z in zip(P,P[1:],P[2:]))
print(answer)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

ans=0
for i in range(n-2):
    if p[i+1]>min(p[i],p[i+2]) and p[i+1]<max(p[i],p[i+2]):
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int, input().split()))
counter = 0
for i in range(1, len(arr)-1):
    if (arr[i] > arr[i-1] and arr[i] < arr[i+1]) or (arr[i] < arr[i-1] and arr[i] > arr[i+1]):
        counter += 1
print(counter)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#ABC132B - Ordinary Number
n = int(input())
p = list(map(int,input().split()))
count = 0
for i in range(1,n-1,1):
    if min(p[i-1],p[i],p[i+1]) < p[i] < max(p[i-1],p[i],p[i+1]):
        count += 1
print(count)

Submission status: Accepted

Solution code:
n = int(input())
P = [int(p) for p in input().split()]

ans = 0
for i in range(1, n-1):
    if max(P[i-1], P[i], P[i+1]) != P[i] and min(P[i-1], P[i], P[i+1]) != P[i]:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
print(len([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]]))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

count = 0
for i in range(1, n-1):
    if (p_list[i-1] < p_list[i] and p_list[i] < p_list[i+1]) or (p_list[i+1] < p_list[i] and p_list[i] < p_list[i-1]):
        count += 1

print(count)

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int,input().split()))
count = 0
for i in range(1,n-1):
  q = p[i-1:i+2]
  asc = sorted(q)
  des = sorted(q,reverse=True)
  if q==asc or q==des:
    count+=1
print(count)

Submission status: Accepted

Solution code:
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] or L[i-1]>L[i]>L[i+1]:
    cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
p=input()
P=p.split()
x=[]
ans=0
for j in range(n):
    P[j]=int(P[j])
for i in range(n-2):
    x.append([])
    x[i].append(P[i])
    x[i].append(P[i+1])
    x[i].append(P[i+2])
for k in range(len(x)):
    if (x[k][0]<x[k][1] and x[k][1]<x[k][2]) or (x[k][1]>x[k][2] and x[k][1]<x[k][0]):
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
cnt=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]:
        cnt+=1
print(cnt)

Submission status: Accepted

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

print(a)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int,input().split()))
count = 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]:
        count += 1
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for idx in range(1,n-1):
    if p[idx-1]<p[idx]<p[idx+1] or p[idx-1]>p[idx]>p[idx+1]:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
ans=0
for i in range(n-2):
    if p[i+1] < max(p[i],p[i+1],p[i+2]):
        if p[i+1] > min(p[i],p[i+1],p[i+2]):
            ans+=1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int,input().split()))
m=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]):
    m +=1
  else:
    m +=0

    
print(m)

Submission status: Accepted

Solution code:
n = input()
src = list(map(int, input().split()))
count = 0
for i in range(len(src)-2):
    if src[i]<src[i+1]<src[i+2]:
        count += 1
new_li = list(reversed(src))
for n in range(len(new_li)-2):
    if new_li[n]<new_li[n+1]<new_li[n+2]:
        count += 1
print(count)

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
cnt=0
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]:
    cnt+=1
print(cnt)

Submission status: Accepted

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

ans = 0
for i, j, k in zip(p[:-2], p[1:-1], p[2:]):
    if i < j < k or i > j > k:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
cnt = 0
for p1, p2, p3 in zip(p, p[1:], p[2:]):
    if p2 != max(p1, p2, p3) and p2 != min(p1, p2, p3):
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int, input().split()))
result = 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])):
    result += 1

print(result)

Submission status: Accepted

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

d = 0
for i in range(1,n-1):
  a, b, c = p[i-1:i+2]
  #print("{} {} {}".format(a,b,c))
  if (a < b and b < c) or (a > b and b > c):
    d += 1

print(d)

Submission status: Accepted

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

n = int(input().split()[0])
p_list = list(map(int, input().split()))
count = 0

for i in range(1, n - 1):
    a, b, c = p_list[i - 1], p_list[i], p_list[i + 1]
    if a <= b <= c or c <= b <= a:
        count += 1

ans = count
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
count = 0

for i in range(1, n-1):
    l = [p[i-1], p[i], p[i+1]]
    if p[i] < max(l) and p[i] > min(l):
        count += 1
    else:
        continue

print(count)

Submission status: Accepted

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

a = 0

for i in range(n - 2):
  p_l = [p[i], p[i + 1], p[i + 2]]
  p_l2 = sorted(p_l)
  if p_l2[1] == p_l[1]:
    a += 1
    
print(int(a))

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    p = list(map(int, input().rstrip('\n').split()))
    cnt = 0
    for i in range(1, n-1):
        if p[i-1] < p[i] < p[i+1]:
            cnt += 1
        elif p[i-1] > p[i] > p[i+1]:
            cnt += 1
    print(cnt)


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:
n = int(input())
p = [int(_) for _ in input().split()]
cnt =0
for i in range(2,n):
  if p[i-2] < p[i-1] < p[i] or p[i]<p[i-1]<p[i-2]:
    cnt +=1
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int , input().split()))
cnt = 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]:
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
cnt = 0
for i in range(len(li)-2):
    a = li[i]
    b = li[i+1]
    c = li[i+2]
    if max(a,c)>b and b>min(a,c):
        cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
# AtCoder
N = int(input())
lst = [int(x) for x in input().split()]
ans = 0

for i in range(1, len(lst)-1):
    first = lst[i-1]
    second = lst[i]
    third = lst[i+1]

    if first < second < third or third < second < first:
        ans += 1

print(ans)