Subsequence

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 12333 Accepted: 5178

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

Southeastern Europe 2006

/*
题目大意:
要求n个数总和不小于s的连续子序列的长度的最小值.
二分+前缀和查询.
*/
#include<iostream>
#include<cstdio>
#define MAXN 100001
using namespace std;
int n,m,s[MAXN],sum[MAXN];
bool check(int x)
{
for(int i=1;i<=n-x+1;i++){
if(sum[x+i-1]-sum[i-1]>=m) return true;
}
return false;
}
int erfen(int l,int r)
{
int mid,ans=0;
while(l<=r){
int mid=(l+r)>>1;
if(check(mid)) ans=mid,r=mid-1;
else l=mid+1;
}
return ans;
}
int main()
{
int t;scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&s[i]),sum[i]=s[i]+sum[i-1];
printf("%d\n",erfen(0,n+1));
}
return 0;
}

Poj 3061 Subsequence(二分+前缀和)的更多相关文章

  1. poj 3061 Subsequence 二分 前缀和 双指针

    地址 http://poj.org/problem?id=3061 解法1 使用双指针 由于序列是连续正数 使用l r 表示选择的子序列的起始 每当和小于要求的时候 我们向右侧扩展 增大序列和 每当和 ...

  2. 题解报告:poj 3061 Subsequence(前缀+二分or尺取法)

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

  3. POJ 3061 Subsequence 二分或者尺取法

    http://poj.org/problem?id=3061 题目大意: 给定长度为n的整列整数a[0],a[1],--a[n-1],以及整数S,求出总和不小于S的连续子序列的长度的最小值. 思路: ...

  4. POJ 3061 Subsequence ( 二分 || 尺取法 )

    题意 : 找出给定序列长度最小的子序列,子序列的和要求满足大于或者等于 S,如果存在则输出最小长度.否则输出 0(序列的元素都是大于 0 小于10000) 分析 : 有关子序列和的问题,都可以考虑采用 ...

  5. POJ 3061 Subsequence 二分查找

    题目大意:给出长度为n的一个序列,给出一个数字S,求长度最短的序列和大于等于S的连续子序列,输出该长度,如果没有答案输出0. 题目思路:看数据范围,这道题就是卡时间的.我们可以用sum[i]记录前i项 ...

  6. poj 3061 Subsequence

    题目连接 http://poj.org/problem?id=3061 Subsequence Description A sequence of N positive integers (10 &l ...

  7. POJ 3061 Subsequence【二分答案】||【尺取法】

    <题目链接> 题目大意: 给你一段长度为n的整数序列,并且给出一个整数S,问你这段序列中区间之和大于等于S的最短区间长度是多少. 解题分析:本题可以用二分答案做,先求出前缀和,然后枚举区间 ...

  8. POJ - 3061 Subsequence(连续子序列和>=s的最短子序列长度)

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

  9. POJ 3061 Subsequence(Two Pointers)

    [题目链接] http://poj.org/problem?id=3061 [题目大意] 给出S和一个长度为n的数列,问最短大于等于S的子区间的长度. [题解] 利用双指针获取每一个恰好大于等于S的子 ...

随机推荐

  1. SQL中CUBE 用法

    转自 http://www.cnblogs.com/dyufei/archive/2009/11/11/2573975.html CUBE 运算符生成的结果集是多维数据集.多维数据集是事实数据(即记录 ...

  2. maven,spring,mybatis集成错误

    maven,spring,mybatis集成的时候单元测试junit测试没问题,但mvn jetty:run 就报错误 错误: org.apache.ibatis.binding.BindingExc ...

  3. C# WinForm多线程(三)---- Control.Invoke[转]

    开发中遇到更新Winform中控件假死问题,通过看了这篇文章了解了原理,感谢!此处标记起来,以备查看! 原文地址:http://www.cnblogs.com/joechen/archive/2009 ...

  4. Worker、Task、Executor三者之间的关系

  5. The Dangers of JavaScript’s Automatic Semicolon Insertion

    Although JavaScript is very powerful, the language’s fundamentals do not have a very steep learning ...

  6. javascript默认中文(汉字/标点)长度均为1的解决

    javascript默认中文(汉字/标点)长度均为1 与后台(java)不一致, function calculate(str) { //var str="你好,哈哈哈000111lll&q ...

  7. 在IT在系统中使用多租户技术的跨部门和虚拟团队的解决方案为员工提供(草案)

    1 前言 经过多年的企业信息化建设,Office系统逐步形成有9营业场所的分部门.9专业应用子系统.20独立的信息模块.330一种方法.这些系统或模块内置于Microsoft IIS.Apache T ...

  8. windows下把Apache加入系统服务

    始 --- 运行,输入cmd,再打开一个命令提示符.分别输入如下命令(每行回车) cd到Apache24\binhttpd.exe-k install-n"servicename" ...

  9. Copy-On-Write技术 (摘录)

    Copy-On-Write技术     Copy-On-Write是写时才copy的意思,有延迟操作的意思在里面.比如往磁盘写数据,先写到的是内存里面,只有文件close或flush时才真正写到磁盘. ...

  10. Git使用完全解析(一)

    是时候来系统的介绍一下Git了.毫无疑问,Git是目前最优秀的分布式版本控制工具,木有之一,可是我见到的很多人还是不会用,我的老东家每天忍受着SVN带来的痛苦,却迟迟不愿切换到Git上,个人感觉,许多 ...