• poj3061尺取法


    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive elements of the sequence, the sum of which is greater than or equal to S.

    Input

    The first line is the number of test cases. For each test case the program has to read the numbers N and S, separated by an interval, from the first line. The numbers of the sequence are given in the second line of the test case, separated by intervals. The input will finish with the end of file.

    Output

    For each the case the program has to print the result on separate line of the output file.if no answer, print 0.

    Sample Input

    2
    10 15
    5 1 3 5 10 7 4 9 2 8
    5 11
    1 2 3 4 5

    Sample Output

    2
    3
    题意:找最小的连续的数来大于给定的数(好像这类题目都是要找连续的数额。。)
    题解:尺取法咯,当然还有别的方法只是时间复杂度高一点
    尺取法的:
    #include<map>
    #include<set>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define pi acos(-1)
    #define ll long long
    #define mod 1000000007
    
    using namespace std;
    
    const int N=100000+5,maxn=100+5,inf=0x3f3f3f3f;
    
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        int t,n,s,a[N],sum[N];
        cin>>t;
        while(t--){
            cin>>n>>s;
            for(int i=0;i<n;i++)cin>>a[i];
            int sum=0,st=0,en=0,ans=n+2;
            while(en<=n){
                while(sum<s&&en<=n){
                    sum+=a[en++];
                }
            //    cout<<sum<<" "<<st<<" "<<en<<endl;
                if(sum>=s)ans=min(ans,en-st);
                sum-=a[st++];
            }
            if(ans==n+2)cout<<0<<endl;
            else cout<<ans<<endl;
        }
        return 0;
    View Code

    非尺取:

    #include<map>
    #include<set>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define pi acos(-1)
    #define ll long long
    #define mod 1000000007
    
    using namespace std;
    
    const int N=100000+5,maxn=100+5,inf=0x3f3f3f3f;
    
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        int t,n,s,a[N],sum[N];
        cin>>t;
        while(t--){
            cin>>n>>s;
            for(int i=0;i<n;i++)
            {
                cin>>a[i];
                if(i==0)sum[i]=a[i];
                else sum[i]=sum[i-1]+a[i];
            }
            int res=n+2;
            for(int i=0;sum[i]+s<=sum[n-1];i++)
            {
                int j=lower_bound(sum+i,sum+n,sum[i]+s)-sum;
          //      cout<<j<<endl;
                res=min(res,j-i);
            }
            if(sum[n-1]>=s)cout<<res<<endl;
            else cout<<0<<endl;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    C语言运算符
    C语言的scanf函数
    C语言的数据、常量和变量
    js 操作样式属性
    点击图片显示或隐藏密码案例
    js 操作元素
    DOM 获取元素
    js 字符串对象
    js 数组
    js 倒计时 案例
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/6752452.html
Copyright © 2020-2023  润新知