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. 【前端】CSS3实现弹出效果

    36氪这个网站上的登录框弹出的时候挺帅气的,想知道它是怎么做的 .. 今天通过问新爷再加上自己琢磨琢磨写出一个小小Demo - 上代码 <!DOCTYPE html> <html&g ...

  2. Weka 入门1

    本人也是借鉴网上他人资料.主要介绍使用java调用Weka库. 首先介绍weka,Weka的全名是怀卡托智能分析环境,是基于开源环境的机器学习和数据挖掘软件.我们可以去weka官网下载最新的Weka软 ...

  3. <转>为什么转置一个512x512的矩阵,会比513x513的矩阵慢很多?

    转自evol128  特此表示感谢 http://evol128.is-programmer.com/posts/35453.html 问题的出处:http://stackoverflow.com/q ...

  4. puppet_list

  5. Jquery常用方法篇(一)

    1.add方法:将匹配的元素添加到当前的jquery对象集合中. 语法 add(expr,[context]) 参数1表示 用于匹配元素选择器(待添加的对象),或者用于动态生成的HTML代码(字符串) ...

  6. springboot 详细配置2

    # =================================================================== # COMMON SPRING BOOT PROPERTIE ...

  7. 动作-CCActionInterval之CCActionEase家族

    补间动作也是一个包装器(之前叫补间动画,从字面上讲,叫动作更合适一些.).你也可以叫他缓释动作. 1.含义 补间动作改变的是,内部动作的执行速率(注意,并没有改变执行的最终效果,和执行的时间.)关于这 ...

  8. Cisco 交换机与路由器故障处理方法分享

    一.网络的复杂性 一般网络包括路由.拨号.交换.视频.WAN(ISDN.帧中继.ATM.…).LAN.VLAN.… 二.故障处理模型 1. 界定问题(Define the Problem) 详细而精确 ...

  9. java pdu短信解码

    java pdu短信解码 长短信未验证 有兴趣的可以试试 根据python的方法改写的 /** * PDU短信解析 * * * @param pduPayload * @return */ publi ...

  10. Android(java)学习笔记158:Java程序入口和Android的APK入口

    1.Java程序的入口:static main()方法 public class welcome extends Activity { @Override public void onCreate(B ...