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. qt数据库有效插件为空的情况

    打了一周的环境,从ubuntu到win7,搭建环境的时间比写代码的时间都多.先简单的介绍一下我搭建的环境不是纯QT环境,是一个芬兰的软件开发商开发出来的SDK里面完全融合qt,其中qt是以单独的目录存 ...

  2. 507. Perfect Number 因数求和

    [抄题]: We define the Perfect Number is a positive integer that is equal to the sum of all its positiv ...

  3. mybatis学习笔记 spring与mybatis整合

    转载自http://blog.csdn.net/naruto_Mr/article/details/48239357 1.创建web工程,导入spring依赖包与mybatis依赖包,还需要mybat ...

  4. Python 网络爬虫 009 (编程) 通过正则表达式来获取一个网页中的所有的URL链接,并下载这些URL链接的源代码

    通过 正则表达式 来获取一个网页中的所有的 URL链接,并下载这些 URL链接 的源代码 使用的系统:Windows 10 64位 Python 语言版本:Python 2.7.10 V 使用的编程 ...

  5. hdu 4279 Number(G++提交)

    打表找规律: #include<stdio.h> #include<math.h> #define N 250 bool judge(int i,int j) { ;k< ...

  6. 8.python 系统批量运维管理器之pexpect模块

    小插曲 前几节讲了paramiko模块,但是pexpect模块的功能几乎跟paramiko一样,先来分析一下: 1.各自介绍 pexpect是一个通过启动子程序,使用正则表达式对程序输出做出特定响应, ...

  7. What is difference between 3-layer architecture and MVC architecture?

    By Vikas Singh on Sep 26, 2014 In 3-layer architecture  3-layer architecture separates the applicati ...

  8. ASP.NET Web Pages (Razor) FAQ

    ASP.NET Web Pages (Razor) FAQ By Tom FitzMacken|February 7, 2014 Print   This article lists some fre ...

  9. 下载特定区域内街景照片数据 | Download Street View Photos within Selected Region

    作者:姜虹,刘子煜,王玥瑶,杨安琪,天靖居士 街景图片可以通过api下载,但需要提供参数,参数中的poiid.panoid.location可以用来确定位置或全景图片的ID以确定对应的街景图片.优先级 ...

  10. 查询某张表被哪些存储过程或者视图用到的sql语句

    /*查询某张表被哪些存储过程或者视图用到的sql语句*/select distinct object_name(id) from syscomments where id in (select id ...