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

一开始c++交不过,g++过,原来是int 输入的时候是long long,wa了,g++交没这个问题

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<cmath> const int maxn=1e5+5;
typedef long long ll;
using namespace std;
ll a[maxn];
ll s[maxn];
ll n,ss;
bool check(int x)
{
if(x>=n)
{
return true;
}
for(int t=x;t<=n;t++)
{
// cout<<s[t]<<" "<<s[t-x+1]<<endl;
if(s[t]-s[t-x+1]>=ss)
{ return true;
}
}
return false;
}
int main()
{
int T;
cin>>T;
while(T--)
{
scanf("%lld%lld",&n,&ss);
for(int t=1;t<=n;t++)
{
scanf("%lld",&a[t]);
}
memset(s,0,sizeof(s));
for(int t=1;t<=n;t++)
{
s[t]=s[t-1]+a[t];
}
if(s[n]<ss)
{
cout<<"0"<<endl;
}
else
{
int l=0,r=100000;
int mid;
while(l<=r)
{
mid=(l+r)/2;
if(check(mid))
{
r=mid-1;
}
else
{
l=mid+1;
}
}
mid=(l+r)/2;
cout<<mid<<endl;
}
} return 0;
}

Subsequence(二分)的更多相关文章

  1. Poj 3061 Subsequence(二分+前缀和)

    Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12333 Accepted: 5178 Descript ...

  2. CSU1553 Good subsequence —— 二分 + RMQ/线段树

    题目链接: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1553 Description Give you a sequence of n n ...

  3. Intel Code Challenge Final Round D. Dense Subsequence 二分思想

    D. Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  4. POJ 3061 Subsequence 二分查找

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

  5. POJ-3061 Subsequence 二分或尺取

    题面 题意:给你一个长度为n(n<100000)的数组,让你找到一个最短的连续子序列,使得子序列的和>=m  (m<1e9) 题解: 1 显然我们我们可以二分答案,然后利用前缀和判断 ...

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

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

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

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

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

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

  9. Subsequence poj 3061 二分(nlog n)或尺取法(n)

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9236   Accepted: 3701 Descr ...

  10. 【二分答案nlogn/标解O(n)】【UVA1121】Subsequence

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

随机推荐

  1. php扩展开发2--添加类

    1.需要实现的细节 实现一个person类 实现一个doing方法和saying方法 2.第一个扩展 2.1创建类的扩展: [root@bogon ext]# cd /usr/local/src/ph ...

  2. Mask_RCNN openpose AlphaPose Kinect姿态识别

    1.Mask_RCNN ---------------------------------------------------------------------------------------- ...

  3. libcurl用法

    本文以向百度搜索开放平台搜索关键字所对应的推荐搜索条目为例子: url:http://m.baidu.com/su?wd=%s&action=opensearch&ie=utf-8 ( ...

  4. 算法Sedgewick第四版-第3章Searching-搜索总结

  5. Django--初始化

    1.Django介绍 它是一个WEB框架 Django--大而全 tornado.flask--小而精 2.Django安装 https://www.djangoproject.com/downloa ...

  6. Luogu 3594 [POI2015]WIL-Wilcze doły

    简单题. 考虑没有修改数字的条件的限制,我们直接用双指针扫描就可以计算出答案了. 然后考虑加入修改数字的条件,只要用单调队列维护出当前两个指针表示的区间中长度为$d$的一段区间的最大值,用总和减掉这个 ...

  7. Go程序设计3——并发编程

    1 channel 一般channel的声明形式为: var chanName chan ElementType 与一般的变量声明不同的地方仅仅是在类型之前增加了chan关键字.ElementType ...

  8. Java学习——JSTL标签与EL表达式之间的微妙关系

    原文总结的太好了,忍不住记录.转发. 原文地址:http://blog.csdn.net/u010168160/article/details/49182867 目录(?)[-] 一EL表达式 EL相 ...

  9. Topshelf + Quartz2.5 创建基于windows服务

    1.创建一个定时调度Quartz类 using Quartz; using Quartz.Impl; using System; using System.Collections.Generic; u ...

  10. hbuilder h5 原生socket

    在网上搜索了很多资料都不行,要么就是不能发送数据,要么就不能接收数据,使用如下的方法可以接收数据,一个一个字节接收: 有部分限制是需要明确知道要接收多少个字节,否则容易出现接收异常.. var tes ...