This page is still under construction.

Parts of this page are still being built. What you see may change.

Above the Median

Interview

Time limit1sMemory limit128 MB

Summary
Given N cow heights, count contiguous ranges whose defined median (the ceil(K/2)-th smallest) is at least a threshold X.
Level

Medium6 of 10

Topics
Prefix sum, Binary search, Sorting, Array
Solved
No attempts yet

Problem

Farmer John has lined up his NN (1≤N≤100,0001 \le N \le 100{,}000) cows in a row to measure their heights; cow ii has height HiH_i (1≤Hi≤1091 \le H_i \le 10^9) nanometers—FJ believes in precise measurements! He wants to take a picture of some contiguous range of the cows to submit to a bovine photography contest at the county fair.

The contest has an unusual rule: a photograph may be submitted only if the cows it shows have a median height of at least a threshold XX (1≤X≤1091 \le X \le 10^9).

For this problem, the median of an array A[0..K]A[0..K] (which has K+1K+1 elements) is defined as A[⌈K/2⌉]A[\lceil K/2 \rceil] after AA is sorted in non-decreasing order, where ⌈K/2⌉\lceil K/2 \rceil is K/2K/2 rounded up (or K/2K/2 itself when it is already an integer). For example, the median of {7,3,2,6}\{7, 3, 2, 6\} is 66, and the median of {5,4,8}\{5, 4, 8\} is 55.

Count the number of distinct contiguous ranges of cows that FJ could submit to the contest.

Input

  • Line 1: Two space-separated integers NN and XX.
  • Lines 2 to N+1N+1: Line i+1i+1 contains the single integer HiH_i.

Output

  • Line 1: The number of contiguous ranges whose median is at least XX. This value may not fit in a 32-bit integer.

Examples1

  1. Example 1

    Input
    4 6
    10
    5
    6
    2
    
    Expected output
    7