首页 > 其他 > 详细

Subsequence

时间:2020-02-01 15:17:48      阅读:62      评论:0      收藏:0      [点我收藏+]

Subsequence

该题要求的是连续序列,所以有两个方法,一个是是二分加前缀和,复杂度为\(O(nlog(n))\),另一种方法是用尺取法,复杂度为\(O(n)\)

尺取法代码:

// Created by CAD on 2020/2/1.
#include <iostream>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1e5+5;
int a[maxn];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;cin>>t;
    while(t--){
        int n,s;  cin>>n>>s;
        for(int i=1;i<=n;++i)
            cin>>a[i];
        int head=1,tail=1;
        int sum=a[1],ans=inf;
        while(head<=n){
            while(sum>=s&&tail<=head) ans=min(ans,head-tail+1),sum-=a[tail++];
            sum+=a[++head];
        }
        if(ans==inf)    cout<<0<<endl;
        else    cout<<ans<<'\n';
    }
    return 0;
}

Subsequence

原文:https://www.cnblogs.com/CADCADCAD/p/12248424.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!