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

poj 3061

时间:2014-11-20 06:46:04      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   io   ar   os   sp   for   strong   

B - Subsequence
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Appoint description: 

Description

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<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<vector>
#include<set>
using namespace std;
#define INF 1<<30
int t,n,s,num[100010],sum[100010],ans;
int main()
{
      scanf("%d",&t);
      while(t--)
      {
            scanf("%d%d",&n,&s);
            for(int i=1;i<=n;i++)
            {
                 scanf("%d",&num[i]);
                 sum[i]=sum[i-1]+num[i];
            }
            if(sum[n]<s)
            {
                  printf("0\n");
                  continue;
            }
            ans=INF;
            for(int i=0;sum[i]+s<=sum[n];i++)
            {
                  int t=lower_bound(sum+i+1,sum+n+1,sum[i]+s)-(sum+i);
                  ans=min(ans,t);
            }
            printf("%d\n",ans);
      }
      return 0;
}

  2法二:

     尺度法

            主要思想为:当a1,  a2  , a3 满足和>=S,得到一个区间长度3,那么去掉开头a1,   剩下 a2,a3,判断是否满足>=S,如果满足,那么区间长度更新,如果不满足,那么尾部向后拓展,判断a2,a3,a4是否满足条件。重复这样的操作。当一个区间满足条件时,那么去掉区间开头第一个数,得到新区间,判断新区间是否满足条件,如果不满足条件,向后扩展。

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<vector>
#include<set>
using namespace std;
const int maxn=100010;
int num[maxn];
int n,S;

int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&S);
        for(int i=1;i<=n;i++)
            scanf("%d",&num[i]);
        int sum=0,s=1,e=1;
        int ans=n+1;
        for(;;)
        {
            while(e<=n&&sum<S)
                sum+=num[e++];
            if(sum<S)
                break;
            ans=min(ans,e-s);
            sum-=num[s++];
        }
        if(ans==n+1)
            cout<<0<<endl;
        else
            cout<<ans<<endl;
    }
    return 0;
}

  



poj 3061

标签:des   blog   http   io   ar   os   sp   for   strong   

原文地址:http://www.cnblogs.com/a972290869/p/4109631.html

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