zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    redhat linux tftp
    mysql
    mysql操作!
    【Android】第一个JNI测试程序
    【android】【google map api v2】google 地图 api v2
    【Android】【转】内存耗用:VSS/RSS/PSS/USS
    【Android】获取Mac地址【2】
    【Android】每个Activity中加入引导界面
    【JNI】javah使用(初步)
    【Android】Eclipse svn插件安装说明
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/6752452.html
Copyright © 2011-2022 走看看