码迷,mamicode.com
首页 > 其他好文 > 详细

poj3061尺取法

时间:2017-04-23 15:06:08      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:ide   log   return   output   else   less   gif   map   write   

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

 

poj3061尺取法

标签:ide   log   return   output   else   less   gif   map   write   

原文地址:http://www.cnblogs.com/acjiumeng/p/6752452.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!