POJ 3061 Subsequence(尺取法)


                        Subsequence

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<cstdio>
#define min(a,b) (a<b?a:b)
using namespace std;

int main()
{
    int sum[100005];
    int t,ans,i,N,S,temp,j;
    scanf("%d",&t)!=EOF;
    while(t--)
    {
        ans=1000005;
        sum[0]=0;
        scanf("%d%d",&N,&S);
        for(i=1;i<=N;i++)
        {
            scanf("%d",&temp);
            sum[i]=sum[i-1]+temp;
        }
        for(i=1,j=1;j<=N;j++)
        {
            if(sum[j]-sum[i]>=S)
            {
                while(sum[j]-sum[i]>=S&&i<=j)
                {
                    ans=min(ans,j-i);
                    i++;
                }
            }
        }
        if(ans==1000005)ans=0;
        printf("%d\n",ans);
    }
    return 0;
}

优质内容筛选与推荐>>
1、第二个冲刺阶段(10天)--张永组---成员 (王丹)
2、通过权限控制参数显隐和校验
3、数据类型回顾——NaN和isNaN—JS学习笔记2015-6-4(第48天)
4、UVA 10635 Prince and Princess【LCS 问题转换为 LIS】
5、深入理解计算机系统cp1:存储单位与编码


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn