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

题意:问最少要几个连续的数字大于给定的s;

题解: 尺取法:

这是他的执行过程。



先找到一个大于s的序列,然后对这个序列处理,直到小于s,然后在加入后面的数字,不断的循环。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[100000+10];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
long long sum=0;
int st=0,en=0;
int ans=0x3f3f3f3f;
while(1)
{
while(en<n&&sum<m)
sum+=a[en++];
if(sum<m)
break;
ans=min(ans,en-st);
sum-=a[st++];
}
if(ans==0x3f3f3f3f) ans=0;
printf("%d\n",ans);
} return 0;
}

Poj3061Subsequence的更多相关文章

  1. poj--3061--Subsequence(贪心)

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10882   Accepted: 4498 Desc ...

  2. 【尺取法】POJ3061 & POJ3320

    POJ3061-Subsequence [题目大意] 给定长度微n的数列整数及整数s.求出总和不小于s的连续子序列的长度的最小值.如果节不存在,则输出0. [思路] 尺取法五分钟裸裸裸~刷水刷出了罪恶 ...

随机推荐

  1. SpringBoot | 第二十章:异步开发之异步请求

    前言 关于web开发的相关知识点,后续有补充时再开续写了.比如webService服务.发邮件等,这些一般上觉得不完全属于web开发方面的,而且目前webService作为一个接口来提供服务的机会应该 ...

  2. 《C#高效编程》读书笔记05-为类型提供ToString()方法

    System.Object.ToString()是.NET环境中最常用的方法之一.编写类型时,要提供一个合理的ToString版本,否则使用者就不得不自己构造一套可以阅读的表示. public cla ...

  3. 固定ip地址

    IP.  配置文件写数据库文件连接时,之前一直是就写个.  ; 毕竟之前就自己本地用.现在需要,写ip地址,但是公司点的ip的都是自动获得的.并且过一段时间还会改变. 所以,需要固定一下啊. 首先cm ...

  4. 解决Maven依赖下载不全的问题

    背景描述 在日常学习过程中使用Maven构建SpringBoot+SpringCloud服务时,有时会使用非正式版的SpringBoot和SpringCloud(非正式版是指不是最终发布的版本,而是测 ...

  5. HTML 5 Web 存储提供了几种存储数据的方法

    localstorage存储对象分为两种: 1. sessionStorage: session即会话的意思,在这里的session是指用户浏览某个网站时,从进入网站到关闭网站这个时间段,sessio ...

  6. 零基础逆向工程25_C++_02_类的成员权限_虚函数_模板

    1 类的成员权限 1.1 小结: 1.对外提供的函数或者变量,发布成public的 但不能随意改动. 2.可能会变动的函数或者变量,定义成private的 这样编译器会在使用的时候做检测. 3.只有结 ...

  7. ArcGIS API for JavaScript开发初探——HelloMap

    1.前言 在开始ArcGIS API for JavaScript开发之前我们需要了解一些基本的知识: 1.开发工具选什么? 前端技术的开发工具选择是一个仁者见仁智者见智的问题,有人喜欢Hbuilde ...

  8. spring的struts简单介绍

    之前一段时间学习了springmvc+mybatis+spring框架,突然对之前的struts东西有点陌生, 所以这里简单记录下温故而知新的东西吧. 1.  首先建立一个Dynamic Web Pr ...

  9. Masonry 等间隔或等宽高排列多个控件

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...

  10. cms-数据库设计

    业务相关的3张表 1.类型表: CREATE TABLE `t_arctype` (`id` int(11) NOT NULL AUTO_INCREMENT,//id`typeName` varcha ...