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. Linq查询满足条件记录集

    通过linq查询datatable数据集合满足条件的数据集 1.首先定义查询字段的变量,比方深度 string strDepth=查询深度的值: var dataRows = from datarow ...

  2. 【转】supervisord使用

    Supervisor (http://supervisord.org) 是一个用 Python 写的进程管理工具,可以很方便的用来启动.重启.关闭进程(不仅仅是 Python 进程).除了对单个进程的 ...

  3. [Algorithom] Stack Data Structure in JavaScript

    A stack is a collection of items that obeys the principle of "last in, first out". Like a ...

  4. 深度探究apk安装过程

    一.先验知识 0.PcakageaManagerService版本号变化 1.概述 2.PackageManagerService服务启动流程 3. PackageManagerService入口 二 ...

  5. 大牛blog汇总

    系列专题的文件夹 01. Java String系列 (共3篇) 02. Java异常系列 (共3篇) 03. Java 时间日期系列 (共7篇) 04. java io系列 (共26篇) 05, J ...

  6. 算法排序-lowB三人组

    冒泡排序思路: 选择排序思路: 插入排序思路: 小结: 详细代码解释看下一篇

  7. C# WPF DataGrid控件实现三级联动

    利用DataGrid控件实现联动的功能,在数据库客户软件中是随处可见的,然而网上的资料却是少之又少,令人崩溃. 本篇博文将介绍利用DataGrid控件模板定义的三个ComboBox实现“省.市.区”的 ...

  8. 解决对象不支持“getElementsByClassName”属性或方法 ie兼容性

      解决 IE 或者兼容模式不支持 document.getElementsByClassName() 的方法 自已实现document.getElementsByClassName():   网页错 ...

  9. Error: EACCES: permission denied, mkdir '/root/.nvm/versions/node/......

    当我执行npm install -g node-inspector的时候报错. 所以就去网上搜索了一下答案. 有这么几种答案.请看完再执行相关命令 有说需要在前面加上sudo命令的. 不能解决我出现的 ...

  10. Java,获取文件的Base64字符串,解码Base64字符串还原文件

    在jdk1.8以前,获取文件Base64字符串需要用到第三方库,从1.8开始,Java中引入了Base64相关的类 以下是代码示例 获取文件的Base64编码字符串 import java.io.Fi ...