HDU 5806 NanoApe Loves Sequence Ⅱ

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
 
   
1 7 4 2 4 2 7 7 6 5 1
 

Sample Output
 
   

18

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

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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;
}


你可能感兴趣的:(HDU)