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:
n = int(input())
p_ = list(input().split())

p = [int(p) for p in p_]

res = 0

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

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:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

readl= lambda: list(map(int, sys.stdin.readline().split()))
readt= lambda: tuple(map(int, sys.stdin.readline().split()))
read = lambda: sys.stdin.readline().rstrip()
readi = lambda: int(read())
readmi = lambda: map(int, sys.stdin.readline().split())
readms = lambda: map(str, sys.stdin.readline().split())

n = readi()
p = readl()
k = 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]):
        k += 1
print(k)

Submission status: Accepted

Solution code:
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

def s(generator, splitter, mapper):
    return [ mapper(s) for s in generator().split(splitter) ]

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

n = int(input())
p = get_nums_l()

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
p=[int(i) for i in 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+2]<p[i+1] and p[i+1]<p[i]:
        ans+=1

print(ans)

Submission status: Accepted

Solution code:
def main():
    _ = int(input())
    p = [int(s) for s in input().split()]
    print(solve(p))

def solve(p):
    cnt = 0
    for a, b, c in zip(p, p[1:], p[2:]):
        if (a < b < c) or (c < b < a):
            cnt += 1
    return cnt

main()

Submission status: Accepted

Solution code:
n = int(input())
ps = [int(i) for i in input().split()]
print(sum([1 for i in range(1, n-1) if not((ps[i-1] < ps[i] and ps[i+1] < ps[i]) or (ps[i-1] > ps[i] and ps[i+1] > ps[i]))]))

Submission status: Accepted

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

count = 0
for i in range(n-2):
    if p[i] < p[i+1] <= p[i+2]:
        count += 1
    elif 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()))

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

ans = 0

for i in range(n-2):
    p1 = p_list[i]
    p2 = p_list[i + 1]
    p3 = p_list[i + 2]
    if min(p1, p2, p3) != p2 and max(p1, p2, p3) != p2:
        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-1] < p[i] < p[i+1]:
        count += 1
    elif p[i-1] > p[i] > p[i+1]:
        count+= 1
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(1,n-1):
    l = [p[i-1],p[i],p[i+1]]
    l.sort()
    if l[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):
    if p[i] == sorted(p[i-1:i+2])[1]:
        cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
pList = list(map(int,input().split()))
c = 0
for i in range(1,n-1):
    tList = [pList[i-1],pList[i],pList[i+1]]
    tList.sort()
    if pList[i] == tList[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] > p[i + 1] and p[i] < p[i - 1] or p[i] < p[i + 1] and p[i] > p[i - 1]:
    ans += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

def LI(): return list(map(int, sys.stdin.readline().split()))

n = int(input())
p = LI()
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(1,n-1):
    if p[i-1] > p[i] and p[i] > p[i+1]:
        ans += 1
    if p[i-1] < p[i] and p[i] < p[i+1]:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
ps=list(map(lambda x: int(x),input().split()))
count=0
for i in range(N-2):
    if max(ps[i],ps[i+2],ps[i+1])!=ps[i+1] and min(ps[i],ps[i+1],ps[i+2])!=ps[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(n-2):
    tmp = P[i:i+3]
    tmps = sorted(tmp)
    if tmps[1] == tmp[1]:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
p = [int(x) for x in input().split()]
a = 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]:
        a += 1
        
print(a)

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

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
cnt = 0
for i in range(0, n-2):
    tmp = [p[j] for j in range(i, i+3)]
    x = tmp[1]
    tmp.sort()
    if tmp[1] == x:
        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] <= 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 min(p[i],p[i+2]) <= p[i+1] and max(p[i],p[i+2]) > p[i+1]:

        ans += 1

print (ans)

Submission status: Accepted

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

ans = 0

for i in range(1, len(perm) - 1):
    a = [perm[i - 1], perm[i], perm[i + 1]]
    a.sort()

    ans += perm[i] == a[1]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
p = [int(x) for x in input().split()]
ans = 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]):
        ans += 1
print(ans)

Submission status: Accepted

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

cnt = 0
for i in range(n-2):
    s1 = li[i]
    s2 = li[i+1]
    s3 = li[i+2]
    if s1 < s2 and s3 > s2:
        cnt += 1
    elif s1 > s2 and s3 < s2:
        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 = 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] <= P[i + 1] or P[i + 1] <= P[i] <= P[i - 1]:
        ans += 1

print(ans)

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 = list(map(int, input().split()))

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
Ps = [int(n) for n in input().split()]
score=0
for ind in range(len(Ps)-2):
    p1 = Ps[ind]
    p2 = Ps[ind+1]
    p3 = Ps[ind+2]
    l = [p1,p2,p3]
    if max(l)!=p2 and min(l)!=p2:
       score+=1
print(score)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
P = [int(i) for i in input().split()]
c = 0
for i in range(N-2):
  p1 = P[i]
  p2 = P[i+1]
  p3 = P[i+2]
  if p1 <= p2 <= p3 or p3 <= p2 <= p1:
    c += 1
print(c)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
p=input().rstrip().split(' ')
S=0;
for i in range(1,len(p)-1):
    a=int(p[i])
    b=int(p[i-1])
    c=int(p[i+1])
    T=min(a,b,c)
    if T==a:
        continue;
    elif T==b:
        B=min(a,c)
        if B==a:
            S+=1;
            continue;
    elif T==c:
        B=min(a,b)
        if B==a:
            S+=1;
            continue;
print(S)

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())
a = list(map(int,input().split()))
c = 0
for i in range(len(a)):
  if (i == 0 or i == len(a)-1):
    pass
  else:
    #print(i)
    if a[i-1]<a[i]<a[i+1] or a[i-1]>a[i]>a[i+1]:
      c+=1
print(c)

Submission status: Accepted

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

ans = 0
for a, b, c in zip(p, p[1:], p[2:]):
    if (b < a and b > c) or (b > a and b < c):
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
import sys

fin = sys.stdin.readline

n = int(fin())
p = list(map(int, fin().split()))

count = 0
for i in range(len(p)-2):
    if (p[i] - p[i + 1]) * (p[i+1]-p[i+2]) >0:
        count += 1

print(count)

Submission status: Accepted

Solution code:
#!venv/bin/python
from collections import defaultdict 

N = int(input())
P = [int(x) for x in input().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)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC132 B
import sys
sys.setrecursionlimit(1000000)
from collections import deque

N = int(input())
P = list(map(int,input().split()))
ans = 0
for i in range(1,N-1):
    if max(P[i-1],P[i],P[i+1]) == P[i]:
        continue
    elif min(P[i-1],P[i],P[i+1]) == P[i]:
        continue
    else:
        ans += 1
print(ans)

Submission status: Accepted

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

count = 0
for i in range(n-2):
    if max(P[i], P[i+2]) > P[i+1] and min(P[i], P[i+2]) < P[i+1]:
        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, n-1):
    if p[i] != max(p[i-1],p[i],p[i+1]) and p[i] != min(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()))
k = 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]:
    k += 1
print(k)

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:
# -*- coding: utf-8 -*-
n = int(input())
p = list(map(int,input().split()))
ret = 0
for i in range(1,len(p)-1):
    if p[i-1] < p[i] and p[i] <= p[i+1]:
        ret += 1
    if p[i+1] < p[i] and p[i] <= p[i-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(1, len(p)-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())
li = list(map(int,input().split()))

count = 0
for i in range(1,n-1):
    if li[i] < li[i+1] and li[i] >= li[i-1]:
        count += 1
    elif li[i] < li[i-1] and li[i] >= li[i+1]:
        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+1] not in [max(p[i:i+3]),min(p[i:i+3])]:
        ans+=1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
p = [int(i) for i in 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())
L=list(map(int,input().split()))
ans=0
for i in range(2,N):
    A=[L[i-2],L[i-1],L[i]]
    A.sort()
    if A[1]==L[i-1]:
        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):
  a = p[i-1:i+2]
  if a[0] < a[1] < a[2] or a[0] > a[1] > a[2]:
    count += 1
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
p = [int(_) for _ in input().split()]
cnt = 0
for i in range(1, N - 1):
    L = p[i - 1]
    C = p[i]
    R = p[i + 1]

    if L < C < R or R < C < L:
        cnt += 1

print(cnt)

Submission status: Accepted

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

counter = 0
for i in range(1, n-1):
    _p = [p[i-1], p[i], p[i+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]):
        counter += 1
print(counter)

Submission status: Accepted

Solution code:
def resolve():

    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)
    
resolve()

Submission status: Accepted

Solution code:
a = int(input())
list_p = list(map(int, input().split()))
count = 0
for i in range(a-2):
    if((list_p[i] < list_p[i+1] and list_p[i+1] < list_p[i+2]) or\
        (list_p[i] > list_p[i+1] and list_p[i+1] > list_p[i+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(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())
l = list(map(int,input().split()))
cnt = 0
for i in range(1,n-1):
    a,b,c = l[i-1],l[i],l[i+1]
    if (a<=b and b<=c) or (c<=b and b<=a):
        cnt += 1
print(cnt)

Submission status: Accepted

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

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

print(cnt)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    s = S()
    b = list(set(s))
    a = len(list(set(s)))
    if a == 2 and s.count(b[0]) == s.count(b[1]) == 2:
        print("Yes")
    else:
        print("No")
    return

#B
def B():
    n = II()
    p = LI()
    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)
    return

#C
def C():
    n = II()
    d = LI()
    d.sort()
    ans = 0
    for i in range(d[-1]):
        if n // 2 == bisect_left(d, i):
            ans += 1
    print(ans)
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    B()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(4100000)
n = int(input())
p = [int(i) for i in input().split()]
counter = 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]:
        counter += 1
print(counter)

Submission status: Accepted

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

ans=0
for i in range(n-2):
    l=[p[i],p[i+1],p[i+2]]
    l.sort()
    if p[i+1]==l[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):
    sort_p = sorted(P[i:i+3])
    if sort_p[1] == P[i+1]:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
p = [int(i) for i in input().split()]
count = 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]:
    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-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()))

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:
a=int(input())
b=list(map(int,input().split()))
ans=0
for i in range(1,a-1):
    if (b[i-1]<b[i] and b[i]<b[i+1]) or (b[i-1]>b[i] and b[i]>b[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(n-2):
    if (p[i]-p[i+1]) * (p[i+2]-p[i+1]) < 0:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:

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

ans = 0
for i in range(0,n-2):
    lis = p[i:i+3]
    a = p[i+1]
    lis.sort()
    if a == lis[1]:
        ans += 1

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
num_list = [ int(v) for v in input().split() ]

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

Submission status: Accepted

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

if __name__=="__main__": main()

Submission status: Accepted

Solution code:
n=int(input())
p=[int(x) for x in input().split()]
count=0
for i in range(1,n-1):
  a=p[i-1]
  b=p[i]
  c=p[i+1]
  l=[a,b,c]
  k=sorted(l)
  if(k[1]==l[1]):
    count+=1
print(count)

Submission status: Accepted

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

answer = 0
for i in range(1, N-1):
  if p[i] != max(p[i-1:i+2]) and p[i] != min(p[i-1:i+2]):
    answer +=1

print(answer)

Submission status: Accepted

Solution code:
N = int(input())
*p, = 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]:
        ans += 1
    elif p[i+1] >= p[i] and p[i] >= p[i-1]:
        ans += 1

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
AtCoder A
"""

import sys, math, random
import numpy as np

# N = int(input())
# A = list(map(int,input().split())) # N row 1 column
# A = [int(input()) for _ in range(N)] # 1 row N column
# S = str(input()) # str(input()) == input() -> 'abc'
# S = list(input()) # abc -> ['a','b','c']
# S.replace('ABC','X') # "testABCABC" -> "testXX"
# S=numpp.assaray(S)=="#"
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LLS(): return [list(sys.stdin.readline().rstrip()) for _ in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()



def main():
    N = I()
    P =LI()
    out=0
    for i in range(2,N):
        if (P[i-2]>P[i-1] and P[i-1]>P[i]) or(P[i-2]<P[i-1] and P[i-1]<P[i]):
            out+=1

    print(out)

    
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


n = ni()
a = list(li())
ans = 0

for i in range(n-2):
    p0, p1, p2 = a[i:i+3]
    st = sorted([p0, p1, p2])

    if p1 == st[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] and P[i]>P[i-1]: ans+=1
    elif 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()))
cnt = 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]):
        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:
# -*- coding: utf-8 -*-

def readInts():
  return [int(s) for s in input().split(" ")]

def main():
  n = readInts()[0]
  p = readInts()

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

if __name__ == "__main__":
  main()

Submission status: Accepted

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

import sys

n= int(sys.stdin.readline())
p= [ int(x) for x in sys.stdin.readline().split() ]
#
count = 0
for i in range(n-2):
    m= p[i+1]
    t= p[i:i+3]
    if m!=min(t) and m!=max(t): 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):
    if (p[i-1]<p[i]<p[i+1]) or (p[i+1]<p[i]<p[i-1]):
        count += 1

print(str(count))

Submission status: Accepted

Solution code:
import sys
from collections import deque  # 双方向キュー
from collections import defaultdict  # 初期化済み辞書
from heapq import heapify, heappush, heappop, heappushpop  # プライオリティキュー
from bisect import bisect_left, bisect_right  # 二分探索
#import numpy as np  # 1.8.2
#import scipy  # 0.13.3
#sys.setrecursionlimit(int(1e+6))
s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2sss = lambda n: [list(sys.stdin.readline().rstrip()) for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))
MOD = int(1e+9) + 7

def main():
    n = i2n()
    p = i2nn()
    ans = 0
    for i in range(1, n-1):
        x, y, z = p[i-1:i+2]
        if x < y and y < z:
            ans += 1
        elif x > y and y > z:
            ans += 1
    print(ans)

main()

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()))

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)

Submission status: Accepted

Solution code:
n = int(input())
p = [int(i) for i in 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 = [int(i) for i in input().split()]

res = 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]:
        res += 1

print(res)

Submission status: Accepted

Solution code:
N=int(input())
L=list(map(int,input().split()))
cnt=0
for i in range(N-2):
    M=L[i:i+3]
    if L[i+1]>min(M) and L[i+1]<max(M):
        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) :
    a = P[i]
    b = P[i+1]
    c = P[i+2]
    
    Z = sorted([a,b,c])
    
    if max(Z) != b and min(Z) != b :
        ans += 1
print(ans)