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:
ma = lambda :map(int,input().split())
ni = lambda:int(input())
import collections
import math
import itertools
import heapq as hq
gcd = math.gcd
n = ni()
P = list(ma())
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 x in range(n-2):
  if (p[x]>=p[x+1] and p[x+1]>=p[x+2]) or (p[x]<=p[x+1] and p[x+1]<=p[x+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(N-2):
    ans += P[i] < P[i+1] < P[i+2]
    ans += P[i] > P[i+1] > P[i+2]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

cnt = 0

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

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())
a = list(map(int,input().split()))
count = 0
for i in range(1,n-1):
  if min(a[i-1],a[i+1]) < a[i] and max(a[i-1],a[i+1]) > a[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(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 = int(input())
    p = list(map(int, input().split()))
    loop = n - 3 + 1
    count = 0
    for i in range(loop):
        if p[i] < p[i + 1] < p[i + 2] or p[i + 2] < p[i + 1] < p[i]:
            count += 1
    print(count)
main()

Submission status: Accepted

Solution code:
n = int(input())
p = [int(i) for i in input().split()]
print(sum(sorted(p[i:i + 3])[1] == p[i + 1] for i in range(n - 2)))

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int,input().split()))
#print(p[:3])
p_cut =[]
cnt = 0

for i in range(n-2):
    p_cut = p[i:i+3]
    #print(p_cut)
    if p_cut[1] != max(p_cut)and p_cut[1] != min(p_cut):
        cnt += 1
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#create date: 2020-07-20 13:34

import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n = ni()
    p = na()
    ans = 0
    for i in range(1, n-1):
        a = p[i-1:i+2]
        if p[i] != max(a) and p[i] != min(a):
            ans += 1
    print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

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

def checkP(int1, int2, int3):
  l = [int1, int2, int3]
  l.sort()
  if l[1] == int2:
    return True
  else:
    return False

ans = 0

for i in range(1, n-1):
  if checkP(p[i-1], p[i], p[i+1]):
    ans +=1
  else:
    pass

print(ans)

Submission status: Accepted

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

for i in range(n-2):
    if l[i] < l[i+1] < l[i+2] or l[i+2] < l[i+1] < l[i]:
        count += 1

print(count)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
print(sum(p[i-2]<p[i-1]<=p[i] or p[i]<p[i-1]<=p[i-2] or \
            p[i]==p[i-2]<p[i-1] for i in range(2,n)))

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()))
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:
input()
*p,=map(int,input().split())
print(sum(p[i]<p[i+1]<p[i+2] or p[i]>p[i+1]>p[i+2]for i in range(len(p)-2)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
n = int(input())
array = list(map(int,input().split()))

if not ( 3 <= n <= 20 ): sys.exit()

count = 0
for I in range(n-2):
    if array[I] < array[I+1] < array[I+2]: count += 1
    if array[I] > array[I+1] > array[I+2]: count += 1
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int,input().split()))
cnt = 0
for i in range(1,n-1,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())
c = 0
l = list(map(int, input().split()))
for i in range(1,n-1,1):
    m = max(l[i-1],l[i+1],l[i]); n = min(l[i-1],l[i],l[i+1])
    if l[i]!= m and l[i]!= n:
        c += 1
print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations, product
from heapq import heappush, heappop
from functools import lru_cache
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 1000000007
sys.setrecursionlimit(1000000)

N = ri()
P = rl()

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] < 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(n):
  if n == i + 2:
    break
  else:
    
    p_com = p[i:i+3]
    sp_com= sorted(p_com)
    if sp_com[1] == p_com[1]:
      ans += 1
      
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    P = il()

    ret = 0
    for n in range(1, N-1):
        if P[n-1] < P[n] < P[n+1] or P[n-1] > P[n] > P[n+1]:
            ret += 1
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))

n=int(input())
p=input_array()

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
	else:
		continue
print(count)

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 = c+1
print(c)

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

Submission status: Accepted

Solution code:
n = int(input())
#h, w, k = list(map(int, input().split()))
# A = list(map(int, input().split()))
# a = list(map(int, input().split()))
# data = [list(map(int, input().split())) for i in range(n)]
# q = int(input())
p = list(map(int, input().split()))
# b = list(map(int, input().split()))
# ab_sorted = sorted(ab, key=lambda x: x[0])
# ab = [list(map(int, input().split())) for i in range(n)]
# print(min(15*n, 100*(n//10+1), 100*(n//10)+15*(n % 10)))
count = 0
for i in range(1, n-1):
    if p[i] > p[i-1] and p[i] < p[i+1]:
        count += 1
    elif p[i] < p[i-1] and p[i] > p[i+1]:
        count += 1

print(count)

Submission status: Accepted

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

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

print(count)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
import sys
def S(): return sys.stdin.readline().rstrip()
def M(): return map(int,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())
N = I()
pl = LI()
ans = 0
for i in range(2, N):
    p1 = pl[i-2]
    p2 = pl[i-1]
    p3 = pl[i]
    temp = list()
    temp.append(p1)
    temp.append(p2)
    temp.append(p3)
    temp.sort()
    if temp[1] == p2:
        ans += 1
print(ans)

Submission status: Accepted

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

for i in range(1, len(ps)-1):
    s1 = ps[i-1] < ps[i] and ps[i+1] > ps[i]
    s2 = ps[i-1] > ps[i] and ps[i+1] < ps[i]
    if s1 or s2:
        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]:
        cnt += 1

    elif p[i-1] < p[i] and p[i] < p[i+1]:
        cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
# Vicfred
# https://atcoder.jp/contests/abc132/tasks/abc132_b
# implementation
n = int(input())
p = list(map(int,input().split()))

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

print(answer)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
def gcd(*numbers): reduce(math.gcd, numbers)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
count = 0
ans = 0

n = k()
p = l()
for i in range(1,n-1):
    a = []
    a.append(p[i-1])
    a.append(p[i])
    a.append(p[i+1])
    a.sort()
    if a.index(p[i]) == 1:
        count += 1

print(count)

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n = ri()
li = rl()
cnt = 0
for i in range(1, n-1):
  if li[i-1] < li[i] < li[i+1] or li[i-1] > li[i] > li[i+1]:
    cnt += 1
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

n = int(input())
p = list(map(int,input().split()))
count = 0
for i in range(2,n):
    if max(p[i-2:i+1]) != p[i-1] and min(p[i-2:i+1]) != p[i-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] != min(p[i-1],p[i],p[i+1]) and p[i] != max(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] < 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(n-2):
    if (p[i+1]<p[i] and p[i+1]<p[i+2]) or (p[i+1]>p[i] and p[i+1]>p[i+2]):
        pass
    else:
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
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()
    cnt = 0
    for i in range(n-2):
        if p[i] < p[i+1] < p[i+2]:
            cnt += 1
        if p[i+2] < p[i+1] < p[i]:
            cnt += 1
    print(cnt)
main()

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

def main():
    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]])[1] == p[i]:
            ans += 1
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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
li = []

for i in range(1, n-1):
  mn = min(p[i-1], p[i], p[i+1])
  mx = max(p[i-1], p[i], p[i+1])
  if p[i]!=mn and p[i]!=mx:
    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:
import sys
sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right#2分探索
#bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下
from collections import deque
#deque(l), pop(), append(x), popleft(), appendleft(x)
#q.rotate(n)で → にn回ローテート
from collections import Counter#文字列を個数カウント辞書に、
#S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items()
from itertools import accumulate,combinations,permutations#累積和
#list(accumulate(l))
from heapq import heapify,heappop,heappush
#heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone
#import fractions#古いatcoderコンテストの場合GCDなどはここからimportする
from functools import lru_cache#pypyでもうごく
#@lru_cache(maxsize = None)#maxsizeは保存するデータ数の最大値、2**nが最も高効率
from decimal import Decimal

def input(): 
    x=sys.stdin.readline()
    return x[:-1] if x[-1]=="\n" else x
def printe(*x):print("## ",*x,file=sys.stderr)
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def main():
    mod = 1000000007
    #w.sort(key=itemgetter(1),reversed=True)  #二個目の要素で降順並び替え

    N = int(input())
    #N, K = map(int, input().split())
    A = tuple(map(int, input().split())) #1行ベクトル
    #L = tuple(int(input()) for i in range(N)) #改行ベクトル
    #S = tuple(tuple(map(int, input().split())) for i in range(N)) #改行行列
    count=0
    for i in range(N-2):
        a1,a2,a3=A[i],A[i+1],A[i+2]
        if a1<a2<a3 or a3<a2<a1:
            count+=1
    print(count)
 




if __name__ == "__main__":
    main()

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] > p[i-1] and p[i]<p[i+1]:
    a += 1
  elif p[i] < p[i-1] and p[i] > p[i+1]:
    a += 1
print(a)

Submission status: Accepted

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

Submission status: Accepted

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

Count = 0
for T in range(0,N-2):
    if P[T+1] == sorted(P[T:T+3])[1]:
        Count += 1
print(Count)

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    P = il()
    ret = 0
    for n in range(1, N - 1):
        if P[n - 1] < P[n] < P[n + 1] or P[n - 1] > P[n] > P[n + 1]:
            ret += 1
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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)))

Submission status: Accepted

Solution code:
N=int(input())
arr=list(map(int,input().split()))
count=0

for i in range(N-2):
    if arr[i+1]!=max(arr[i:i+3]) and arr[i+1]!=min(arr[i:i+3]):
        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-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):
    tmp_P = P[i-1:i+2]
    a = tmp_P[1]
    tmp_P.sort()
    if a == tmp_P[1]:
        ans += 1

print(ans)

Submission status: Accepted

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


count = 0
for i in range(1,n-1):
    if min(L[i-1],L[i+1]) < L[i] < max(L[i-1], L[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):
    S=p[i:i+3]
    if S[0]<S[1]<S[2] or S[2]<S[1]<S[0]:
        ans+=1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

def check_condition(l):
  if (l[0] < l[1] < l[2]) or  (l[0] > l[1] > l[2]):
    return 1
  else:
  	return 0

is_satisfied = [check_condition(p[i:i+3]) for i in range(n-2)]
  
print(np.sum(is_satisfied))

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int,input().split()))
count = 0
for i in range(1,n-1):
    test_list=sorted([p[i-1],p[i],p[i+1]])
    if(test_list[1]==p[i]):
        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]:
        count += 1
    elif p[i] < p[i - 1] == p[i + 1]:
        count += 1
    elif p[i - 1] > p[i] > p[i + 1]:
        count += 1
print(count)

Submission status: Accepted

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

    
if __name__ == "__main__":
    main()

Submission status: Accepted

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

count = 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]):
        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] < 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 = 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]) or (p[i+2] < p[i+1] 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):
  if p[i] <= p[i-1] and p[i] > p[i+1]:
    cnt += 1
  if p[i] <= p[i+1] and 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]<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):
    a, b, c = p[i-1:i+2]
    if min(a, b, c) < b < max(a, b, c):
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/ python3.8
n=int(input())
p=[int(x) for x in input().split()]
ans=0
for i in range(1,n-1):
    if max(p[i - 1], p[i + 1]) > p[i] > min(p[i - 1], p[i + 1]):
        ans += 1
print(ans)

Submission status: Accepted

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