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(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]) or (p[i-1]>p[i] and 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):
    if i+3 <= n:
        tmp = p[i:i+3]
        if tmp[1] == sorted(tmp)[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(n - 2):
    p1 = p[i]
    p2 = p[i+1]
    p3 = p[i+2]
    l = [p1, p2, p3]
    l.sort()
    if l[1] == p2:
        a += 1
print(a)

Submission status: Accepted

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

num = 0
for i in range(n):
  if i == 0 or i == n-1:
    pass
  else:
    if (P_n[i]-P_n[i-1])*(P_n[i]-P_n[i+1]) < 0:
      num += 1
    else:
      pass
print(num)

Submission status: Accepted

Solution code:
n = int(input())

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

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

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]!=p[i-1] and p[i]!=p[i+1] and p[i-1]!=p[i+1] and p[i]!= max(p[i-1],p[i], p[i+1]) and p[i]!= min(p[i-1],p[i], p[i+1]):
        count = count + 1
    if (p[i]==p[i-1] or p[i]==p[i+1] or p[i-1]==p[i+1])  and p[i] == max(p[i-1],p[i], p[i+1]):
        count = count + 1

print(count)

Submission status: Accepted

Solution code:
import numpy as np


def resolve():
    n = int(input())
    p = [int(i) for i in input().split()]
    count = 0
    for i in range(n - 2):
        idx = np.argsort(p[i:i + 3])
        if (idx[1] == 1):
            count += 1
    print(count)
resolve()

Submission status: Accepted

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

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

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# B問題
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
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 [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  l=LI()

  cnt=0
  for i in range(1,n-1):
    a,b,c=[l[i-1],l[i],l[i+1]]

    if b!=min(a,b,c) and b!=max(a,b,c):
      cnt+=1

  return cnt

print(main())

Submission status: Accepted

Solution code:
def check(l):
    a = l[1]
    l.sort()
    if a == l[1] and l.count(a) == 1:
        return True
    else:
        return  False

n = int(input())
p = list(map(lambda x:int(x),input().split()))
cnt = 0
for i in range(3,n+1):
    if check(p[i-3:i]):
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
p_lst = input().split()
# int化
for i in range(0, len(p_lst)):
    p_lst[i] = int(p_lst[i])

cnt = 0
for i in range(0, len(p_lst)-2):
    if (p_lst[i+1] > p_lst[i] and p_lst[i+1] < p_lst[i+2]) or (p_lst[i+1] < p_lst[i] and p_lst[i+1] > p_lst[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,len(p)-1):
    c = p[i]
    l = p[i-1]
    r = p[i+1]
    judge = (l-c)*(r-c)
    if judge < 0:
        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] < p[i + 2] or p[i + 2] < p[i + 1] < p[i]:
        count += 1

print(count)

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(len(p)-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 = ans + 1
    else:
        continue

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(2, N):
    tmp = [P[i-2], P[i-1], P[i]]
    tmp.remove(max(tmp))
    tmp.remove(min(tmp))
    if tmp[0] == P[i-1]:
        cnt += 1

print(cnt)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S=int(input())
L=list(map(int,input().split()))
ans=0
for l in range(len(L)-2):
  if L[l]<L[l+1] and L[l+1]<L[l+2]:
    ans=ans+1
  elif L[l]>L[l+1] and L[l+1]>L[l+2]:
    ans=ans+1
  else:
    continue
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 i <= n - 2:
        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 = list(map(int,input().split()))
cnt = 0
for i, p in enumerate(p_list):
    if i == 0:
        continue
    if i > N-2:
        continue
    if (p_list[i-1] < p  < p_list[i+1]) or (p_list[i-1] > p  > p_list[i+1]):
        #print(p,i)
        cnt +=1
print(cnt)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
  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
  print(count)

Submission status: Accepted

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

print(count)

Submission status: Accepted

Solution code:
N = input()
num = input().split(' ')
count = 0
for i in range(int(N) - 2):
  if (int(num[i]) < int(num[i+1]) and int(num[i+1]) < int(num[i+2])) or (int(num[i+2]) < int(num[i+1]) and int(num[i+1]) < int(num[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):
    tmp = list([l[i],l[i+1],l[i+2]])
    if max(tmp) != tmp[1] and min(tmp) != tmp[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, len(p)-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())
input_line = [input() for i in range(1)]
p = list(map(int,input_line[0].split()))

counter = 0
for i in range(0,N-2):
    b = p[i:i+3]
    b.sort()
    if b[1] == p[i:i+3][1]:
        counter += 1
print(counter)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

mycnt = 0
for i in range(n-2):
  j = int(p[i])
  k = int(p[i+1])
  l = int(p[i+2])
  if j < k < l or l < k < j:
    mycnt += 1

print(mycnt)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from collections.abc import Iterable
from math import *
from itertools import *
from collections import *
from functools import *
from operator import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(n: int, p: "List[int]"):
    ret = 0
    for p0, p1, p2 in zip(p, p[1:], p[2:]):
        s0, s1, s2 = sorted([p0, p1, p2])
        ret += int(s1 == p1)
    return ret


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]"
    result = solve(n, p)
    if isinstance(result, Iterable) and not isinstance(result, str):
        result = '\n'.join([str(v) for v in result])
    print(result)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
xlist = [int(iz) for iz in input().split()]

run = n-2
ans = 0

for i in range(run):
    a = xlist[i]
    b = xlist[i+1]
    c = xlist[i+2]

    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()))
ans = 0
before =0
for i in range(n-1):
    if p[i+1]-p[i]>0:
        up = 1
    else:
        up = -1
    if before*up>0:
        ans += 1
    else:
        pass
    before = up
print(ans)

Submission status: Accepted

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

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

print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
data_list = [int(x) for x in input().split()]
count = 0
test = []
for i in range(1,N-1):
    test = sorted(data_list[i-1:i+2])
    if data_list[i] == test[1]:
        count += 1
print(count)

Submission status: Accepted

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

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

print(c)

Submission status: Accepted

Solution code:

def main():

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

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




if __name__=='__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

# solve
ans = 0
for i in range(0, n - 2):
    l = list(p[i:i + 3])
    s = sorted(l)
    if l[1] == s[1]:
        ans += 1

# output
print(ans)

Submission status: Accepted

Solution code:
N = int(raw_input())
S = map(int, raw_input().split(' '))

count = 0
for i in range(N-2):
      b = S[i:i+3]
      c = b[:]
      c.sort()
      if b[1] == c[1]:
            count += 1
      else:
            pass
print(count)

Submission status: Accepted

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

for i in range(1,n - 1):
    num.append(p[i-1])
    num.append(p[i])
    num.append(p[i+1])
    num.sort()
    if(num[1] == p[i]):
        count += 1
    num = []
print(count)

Submission status: Accepted

Solution code:
n = int(input()) 
p = list(int(_) for _ in input().split()) 

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

Submission status: Accepted

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

count = 0

for i in range(1, num - 1):
  if (in_list[i-1] < in_list[i] and in_list[i] < in_list[i+1]) or (in_list[i-1] > in_list[i] and in_list[i] > in_list[i+1]):
    count += 1

print(count)

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
plen = len(p)
num = 0
for i in range(plen):

    if i == 0 or i == plen-1:
        continue
    ls = [p[i-1], p[i], p[i+1]]
    max_ = ls.index(max(ls))
    min_ = ls.index(min(ls))
    if max_ != 1 and min_ != 1:
        num += 1
print(num)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = map(int, input().split())
L = list(map(int,input().split()))

sum = 0
for m in range(len(L)):
  if m >= 2 and m <= len(L)-1:
    if (L[m-2] < L[m-1] < L[m]) or (L[m-2] > L[m-1] > L[m]):
      sum += 1
print(sum)

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:
# input process
N = int(input())
inputData = input().split()
P = [int(x) for x in inputData]
c = 0
for i in range(1, N-1):
    threenum = [P[i-1], P[i], P[i+1]]
    st_threenum = sorted(threenum)
    if threenum[1] == st_threenum[1]:
        c += 1
    if i == (N - 1):
        break
print(c)

Submission status: Accepted

Solution code:
N = int(input())
C = list(map(int,input().split()))
cnt = 0

for i in range(N-2):
    if (C[i] < C[i+1] < C[i+2]) or (C[i+2] < C[i+1] < C[i]):
        cnt += 1

print(cnt)

Submission status: Accepted

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

ans = 0
for i in range(1, len(P)-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:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
''' 
------------------------
author : iiou16
------------------------
'''

def main():
    n = int(input())
    P = list(map(int, input().split()))

    count = 0
    for i in range(len(P) - 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]:
            count += 1
    print(count)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

c = 0
for i in range(N-2):
  if P[i] < P[i+1] <= P[i+2]:
    c += 1
  elif P[i+2] < P[i+1] <= P[i]:
    c += 1
  
print(c)

Submission status: Accepted

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

from fractions import gcd
from math import ceil,floor
import collections

# n=int(input())
# A,B,C,D=map(int, input().split())
# string = input()
# list = list(map(int, input().split()))


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

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
L=list(map(int,input().split()))
C=0
#print(L)

for i in range(N-2):
  if (L[i]>L[i+1] and L[i+1]>L[i+2]) or (L[i]<L[i+1] and L[i+1]<L[i+2]):
    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):
    tmp = [p[i],p[i+1],p[i+2]]
    tmp_sorted = sorted(tmp)
    if(tmp_sorted[1]==p[i+1]):
        ans+=1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
pl = list(map(int,input().split(" ")))
ans = 0
for i in range(N-2):
    num_l = pl[i:i+3]
    if(num_l[1]==sorted(num_l)[1]):
        ans += 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


sc=0
for s in range(n-2):
    if (P[s]<P[s+1]) and (P[s+1]<P[s+2]):
        sc+=1
        #print(P[s],P[s+1],P[s+2])
    elif (P[s]>P[s+1]) and (P[s+1]>P[s+2]):
        sc+=1
        #print(P[s],P[s+1],P[s+2])

print(sc)

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:
def no2(tar,r,l):
  if tar > r and tar < l:
    return True
  elif tar > l and tar < r:
    return True
  else:
    return False

x = input()
x = input()
nums = x.split()
#print(nums)
n = 0

for i in range(1,len(nums)-1):
  #print(nums[i])
  if no2(int(nums[i]),int(nums[i-1]),int(nums[i+1])) == True:
    n += 1
    
print(n)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/python3

import sys

def min2nd(ls):
    return sorted(ls)[1]

def solve(input):
    count = 0
    for i in range(0, len(input) - 2):
        slice_ = input[i:i+3]
        if slice_[1] == min2nd(slice_):
            count += 1
    return count

def main():
    sys.stdin.readline()
    inputs = [int(s) for s in sys.stdin.readline().rstrip().split(" ")]
    print(solve(inputs))

main()

Submission status: Accepted

Solution code:
# coding:utf-8

n = int(input())

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

cnt = n - 2

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

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

Submission status: Accepted

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

count = 0
for i, pi in enumerate(p):
    if i == 0 or i == n-1:
        continue
    if p[i-1] < pi < p[i+1] or p[i-1] > pi > 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):
	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:

def solve():
    N = int(raw_input())
    p = [int(_) for _ in raw_input().split()]
    ret = 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]):
            ret += 1
    print ret

if __name__ == '__main__':
    solve()

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

Submission status: Accepted

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

cnt = 0
for i in range(n-2):
    list = [p[i], p[i+2]]
    mini = min(list)
    maxi = max(list)
    if mini < p[i+1] <= maxi:
        cnt += 1

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 29 20:55:46 2019

@author: YudNK
"""

n = int(input())
l = [int(i) for i in input().split()]
count = 0
for i in (range(len(l) - 2)):
    s = l[i:i + 3]
    t = sorted(s)
    if s[1] == t[1]:
        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:
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]) or (p[i-1] > p[i] and 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(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())
s= list(input().split())
for i in range(n):
    s[i] = int(s[i])

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

print(count)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    *P, = map(int, input().split())

    def is_second_biggest(p0, p1, p2):
        return p1 != min(p0, p1, p2) and p1 != max(p0, p1, p2)

    cnt = 0
    for i in range(n-2):
        if is_second_biggest(P[i], P[i+1], P[i+2]):
            cnt += 1
    
    print(cnt)


main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
p = [int(c) for c in input().split(' ')]
count = 0
for i in range(n - 2):
    l = p[i: i + 3]
    l.sort()
    if p[i + 1] == l[1]:
        count += 1
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

cnt=0

for i in range(1,n-1):
	j=i-1
	if (x[j]>x[j+1] and x[j+1]>x[j+2]) or (x[j]<x[j+1] and x[j+1]<x[j+2]):
		cnt+=1

print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
input()
a = list(map(int, input().split()))
print(sum(s[1] == sorted(s)[1] for s in zip(a, a[1:], a[2:])))

Submission status: Accepted

Solution code:
n = int(input())
p = [x for x in map(int, input().split())]
num = 0
for i in range(1, n-1):
  a = [p[i-1], p[i], p[i+1]]
  a.sort()
  if a[1] == p[i]:
    num+=1
print(num)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


def main():
    n = int(input())
    count = 0
    p_list = [int(i) for i in input().split()]
    for i in range(1, n - 1):
        hoge = p_list[i - 1 : i + 2]
        if min(hoge) == p_list[i]:
            continue
        if len(set(hoge)) == 3 and max(hoge) == p_list[i]:
            continue
        count += 1
    print(count)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(num)

Submission status: Accepted

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

count = 0
for i in range(1, n - 1):
    all = []
    all.append(pl[i - 1])
    all.append(pl[i])
    all.append(pl[i+1])
    all.sort()
    if all[1] == pl[i]:
        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,len(p)-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]):
        num+=1
print(num)

Submission status: Accepted

Solution code:
from sys import stdin

n = int(stdin.readline().rstrip())
an = list(map(int, input().split()))


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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
arr = [int(i) for i in input().split()]

cnt = 0
for i in range(N-2):
  sort_arr = sorted([arr[i],arr[i+1],arr[i+2]])
  if sort_arr[1] == arr[i+1]:
    cnt += 1
    
print(cnt)

Submission status: Accepted

Solution code:
def check(i):
    if(p_list[i-1]< p_list[i] and p_list[i] < p_list[i+1]):
        return 1
    if(p_list[i-1] > p_list[i] and p_list[i] > p_list[i+1]):
        return 1
    return 0

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

ans = 0
for i in range(1, n - 1):
    ans += check(i)
print(ans)

Submission status: Accepted

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

for i in range(n):
    if n < i+3:
        break
    a, b, c = p[i:i+3]
    if a < b < c or a > b > c:
        count += 1

print(count)

Submission status: Accepted

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

ans = 0
for i in range(1,n-1):
  if l[i] > l[i-1] and l[i+1] > l[i]:
    ans += 1
  elif l[i] < l[i-1] and l[i+1] < l[i]:
    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-1] < li[i] < li[i+1] or li[i-1] > li[i] > li[i+1]:
        count += 1
print(count)

Submission status: Accepted

Solution code:
N = int(input())
p = [int(num) for num 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
p1 = P[0]
p2 = P[1]
for p3 in P[2:]:
  l = [p1, p2, p3]
  if sorted(l)[1] == p2:
    ans+=1
  p1 = p2
  p2 = p3
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

n=int(input())
s = list(map(int,input().split()))
cnt=0

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

print(cnt)