Subsequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16520   Accepted: 7008

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

Source

 
题目大意:t个测试数据 对于一个长度为N的序列 求出总和不小于S的连续子序列的长度的最小值。
题解:
解法一、
二分搜索
前缀和是满足单调性的,计算从每一个数开始总和刚好大于s的长度。具体实现就是
二分搜索s[i]+s是否存在于前缀和数组中,就是查找以i+1开头的总和刚好大于s的最短长度。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int t,n,s,x;
int sum[];
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&s);
for(int i=;i<=n;i++){
scanf("%d",&x);
sum[i]=sum[i-]+x;
}
if(sum[n]<s){
printf("0\n");
continue;
}
int res=n;
for(int i=;sum[i]+s<=sum[n];i++){
int t=lower_bound(sum+i+,sum+n+,sum[i]+s)-sum;
res=min(res,t-i);
}
printf("%d\n",res);
}
return ;
}
时间复杂度nlog(n)
解法二、
尺取法 时间复杂度O(n)
#include<iostream>
#include<cstdio>
using namespace std;
int res,t,n,s,l,r,sum;
int a[];
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&s);
for(int i=;i<=n;i++)scanf("%d",&a[i]);
l=r=;sum=;res=n+;
for(;;){
while(r<=n&&sum<s){
sum+=a[r++];
}
if(sum<s)break;//必须先判断才能更新解
res=min(res,r-l);
sum-=a[l++];
}
if(res>n)printf("0\n");
else printf("%d\n",res);
}
return ;
}

POJ3061 Subsequence的更多相关文章

  1. poj3061 Subsequence(尺取法)

    https://vjudge.net/problem/POJ-3061 尺取发,s和t不断推进的算法.因为每一轮s都推进1所以复杂度为O(n) #include<iostream> #in ...

  2. poj3061 Subsequence(尺取)

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

  3. POJ3061 Subsequence 尺取or二分

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

  4. poj3061 Subsequence&&poj3320 Jessica's Reading Problem(尺取法)

    这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针 ...

  5. poj3061 Subsequence【尺取法】

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

  6. poj3061 Subsequence ,尺取法

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

  7. POJ3061——Subsequence(尺取法)

    Subsequence POJ - 3061 给定长度为n的数列整数a0,a1,a2-an-1以及整数S.求出总和不小于S的连续子序列的长度的最小值,如果解不存在输出0. 反复推进区间的开头和末尾,来 ...

  8. [POJ3061]Subsequence(二分,前缀和)

    题目链接:http://poj.org/problem?id=3061 题意:给一个长为n的数列和整数s,求一个连续的子序列,使得这个子序列长度最短并且不小于这个整数s. 统计[1~i]的子序列和su ...

  9. POJ3061 Subsequence(二进制前缀和法律+仿真足)

    二分法+前缀和法律 满足子序列长度的条件(0,n)之间,sum[x+i]-sum[i]从i元素开始序列长度x和.前缀和可在O(n)的时间内统计 sum[i]的值.再用二分找出满足条件的最小的子序列长度 ...

随机推荐

  1. jmeter的master、slave模型启动方法

    机器A为master:机器B为slave:可以一个master挂多个slave,方法就是-R参数后面跟一个逗号分割的IP列表 slave启动命令:./jmeter-server -Djava.rmi. ...

  2. c# Dictionary拓展2个key得到1个value

    using System.Collections.Generic; using System.Collections; Dictionary<Tuple<int, int>, int ...

  3. angular - 新建项目 - 2

    ng new testNg 新建项目后,从网络上拉取模板(最后缓存下来,我们下次创建项目的时间将会减少80%) 安装过程中,需要我们提供Git账号和姓名 最后,我们进入 useNg 然后,启动服务器 ...

  4. com关于IUnknown接口

    com定义的每个接口都必须从IUnknown继承过来,主要原因是IUnknown接口提供了两个很重要的特性:生存期控制和接口查询. 客户程序仅仅能通过接口与com对象进行通信.尽管客户程序能够无论对象 ...

  5. 晶振及COMS电路

    COMS电路:http://www.docin.com/p-246885507.html 简介:无源晶振和有源晶振 电子线路中的晶体振荡器也分为无源晶振和有源晶振两种类型.无源晶振与有源晶振的英文名称 ...

  6. 找回Xcode7的代码折叠功能

    升级到Xcode7后,会发现代码折叠功能不见了,这是怎么回事? 其实这个功能还在的,用以下的快捷键仍然可以折叠代码.只是不能用鼠标实现折叠了:在Xcode菜单里选择Preference——Text E ...

  7. Gson解析数组和list容器

    Gson解析数组和list容器 使用Gson解析首先须要增加架包文件:gson-2.2.4.jar 定义一个类Student: public class Student { String name=& ...

  8. ActionChains报错问题总结

    在使用Python2.7+Selenium3.0.2+Firefox50.1时,鼠标事件ActionChains运行总是报错,报错信息如下: C:\Python36\python3.exe C:/Us ...

  9. Java中Class.forName()的作用(转载)

    http://www.360doc.com/content/10/0712/10/1720440_38421273.shtml# 使用jdbc方式连接数据库时会使用一句代码Class.forName( ...

  10. 前端基础——CSS盒子模型

    如今很多网页都是由很多个"盒子"拼接.嵌套而成,所以多少接触过网页设计的朋友一定都对CSS盒子模型有所了解. 为了更好的说明,先举个通俗的样例:在一个仓库中放了10个纸箱,每一个纸 ...