8297: Above the Median

内存限制:128 MB 时间限制:1 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:9 通过:8

题目描述

农夫约翰将他的 N 头奶牛排成一排,以测量它们的身高。

奶牛 i 的高度为 Hi 纳米。

他想拍摄一张连续的奶牛序列的照片,以便参加县博览会上的一次牛摄影比赛。

博览会对所有提交的照片有一个非常奇怪的规则:

当照片中的这一组奶牛的中位数高度至少为某个阈值 X 时,该照片才能算作有效作品。

我们将数组 A[0…K] 的中位数定义为将 A 升序排序后的 A[ceiling(K/2)]

其中 ceiling(K/2) 表示 K/2 上取整。

例如,{7,3,2,6} 的中位数是 6,{5,4,8} 的中位数是 5

请帮助约翰计算,共有多少不同的连续奶牛序列的照片可以满足参赛要求。

输入格式

第一行包含两个整数 N 和 X

接下来 N 行,每行包含一个整数 Hi

输出格式

输出满足中位数至少为 X 的连续奶牛序列数量。

数据范围

1≤N≤105,
1≤Hi,X≤1e9

输入样例:

4 6
10
5
6
2

输出样例:

7

样例解释

满足参赛要求的连续奶牛序列为:

{10}, {6}, {10, 5}, {5, 6}, {6, 2}, {10, 5, 6}, {10, 5, 6, 2}

Problem 1: Above the Median [Brian Dean]


Farmer John has lined up his N (1 <= N <= 100,000) cows in a row to measure
their heights; cow i has height H_i (1 <= H_i <= 1,000,000,000)
nanometers--FJ believes in precise measurements! He wants to take a picture
of some contiguous subsequence of the cows to submit to a bovine
photography contest at the county fair.  


The fair has a very strange rule about all submitted photos: a photograph
is only valid to submit if it depicts a group of cows whose median height
is at least a certain threshold X (1 <= X <= 1,000,000,000).


For purposes of this problem, we define the median of an array A[0...K] to
be A[ceiling(K/2)] after A is sorted, where ceiling(K/2) gives K/2 rounded
up to the nearest integer (or K/2 itself, it K/2 is an integer to begin
with). For example the median of {7, 3, 2, 6} is 6, and the median of {5,
4, 8} is 5.


Please help FJ count the number of different contiguous subsequences of his
cows that he could potentially submit to the photography contest.


PROBLEM NAME: median


INPUT FORMAT:


* Line 1: Two space-separated integers: N and X.


* Lines 2..N+1: Line i+1 contains the single integer H_i.


SAMPLE INPUT (file median.in):


4 6
10
5
6
2


INPUT DETAILS:


FJ's four cows have heights 10, 5, 6, 2. We want to know how many
contiguous subsequences have median at least 6.


OUTPUT FORMAT:


* Line 1: The number of subsequences of FJ's cows that have median at
       least X. Note this may not fit into a 32-bit integer.


SAMPLE OUTPUT (file median.out):


7


OUTPUT DETAILS:


There are 10 possible contiguous subsequences to consider. Of these, only 7
have median at least 6. They are {10}, {6}, {10, 5}, {5, 6}, {6, 2}, {10,
5, 6}, {10, 5, 6, 2}.

输入格式

INPUT FORMAT:


* Line 1: Two space-separated integers: N and X.


* Lines 2..N+1: Line i+1 contains the single integer H_i.


输出格式

OUTPUT FORMAT:


* Line 1: The number of subsequences of FJ's cows that have median at
       least X. Note this may not fit into a 32-bit integer.

输入样例 复制

4 6
10
5
6
2

输出样例 复制

7