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. [反汇编练习] 160个CrackMe之033

    [反汇编练习] 160个CrackMe之033. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  2. 高仿QQ6.0側滑菜单之滑动优化(二)

    好了,昨天已经实现了高仿QQ6.0的側滑大致框架.如有兴趣.能够去看下仿QQ6.0側滑之ViewDragHelper的使用(一) 可是之前的实现.仅仅是简单的能够显示和隐藏左側的菜单,可是特别生硬,并 ...

  3. ajax加载时的进度条

    运行效果如下图,pc和移动都可以展示,调用方法很简单,开始调用:loading.baosight.showPageLoadingMsg(false),false代表不现实加载说明,true展示加载说明 ...

  4. 3.nginx反向代理服务器+负载均衡

    nginx反向代理服务器+负载均衡 用nginx做反向代理和负载均衡非常简单, 支持两个用法: 1个proxy, 1个upstream,分别用来做反向代理,和负载均衡 以反向代理为例, nginx不自 ...

  5. CSDN - 进程结束后new出的内存会回收吗?

    http://blog.csdn.net/stanjiang2010/article/details/5386647     关键词:内存回收  

  6. Chrome 前端 插件

    本文内容都来源于偶整理的fetool. 想让更多使用Chrome的小伙伴,体验到这些令人愉悦的小工具,所以单独整理了这篇文章. 如果你是 前端/服务端/设计/面向Github编程/视觉控,相信下列的插 ...

  7. bvlc_reference_caffenet.caffemodel

    #uncoding:utf-8 # set up Python environment: numpy for numerical routines, and matplotlib for plotti ...

  8. 从前端看JavaWeb软件工程中的解耦合

    以下为作者备忘,详情请看 http://www.cnblogs.com/feichengwulai/articles/3412946.html http://blog.csdn.net/piantou ...

  9. Open Source Streaming Server--EasyDarwin

    Welcome to EasyDarwin Streaming Server, which is an open source Streaming Server Based On Appple's D ...

  10. EasyDarwin开源流媒体云平台中boost Base64编解码后与源长度不匹配的bug

    本文转自EasyDarwin团队Alex的博客:http://blog.csdn.net/cai6811376 EasyDarwin云平台中部分协议使用了Base64编码昨晚报文通信的载体.比如在对摄 ...