5806 NanoApe Loves Sequence Ⅱ(尺取法)

时间:2022-01-09 18:22:38

传送门

NanoApe Loves Sequence Ⅱ

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 1585    Accepted Submission(s): 688

Description

NanoApe, the Retired Dog, has returned back to prepare for for the National Higher Education Entrance Examination!

In math class, NanoApe picked up sequences once again. He wrote down a sequence with n numbers and a number m on the paper.

Now he wants to know the number of continous subsequences of the sequence in such a manner that the k-th largest number in the subsequence is no less than m.

Note : The length of the subsequence must be no less than k.

Input

The first line of the input contains an integer T, denoting the number of test cases.

In each test case, the first line of the input contains three integers n,m,k.

The second line of the input contains n integers A1,A2,...,An, denoting the elements of the sequence.

1≤T≤10, 2≤n≤200000, 1≤k≤n/2, 1≤m,Ai≤109

Output

For each test case, print a line with one integer, denoting the answer.

Sample Output

17 4 24 2 7 7 6 5 1

Sample Output

18

思路

题意:

退役狗 NanoApe 滚回去学文化课啦!
在数学课上,NanoApe 心痒痒又玩起了数列。他在纸上随便写了一个长度为 n 的数列,他又根据心情写下了一个数 m。
他想知道这个数列中有多少个区间里的第 k 大的数不小于 m,当然首先这个区间必须至少要有 k 个数啦。

 题解:求数列中多少个区间第k大不小于m,那么我们可以把数列中大于等于m的位置置1,其余置0,那么利用尺取法从头开始扫,找到每个位置开始和为k的区间的末位置,那么就知道总共有多少个区间了

 
#include<bits/stdc++.h>
using namespace std;
typedef __int64 LL;
const int maxn = 200005;
int num[maxn];

int main()
{
    //freopen("input.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while (T--)
    {
        int n,m,k,tmp;
        scanf("%d%d%d",&n,&m,&k);
        for (int i = 0;i < n;i++)
        {
            scanf("%d",&tmp);
            num[i] = tmp >= m?1:0;
        }
        int s = 0,t = 0;
        LL res = 0,sum = 0;
        for (;;)
        {
            while (t < n && sum < k)
            {
                sum += num[t++];
            }
            if (sum < k)    break;
            res += n - t + 1;
            sum -= num[s++];
        }
        printf("%I64d\n",res);
    }
    return 0;
}