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

cnt = 0
for i in range(1, n - 2 + 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):
    window = (ps[i-1], ps[i], ps[i+1])
    if ps[i] != min(window) and ps[i] != max(window):
        ans += 1

print(ans)

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

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

Submission status: Accepted

Solution code:
from heapq import heappush, heappop
import re
from collections import deque
import sys
input = sys.stdin.readline


def int_raw():
    return int(input())


def ss_raw():
    return input().split()


def ints_raw():
    return tuple(map(int, ss_raw()))


DIV = 10**9+7


def mod_invs(n, mod=DIV):
    inv = [0]*(n+1)
    inv[0] = 1
    inv[1] = 1
    for i in range(2, n+1):
        inv[i] = (-(mod//i)*inv[mod % i]) % mod
    return inv


def main():
    N= int_raw()
    ps = ints_raw()
    ans =0
    for i in range(1,N-1):
        p = ps[i]
        p_sub = ps[i-1:i+2]
        if(min(p_sub)!=p and max(p_sub)!=p):
            ans+=1
    return ans

print(main())

Submission status: Accepted

Solution code:
n = int(input())
p_list = list(map(int, input().split()))
count = 0
for i in range(1, n - 1):
    p1 = p_list[i-1]
    p2 = p_list[i]
    p3 = p_list[i+1]
    if p1 < p2 < p3 or p1 > p2 > p3:
        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(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_list = [p[i-1], p[i], p[i+1]]
    tmp_list.sort()


    if tmp_list[1] == p[i]:
        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] == sorted(p[i-1:i+2])[1]:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
p = na()
a = 0

for i in range(1,n-1):
    mi = min(p[i-1],p[i],p[i+1])
    ma = max(p[i-1],p[i],p[i+1])
    if p[i] != mi and p[i] != ma:
        a+=1

print(a)

Submission status: Accepted

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

print(c)

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]>=p[i+1]>=p[i+2]:
        ans+=1

print(ans)

Submission status: Accepted

Solution code:
import bisect
from operator import itemgetter
import math
import copy
import collections
import functools
import itertools
import numpy as np
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
A = IL()

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

print(cnt)

Submission status: Accepted

Solution code:
#B
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())
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()))
b=0
for i in range(1,n-1):
    if a[i-1]<=a[i]<=a[i+1] or a[i-1]>=a[i]>=a[i+1]:
        b+=1
print(b)

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa == x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))

n = onem()
l = l()
co = 0
for i in range(1,n-1):
    a = l[i-1]
    b = l[i]
    c = l[i+1]
    k = [a,b,c]
    k.sort(reverse = True)
    if k[1] == b and k[0] != k[1]:
        co += 1
print(co)

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 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):
    count = 0
    if p[i] < p[i-1]:
        count += 1
    if p[i] < p[i+1]:
        count += 1
    if count == 1:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
from collections import defaultdict
from functools import reduce

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])) or ((p[i+1] > p[i]) 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()))
cnt = 0
for i in range(n-2):
    a,b,c=p[i],p[i+1],p[i+2]
    if sorted([a,b,c]).index(b) == 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]:
        ans+=1
    if 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(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 = input()
p = [int(x) for x in input().split()]

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

print(ans)

Submission status: Accepted

Solution code:
def find(P):
    count=0
    for i in range(1,len(P)-1):
        x,y,z=P[i-1],P[i],P[i+1]
        if P[i]==sorted([x,y,z])[1]:
            count+=1
    return count
input()
A=list(map(int,input().strip().split(' ')))
print(find(A))

Submission status: Accepted

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

################################################################################

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

res = 0
for i in range(1, n-1):
    a, b, c = p[i-1], p[i], p[i+1]
    t = [a, b, c]
    t.sort()
    if b == t[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]<P[i+1] or 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()))
print(sum([1 for i in range(n-2) if max(a[i],a[i+1],a[i+2]) != a[i+1] and min(a[i],a[i+1],a[i+2]) != a[i+1]]))

Submission status: Accepted

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

res = 0
for i in range(1, N - 1):
    M = max(P[i - 1], P[i], P[i + 1])
    m = min(P[i - 1], P[i], P[i + 1])

    if M != P[i] and m != P[i]:
        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())
p = list(map(int,input().split(' ')))

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

ans = 0
plist = list(map(int, input().split()))

for i in range(len(plist)-2):
    tmp = plist[i:i+3]
    tmp.sort()
    if tmp[1] == plist[i+1]:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:

n = int(input())

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

count = 0
for i in range(1, n - 1):
    if p_lst[i-1] < p_lst[i] < p_lst[i+1]:
        count += 1
    elif p_lst[i+1] < p_lst[i] < p_lst[i-1]:
        count += 1
    else:
        pass

print(count)

Submission status: Accepted

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

ans = 0

for i in range(n-2):
    p1 = p[i]
    p2 = p[i+1]
    p3 = p[i+2]
    
    if p2 > p1 and p2 < p3:
        ans += 1
    elif p2 > p3 and p2 < p1:
        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 = p[i-1:i+2]
    b = sorted(a)
    if a[1] == b[1]:
        ans += 1
print(ans)

Submission status: Accepted

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

cnt=0
for i in range(1,n-1):
  p0 = plist[i-1]
  p1 = plist[i]
  p2 = plist[i+1]
  
  if p0 < p1 and p1 < p2:
    cnt += 1
  elif p0 > p1 and p1 > p2:
    cnt += 1
    
print(cnt)

Submission status: Accepted

Solution code:
n = input()
l = map(int, raw_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()))
count = 0
for i in range(N-2):
    p1, p2, p3 = P[i:i+3]
    if (p2 >= p1 and p2 <= p3) or (p2 <= p1 and p2 >= p3):
        count += 1
print(count)

Submission status: Accepted

Solution code:
n=int(input())
plist = list(map(int, input().split()))
answer=0

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

print(answer)

Submission status: Accepted

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

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

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:
# -*- coding: utf-8 -*-
def inpl(): return list(map(int, input().split()))
from collections import Counter
N = int(input())
P = inpl()
ans = 0
for i in range(N-2):
    ans += (P[i] < P[i+1] < P[i+2]) | (P[i+2] < P[i+1] < P[i])
print(ans)

Submission status: Accepted

Solution code:
import math
import copy
from operator import mul
from functools import reduce
from collections import defaultdict
from collections import Counter
from collections import deque
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
# 一次元累積和
from itertools import accumulate
from bisect import bisect_left, bisect_right

# import numpy as np
# from scipy.special import perm
# from scipy.special import comb

def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W

# 四方向: 右, 下, 左, 上
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
 
def i_inpl(): return int(input())
def l_inpl(): return list(map(int, input().split()))
INF = float("inf")

########

# S = input()

# c = Counter(S)

# flag = True
# for i in c.values():
#     if i != 2:
#         flag = False

# if flag:
#     print("Yes")
# else:
#     print("No")

n = i_inpl()
p = l_inpl()

cnt = 0
for i in range(n-2):
    G = [p[i], p[i+1], p[i+2]]
    G.sort()
    if G[1] == p[i+1]:
        cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
#B問題
n = int(input())
p = list(map(int,input().split()))
count = 0
for i in range(n-2):
    p1 = p[i]
    p2 = p[i+1]
    p3 = p[i+2]
    if p2 != max(p1,p2,p3) and p2 != min(p1,p2,p3):
        count+=1
        
print(count)

Submission status: Accepted

Solution code:
def count(n, ps):
    res = 0
    for i in range(1, n - 1):
        qs = (ps[i - 1], ps[i], ps[i + 1])
        if ps[i] == sorted(qs)[1]:
            res += 1
    return res


def main():
    n = int(input())
    ps = [int(p) for p in input().split()]
    res = count(n, ps)
    print(res)


if __name__ == "__main__":
    main()

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
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007
dire4 = [(1,0), (0,1), (-1,0), (0,-1)]
dire8 = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]

n = int(input())
p = LI()

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

Submission status: Accepted

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

Submission status: Accepted

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


def solve(n: int, p: "List[int]"):
    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)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  # type: int
    p = [ int(next(tokens)) for _ in range(n) ]  # type: "List[int]"
    solve(n, p)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]

# 
# a, b
# s = input()

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

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:
# -*- coding: utf-8 -*-
import sys
from operator import itemgetter
# gcd
from fractions import gcd
# 切り上げ，切り捨て
from math import ceil, floor
# リストの真のコピー（変更が伝播しない）
from copy import deepcopy
# 累積和。list(accumulate(A))としてAの累積和
from itertools import accumulate
# l = ['a', 'b', 'b', 'c', 'b', 'a', 'c', 'c', 'b', 'c', 'b', 'a']
# S = Counter(l)  # カウンタークラスが作られる。S=Counter({'b': 5, 'c': 4, 'a': 3})
# print(S.most_common(2))  # [('b', 5), ('c', 4)]
# print(S.keys())  # dict_keys(['a', 'b', 'c'])
# print(S.values())  # dict_values([3, 5, 4])
# print(S.items())  # dict_items([('a', 3), ('b', 5), ('c', 4)])
from collections import Counter
import math
from functools import reduce
# input関係の定義
# fin = open('in_2.txt', 'r')
# sys.stdin = fin
#
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())
# template


def solve(n: int, p: "List[int]"):
    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)


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  # type: int
    p = [int(next(tokens)) for _ in range(n)]  # type: "List[int]"
    solve(n, p)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

count = 0
for i in range(n - 2):
	tmp = p_list[i:i + 3]
	if tmp[1] == sorted(tmp)[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):
    l=p[i-1:i+2]
    l.sort()
    if p[i]==l[1]:
        c+=1
print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env pypy
from __future__ import division, print_function

import os
import sys
from __builtin__ import xrange as range
from cStringIO import StringIO
from future_builtins import ascii, filter, hex, map, oct, zip
from io import IOBase

import __pypy__


def main():
    n = int(input())
    p = [int(pi) for pi 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)


# region fastio

BUFSIZE = 8192


class FastI(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = StringIO()
        self.newlines = 0

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count("\n") + (not b)
            ptr = self._buffer.tell()
            self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr)
        self.newlines -= 1
        return self._buffer.readline()


class FastO(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = __pypy__.builders.StringBuilder()
        self.write = lambda s: self._buffer.append(s)

    def flush(self):
        os.write(self._fd, self._buffer.build())
        self._buffer = __pypy__.builders.StringBuilder()


def print(*args, **kwargs):
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastI(sys.stdin), FastO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
import math


def main():
    num = int(input())
    data = list(map(int, input().split()))

    ans = 0
    for i in range(1, num - 1):
        aaa = data[i - 1:i + 2]

        if max(aaa) != aaa[1] and min(aaa) != aaa[1]:
            ans += 1

    print(ans)






if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
p = [int(s) for s 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:
from bisect import bisect_left, bisect_right; from collections import Counter, defaultdict, deque, OrderedDict; from copy import deepcopy; from fractions import gcd; from functools import lru_cache, reduce; from math import ceil, floor; from sys import stdin, setrecursionlimit
import heapq as hq; import itertools as its; import operator as op


# globals
inf = float('inf')
input = stdin.readline


def main():
    n = get_int()
    P = get_li()
    ans = 0
    for a, b, c in zip(P, P[1:], P[2:]):
        if a < b < c or a > b > c:
            ans += 1
    print(ans)
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def gets(n, types, sep=None):
    if len(types) == 1:
        return [types[0](input().strip()) for _ in range(n)]
    return list(zip(*(
        [t(x) for t, x in zip(types, input().strip().split(sep=sep))]
        for _ in range(n)
    )))


if __name__ == '__main__':
    setrecursionlimit(4100000)
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def cnt(a):
    k=a[1]
    a.sort()
    if k==a[1]:
        return 1
    return 0

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

ans=0
for i in range(n-2):
    ans+=cnt(p[i:i+3])
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], p[i], p[i + 1]
    if a < b < c or c < b < a:
        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] and p[i] < p[i+1]:
        count += 1
    elif p[i-1] > p[i] and p[i] > p[i+1]:
        count += 1
    else:
        pass
print (count)

Submission status: Accepted

Solution code:
n = int(input())
*p, = map(int, input().split())
ans = 0
for i in range(1, n-1):
    v = p[i-1:i+2]
    s = v[1]
    v.sort()
    if s == v[1]:
        ans += 1

print(ans)

Submission status: Accepted

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


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] > p[i+1] > p[i+2]:
        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):
	x = p[i:i+3]
	s = sorted(x)
	if(x[1]==s[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(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()]
ans = 0
for i in range(n-1):
    if min(p[i:i+3])!=p[i+1] and max(p[i:i+3])!=p[i+1]: ans+=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
P = list(map(int, input().split()))
num = 0
for i in range(1, N - 1):
    tmp = [P[i - 1], P[i], P[i + 1]]
    tmp = sorted(tmp)
    if tmp[1] == P[i]:
        num += 1

print(num)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
pre_pre = l[0]
pre = l[1]
count = 0
for i in range(2, n):
    cur = l[i]
    if pre_pre < pre < cur or cur < pre < pre_pre:
        count += 1
    pre_pre = pre
    pre = cur
print(count)

Submission status: Accepted

Solution code:
N = int(input())
p = [int(x) for x in input().split()]
cnt = 0
for ind,i in enumerate(p[1:-1]):
    if(i>min(p[ind],p[ind+2]) and i<max(p[ind],p[ind+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(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] > 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:
import sys
input = sys.stdin.readline

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 sorted([p[i], p[i+1], p[i+2]])[1] == p[i+1]:
        ans += 1

print(ans)

Submission status: Accepted

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

ans = sum(p[i] == (p[i + 1] + p[i] + p[i - 1] - max(p[i + 1], p[i], p[i - 1]) - min(p[i + 1], p[i], p[i - 1])) for i in range(1, n - 1))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
p = [int(i) for i in input().split()]
count = 0
for k in range(n-2):
    if (p[k+1] - p[k]) * (p[k+2] - p[k+1]) > 0:
        count += 1
print(count)

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int, input().split(' ')))
ans = 0
for i in range(1, len(arr) - 1):
    p2 = arr[i]
    p1 = arr[i-1]
    p3 = arr[i+1]

    x = [p1, p2, p3]
    x = sorted(x)
    if x[1] == p2:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    
    n = int(input())
    arr = list(map(int, input().split()))
    
    ans = 0
    for i in range(1, n - 1):
        b = sorted(arr[i - 1: i + 2])[1]
        if b == arr[i]:
            ans += 1
    
    print(ans)
    
    return 0

main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
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()))
ct=0
for i in range(1,n-1):
  l=[p[i-1],p[i],p[i+1]]
  l.sort()
  if l[1]==p[i]:
    ct+=1
print(ct)

Submission status: Accepted

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

Submission status: Accepted

Solution code:


def main():
    n = input()
    p = map(int, raw_input().split())
    count = 0
    for i in xrange(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


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):
    if P[i] != min(P[i-1:i+2]) and P[i] != max(P[i-1:i+2]):
        ans += 1
print(ans)