HDU5806 NanoApe Loves Sequence Ⅱ(二分ortwo-pointer)

题意:

求满足区间中>=m的数>=k个的区间有多少

思路:

记小于m的数为0,大于等于m的为1,用sum维护区间和

然后我的做法是枚举右端点,二分左端点得到答案,复杂度O(nlogn)

/* ***********************************************
Author        :devil
************************************************ */
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <stdlib.h>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const int N=2e5+10;
int sum[N],k;
int fun(int p)
{
    int l=1,r=p;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(sum[p]-sum[mid]>=k) l=mid+1;
        else r=mid-1;
    }
    return r+1;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int t,n,m,x;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&k);
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&x);
            sum[i]=sum[i-1]+(x>=m?1:0);
        }
        LL ans=0;
        int r;
        for(r=1;r<=n;r++) if(sum[r]>=k) break;
        for(int i=r;i<=n;i++) ans+=fun(i);
        printf("%I64d\n",ans);
    }
    return 0;
}

然后标解是two-pointer,处理完后枚举左端点,然后指针标记右端点

当区间内个数<k时就r++,大于n就跳出了,时间复杂度O(n)

这个题没有卡nlogn真是良心。。。

/* ***********************************************
Author        :devil
************************************************ */
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <stdlib.h>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const int N=2e5+10;
int sum[N];
int main()
{
    //freopen("in.txt","r",stdin);
    int t,n,m,k,x;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&x);
            sum[i]=sum[i-1]+(x>=m?1:0);
        }
        LL ans=0;
        int r=1;
        for(int l=1;l<=n;l++)
        {
            while(r<=n&&sum[r]-sum[l-1]<k) r++;
            if(r>n) break;
            ans+=(n-r+1);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}
时间: 2024-08-28 03:02:45

HDU5806 NanoApe Loves Sequence Ⅱ(二分ortwo-pointer)的相关文章

HDU5806 NanoApe Loves Sequence Ⅱ

NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 517    Accepted Submission(s): 250 Problem Description NanoApe, the Retired Dog, has returned back to prepare for for t

hdu-5806 NanoApe Loves Sequence Ⅱ(尺取法)

题目链接: NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 262144/131072 K (Java/Others) Problem Description NanoApe, the Retired Dog, has returned back to prepare for for the National Higher Education Entrance Examinatio

HDU5806 NanoApe Loves Sequence Ⅱ (BestCoder Round #86 C)二分

分析:大于等于m的变成1,否则变成0,预处理前缀和,枚举起点,找到第一个点前缀和大于m即可 找第一个点可以二分可以尺取 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; typedef long long LL; const int N = 2e5+5; int T,n,m,k,a[N],sum[N]; int mai

HDU-5806 NanoApe Loves Sequence Ⅱ(two-pointer或二分)

题目大意:给一个整数序列,统计<k,m>子序列的数目.<k,m>序列是满足第k大的数字不比m小的连续子序列. 题目分析:维护一个不小于m的数的个数的后缀和数组,可以枚举序列起点,二分查找右端点序列最近的一个<k,m>序列.因为最近右端点是不减的,所以也可以用two-pointer在O(n)的时间复杂度内得到结果. 代码如下: 使用二分查找: # include<iostream> # include<cstdio> # include<st

5806 NanoApe Loves Sequence Ⅱ(尺取法)

传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 1585    Accepted Submission(s): 688 Description NanoApe, the Retired Dog, has returned back to prepare for for the

5805 NanoApe Loves Sequence

传送门 NanoApe Loves Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others) Total Submission(s): 1323    Accepted Submission(s): 521 Description NanoApe, the Retired Dog, has returned back to prepare for the Natio

NanoApe Loves Sequence Ⅱ(尺取法)

题目链接:NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 339    Accepted Submission(s): 165 Problem Description NanoApe, the Retired Dog, has returned back to prepare for

hdu-5805 NanoApe Loves Sequence(线段树+概率期望)

题目链接: NanoApe Loves Sequence Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 262144/131072 K (Java/Others) Problem Description NanoApe, the Retired Dog, has returned back to prepare for the National Higher Education Entrance Examination! In

Best Coder #86 1002 NanoApe Loves Sequence

NanoApe Loves Sequence Accepts: 531 Submissions: 2481 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others) Problem Description NanoApe, the Retired Dog, has returned back to prepare for the National Higher Education Entr