HDU 5806 - NanoApe Loves Sequence Ⅱ (BestCoder Round #86)

时间:2021-05-01 07:29:34

若 [i, j] 满足, 则 [i, j+1], [i, j+2]...[i,n]均满足

故设当前区间里个数为size, 对于每个 i ,找到刚满足 size == k 的 [i, j], ans += n - j + 1 .

i++ 的时候看看需不需要size-- 就可以更新了。

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define LL long long
const int N = ;
int t, n, m, k;
int a[N];
LL ans;
int main()
{
scanf("%d", &t);
while (t--)
{
scanf("%d%d%d", &n, &m, &k);
for (int i = ; i <= n; i++)
scanf("%d", &a[i]);
ans = ;
int j = , i = , size = ;
while (i <= n && j <= n)
{
if (size < k)
{
if (a[j] >= m) size++;
j++;
}
while (size == k)
{
ans += n - (j-) + ;
if (a[i] >= m) size--;
i++;
}
}
printf("%I64d\n",ans);
}
}