Hdu-5806 NanoApe Loves Sequence(尺取法)

来源:互联网 发布:掌盟书城java下载 编辑:程序博客网 时间:2024/05/21 10:33
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 withnn numbers and a number mm on the paper. 

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

Note : The length of the subsequence must be no less than kk.
Input
The first line of the input contains an integer TT, denoting the number of test cases. 

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

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

1T10, 2n200000, 1kn/2, 1m,Ai1091≤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 Input
17 4 24 2 7 7 6 5 1
Sample Output

18


题意:问数列中有多少区间满足区间第k大数不小于m。


分析:尺取法求出以每个下标结尾的所有区间个数。


#include<iostream>#include<string>#include<algorithm>#include<cstdlib>#include<cstdio>#include<set>#include<map>#include<vector>#include<cstring>#include<stack>#include<queue>#define INF 2147483640#define eps 1e-9#define N 200005#define MOD 1000000007typedef long long ll;  using namespace std;int t,n,m,k,tot,a[N];int main(){scanf("%d",&t);for(int T = 1;T <= t;T++){ll ans = 0;int i,j,sum = 0,tot = 0;scanf("%d%d%d",&n,&m,&k);for(int i = 1;i <= n;i++) scanf("%d",&a[i]);for(j = 1;j <= n && sum != k;j++) {if(a[j] >= m) {sum++;i = j;}tot++;}if(sum != k){cout<<0<<endl;continue;}tot--,sum--;for(;i <= n;i++){tot++;if(a[i] >= m) sum++;while(sum >= k){if(a[i - tot + 1] >= m) sum--;tot--; }tot++;sum++;ans += i - tot + 1ll; }cout<<ans<<endl;}}


0 0
原创粉丝点击