HDU 5806 NanoApe Loves Sequence Ⅱ

来源:互联网 发布:珍珠控制台模拟软件 编辑:程序博客网 时间:2024/06/06 06:36
Problem 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.

1T10, 2n200000, 1kn/2, 1m,Ai109
 

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

Sample Input
17 4 24 2 7 7 6 5 1
 

Sample Output

18

m是确定的,所有的数就变成了0和1,然后循环一遍就可以了,原来这种方法叫two-pointer,从来都不知道。。

#include<set>#include<map>#include<ctime>#include<cmath>#include<stack>#include<queue>#include<bitset>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>#include<functional>#define rep(i,j,k) for (int i = j; i <= k; i++)#define per(i,j,k) for (int i = j; i >= k; i--)using namespace std;typedef __int64 LL;const int low(int x) { return x&-x; }const double eps = 1e-8;const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int N = 2e5 + 10;int T, n, m, k;int a[N], cnt;int main(){    scanf("%d", &T);    while (T--)    {        scanf("%d%d%d", &n, &m, &k);        rep(i, 1, n) scanf("%d", &a[i]);        LL ans = cnt = 0;        for (int i = 1, j = 1; i + k - 1 <= n; cnt -= a[i++] >= m)        {            for (; j <= n && (i + k - 1 > j || cnt < k); j++) cnt += a[j] >= m;            if (cnt >= k) ans += n - j + 2;        }        printf("%I64d\n", ans);    }    return 0;}


0 0