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;
}


 

 

 

 

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

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...
  • ren_hui
  • ren_hui
  • 2013年06月19日 19:47
  • 340

LA 2678 Subsequence(尺取法)

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

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

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

[2678]5-1 继承与派生

 5-1 继承与派生 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 通过本题目的...

HOJ 2678 Stars

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

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

近期编程想用CList维护一个对象,来方便的实现增,删,改,查,里面要放多个元素,想实现这种,就要自己写结构体,或是类。类的话就让他派生自CObject吧,然后接下来会遇到两个问题,我在这里整理一下我...

洛谷 P2678 跳石头

二分
  • Rlt1296
  • Rlt1296
  • 2016年11月17日 09:53
  • 159

《算法竞赛-训练指南》第一章-1.21_LA2678

其实这道题目算是一到水题哈,但是由于自己是菜鸟中的小菜鸟中的小菜鸟,所以也是没有独自做出来哈。 题目的描述是这样的,给你N个数字的排列,问你最小的连续子序列的长度,满足和大于等于K。 我的思路...

la2678(前缀和,二分/尺取法)

/* translation: 有n个正整数组成一个序列。给定整数s,求长度最短的连续序列,使得他们的和大于等于s solution: 前缀和,尺取法/二分 这道题可用尺取法或...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:2678 - Subsequence
举报原因:
原因补充:

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