HDU 5806 NanoApe Loves Sequence Ⅱ

将大于等于m的数改为1,其余的改为0。问题转变成了有多少个区间的区间和>=k。可以枚举起点,二分第一个终点 或者尺取法。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-8;
void File()
{
    freopen("D:\in.txt","r",stdin);
    freopen("D:\out.txt","w",stdout);
}
inline int read()
{
    char c = getchar();  while(!isdigit(c)) c = getchar();
    int x = 0;
    while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); }
    return x;
}

const int maxn=200000+10;
int T,n,m,k,a[maxn],sum[maxn];

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&k); sum[0]=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            if(a[i]>=m) a[i]=1;
            else a[i]=0;
            sum[i]=sum[i-1]+a[i];
        }
        LL ans=0;

        for(int i=1;i<=n;i++)
        {
            int L=i,R=n,pos=-1;
            while(L<=R)
            {
                int mid=(L+R)/2;
                if(sum[mid]-sum[i-1]>=k) pos=mid,R=mid-1;
                else L=mid+1;
            }
            if(pos==-1) continue;
            ans=ans+n-pos+1;
        }
        printf("%lld
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zufezzt/p/5747329.html