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_ls = list(map(int, input().split(' ')))
cnt = 0
for i in range(1, len(p_ls) - 1):
    if (p_ls[i-1] < p_ls[i] < p_ls[i+1]) or (p_ls[i+1] < p_ls[i] < p_ls[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(n-2):
    if list(sorted(P[i:i+3]))[1] == 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(n-2):
    if (p[i] < p[i+1] < p[i+2])|(p[i+2] < p[i+1] < p[i]):
        count += 1
print(count)

Submission status: Accepted

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

from collections import deque
temp = deque()
for i in range(3):
    temp.append(p[i])
counter = 0
temp1 = sorted(temp)
if temp[1] == temp1[1]:
    counter += 1
for i in range(n-3):
    temp.popleft()
    temp.append(p[i+3])
    temp1 = sorted(temp)
    if temp[1] == temp1[1]:
        counter += 1
print(counter)

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()))
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(1, n):
    if i + 2 > n:
        break
    x = sorted(P[i - 1:i + 2])
    if x[1] == P[i]:
        ans += 1
print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
p = [int(n) for n 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()))
cnt = 0
for i in range(n - 2):
    l = p[i:i+3]
    L = sorted(l)
    if l[1] == L[1]:
        cnt += 1
        
print(cnt)

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+1] or P[n-1]>P[n]>P[n+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(n-2):
    if a[i] < a[i+1] and a[i+1] < a[i+2]:
        c+= 1
    elif a[i] > a[i+1] and a[i+1] > a[i+2]:
        c+= 1
print(c)

Submission status: Accepted

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

cnt = 0
for i in range(2, n):
  l = [p[i - 2], p[i - 1], p[i]]
  m = sorted(l)
  if l[1] == m[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(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())
p = list(map(int, input().split()))
num = 0
for i in range(0,n-2,1):
    i2 = i+1
    i3 = i+2
    if p[i]<p[i2]<p[i3] or p[i]>p[i2]>p[i3]:
        num += 1
print(num)

Submission status: Accepted

Solution code:
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]

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

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

Submission status: Accepted

Solution code:
N = int(input())
MM = input().split()
a = int(MM[0])
b = int(MM[1])
c = int(MM[2])
list1 = [a,b,c]
count = 0
for i in range(N-3):
  aaa =sorted(list1)
  if aaa[1] == b:
    count +=1
  
  a = b
  b = c
  c = int(MM[i+3])
  list1 =[a,b,c]
  
bbb = sorted(list1)
if bbb[1] == b:
  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:
import sys
from collections import defaultdict


def input():
    return 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 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)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
cnt = 0
for i in range(1, n-3+2):
    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())
A = list(map(int,input().split()))
ans = 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]:
        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(n - 2):
    minp = min(p[i], p[i + 1], p[i + 2])
    maxp = max(p[i], p[i + 1], p[i + 2])
    if p[i + 1] != minp and p[i + 1] != maxp:
        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 - 1] < p[i] < p[i + 1] or p[i + 1] < p[i] < p[i - 1]:
        counter += 1

print(counter)

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

Submission status: Accepted

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

def main():

    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+2] < p[i+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()))
count = 0

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

Submission status: Accepted

Solution code:
# abc130_b.py
# https://atcoder.jp/contests/abc132/tasks/abc132_b

# B - Ordinary Number /
# 実行時間制限: 2 sec / メモリ制限: 1024 MB
# 配点 : 200点

# 問題文
# {1, 2, ..., n} の順列 p = {p1, p2, ..., pn} があります。
# 以下の条件を満たすような pi(1<i<n) がいくつあるかを出力してください。
#     pi−1, pi, pi+1の 3 つの数の中で、pi が 2番目に小さい。

# 制約
#     入力は全て整数である。
#     3≤n≤20
#     pは {1, 2, ..., n} の順列である。

# 入力
# 入力は以下の形式で標準入力から与えられる。
# n
# p1 p2 ... pn

# 出力
# 条件を満たす piの個数を出力せよ。

# 入力例 1
# 5
# 1 3 5 4 2

# 出力例 1
# 2

# p1=1, p2=3, p3=5の中で、p2=3 は 2 番目に小さい数です。
# また、p3=5, p4=4, p5=2 の中で、p4=4 は 2 番目に小さい数です。条件を満たす要素はこの 2つです。

# 入力例 2
# 9
# 9 6 3 2 5 8 7 4 1

# 出力例 2
# 5


global FLAG_LOG
FLAG_LOG = False


def log(value):
    # FLAG_LOG = True
    # FLAG_LOG = False
    if FLAG_LOG:
        print(str(value))


def calculation(lines):
    # S = lines[0]
    N = int(lines[0])
    # X, A = list(map(int, lines[0].split()))
    values = list(map(int, lines[1].split()))
    # values = list(map(int, lines[2].split()))
    # values = list()
    # for i in range(6):
    #     values.append(int(lines[i]))
    # valueses = list()
    # for i in range(M):
    #     valueses.append(list(map(int, lines[i+1].split())))

    result = 0

    for i in range(N-2):
        tmp = values[i:i+3]
        tmp.sort()
        log(f'i=[{i}]')
        if tmp[1] == values[i+1]:
            result += 1

    return [result]


# 引数を取得
def get_input_lines(lines_count):
    lines = list()
    for _ in range(lines_count):
        lines.append(input())
    return lines


# テストデータ
def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['5', '1 3 5 4 2']
        lines_export = [2]
    if pattern == 2:
        lines_input = ['9', '9 6 3 2 5 8 7 4 1']
        lines_export = [5]
    return lines_input, lines_export


# 動作モード判別
def get_mode():
    import sys
    args = sys.argv
    global FLAG_LOG
    if len(args) == 1:
        mode = 0
        FLAG_LOG = False
    else:
        mode = int(args[1])
        FLAG_LOG = True
    return mode


# 主処理
def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines(2)
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    # if mode > 0:
    #     print(f'lines_input=[{lines_input}]')
    #     print(f'lines_export=[{lines_export}]')
    #     print(f'lines_result=[{lines_result}]')
    #     if lines_result == lines_export:
    #         print('OK')
    #     else:
    #         print('NG')
    # finished = time.time()
    # duration = finished - started
    # print(f'duration=[{duration}]')


# 起動処理
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):
    a = sorted(p[i-1:i+2])
    if a[1] == p[i]:
        ans += 1
print(ans)