2678 - Subsequence

原创 2015年07月10日 15:53:36

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 

Many test cases will be given. 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 there isn't such a subsequence, print 0 on a line by itself.

Sample Input 

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 <stdio.h>
using namespace std;
const int MAXN = 100010;
int g[MAXN];
int n, m;
int main()
{
    int i, sum, ans, l;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        ans = n+1;
        l = 0;
        sum = 0;
        for(i=0; i<n; i++)
        {
            scanf("%d",&g[i]);
        }
        for(i=0; i<n; i++)
        {
            sum+=g[i];
            while(sum>m)
            {
                ans = min(ans, i-l+1);
                sum -= g[l++];
            }
        }
        if(ans == n+1)ans = 0;
        printf("%d\n",ans);
    }
    return 0;
}


 

 

 

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

UVALive2678 UVA1121 Subsequence【前缀和+二分搜索+尺取法】

Sequence A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000...

icpcarchive 2678 - Subsequence

2678 - Subsequence A sequence of N positive integers (10 N , each of them less than or equal 10000,...

2678 - Subsequence

链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&ca...

LA 2678 Subsequence(尺取法)

Subsequence Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVAL...

洛谷 P2678 跳石头

二分

HOJ 2678 Stars

天空中有很多星星,这些星星有一定等级,等级计算规则很简单。星星的等级等于该星星可统治的星星的个数。如果星星A(x1,y1,z1)可以通知星星B(x2,y2,z2),则x2≤x1,y2≤y1,z2≤z1...

HOJ Stars 2678

这个题与poj的思路有点类似 点击打开poj Stars #include #include #include using namespace std; const int MAXN ...

MFC中使用自定义CObject派生类重载“=”“==”操作符 error C2248: “CObject::operator =” error C2678: 二进制“==”

近期编程想用CList维护一个对象,来方便的实现增,删,改,查,里面要放多个元素,想实现这种,就要自己写结构体,或是类。类的话就让他派生自CObject吧,然后接下来会遇到两个问题,我在这里整理一下我...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)