HDU5806 NanoApe Loves Sequence Ⅱ【尺取法+乘法原理】

NanoApe Loves Sequence Ⅱ

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

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.
 

Inputhttp://write.blog.csdn.net/postedit?ticket=ST-152705-jLaV4MBV1ZmLGwLvy7lI-passport.csdn.net
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

Source

问题链接HDU5806 NanoApe Loves Sequence Ⅱ

问题简述:在数学课上,NanoApe 心痒痒又玩起了数列。他在纸上随便写了一个长度为n的数列,他又根据心情写下了一个数m。他想知道这个数列中有多少个区间里的第k大的数不小于m,当然首先这个区间必须至少要有k个数。

问题分析(略)

程序说明:这里给出两种程序,一是尺取法的程序,二是乘法原理的程序。占个位置,以后说明。

题记:(略)


参考链接:(略)


AC的C++语言程序如下(尺取法):

/* HDU5806 NanoApe Loves Sequence Ⅱ(尺取法) */

#include <iostream>
#include <stdio.h>

using namespace std;

const int N = 200000;
int a[N];

int main()
{
    int t, n, m, k;
    scanf("%d", &t);
    while(t--) {
        scanf("%d%d%d", &n, &m, &k);

        for(int i=0; i<n; i++)
            scanf("%d", &a[i]);

        int j=0, num = 0;
        long long sum = 0;
        for(int i=0; i<n; i++) {
            while(j < n && num < k) {
                if(a[j] >= m)
                    num++;
                j++;
            }
            if(num >= k)
                sum += n - j + 1;
            if(a[i] >= m)
                num--;
        }

        printf("%lld
", sum);
    }
    return 0;
}

AC的C++语言程序如下(乘法原理):

/* HDU5806 NanoApe Loves Sequence Ⅱ(乘法原理) */

#include <iostream>
#include <stdio.h>

using namespace std;

const int N = 200000;
int a[N];
long long d[N];

int main()
{
    int t, n, m, k;
    scanf("%d", &t);
    while(t--) {
        scanf("%d%d%d", &n, &m, &k);

        int j=0;
        long long sum=0;
        for(int i=0; i<n; i++) {
            scanf("%d", &a[i]);

            if(a[i] >= m) {
                d[j++] = i + 1;
                if(j == k)
                    sum += d[0] * (n - i);
                else if(j > k)
                    sum += (d[j - k] - d[j - k - 1]) * (n - i);
            }
        }
        
        printf("%lld
", sum);
    }
    return 0;
}


 





原文地址:https://www.cnblogs.com/tigerisland/p/7563547.html