#P1627. Count Odd-Length Subarrays with Median b
Count Odd-Length Subarrays with Median b
Count Odd-Length Subarrays with Median b
You are given a permutation of \(1, 2, \ldots, n\). Your task is to count the number of contiguous subarrays with odd length whose median is exactly \(b\). The median of an odd-length array is defined as the element in the middle after the array is sorted in ascending order.
Note: A subarray is a contiguous subsequence of the original array. Since the given array is a permutation, every element is unique and thus the element \(b\) appears exactly once.
Hint: Transform the array by replacing each element \(a\) with \(1\) if \(a > b\) and \(-1\) if \(a < b\) (with \(0\) for the element equal to \(b\)). Then, using prefix sums and the position of \(b\), you can count the valid subarrays effectively.
inputFormat
The first line contains two integers \(n\) and \(b\) where \(n\) is the length of the permutation and \(b\) is the target median.
The second line contains \(n\) distinct integers \(a_1, a_2, \ldots, a_n\) representing a permutation of \(1, 2, \ldots, n\).
outputFormat
Output a single integer representing the number of contiguous subarrays with odd length whose median is \(b\>.
sample
1 1
1
1