Subsequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10172   Accepted: 4160

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

题意是给出一个序列,要求从这个序列中找出几个连续的数,使得这些数的和大于给定的S,求这个数的最小值。

和51nod上面的建设国家很像,都是建立一个队列,然后不断查找。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; long long n,s,a[1000005]; int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); int test,i,ans,start;
long long sum;
scanf("%d",&test); while(test--)
{
scanf("%lld%lld",&n,&s);
for(i=1;i<=n;i++)
{
scanf("%lld",a+i);
}
ans=n+1;
a[0]=0;
sum=0;
start=1;
for(i=1;i<=n;i++)
{
sum += a[i];
while(sum>s)
{
sum = sum - a[start];
start++;
}
if(sum+a[start-1]>s && i-(start-1)+1<ans)
{
ans=i-(start-1)+1;
}
}
if(ans == n+1)
{
cout<<0<<endl;
}
else
{
cout<<ans<<endl;
}
}
//system("pause");
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 3061:Subsequence 查找连续的几个数,使得这几个数的和大于给定的S的更多相关文章

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

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

  2. [ACM] POJ 3061 Subsequence (仿真足)

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8403   Accepted: 3264 Descr ...

  3. poj 3061 Subsequence

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

  4. POJ 3061 Subsequence 二分查找

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

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

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

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

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

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

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

  8. POJ 3061 Subsequence(尺取法)

    题目链接: 传送门 Subsequence Time Limit: 1000MS     Memory Limit: 65536K 题目描述 给定长度为n的数列整数以及整数S.求出总和不小于S的连续子 ...

  9. POJ 3061 Subsequence(Two Pointers)

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

随机推荐

  1. CSS - icon图标(icon font)

    1. 概念 这个小红点是图标,图标在CSS中实际上是字体. 2. 为什么出现本质是字体的图标? 2.1 图片增加了总文件的大小. 2.2 图片增加了额外的http请求,大大降低网页的性能. 2.3 图 ...

  2. Python中from scipy.misc import imread报错的原因?

    from scipy.misc import imread 报错 查询后其原因是from scipy.misc import imread,imwrite 等方法已经被弃用,Python已经将imre ...

  3. python如何在文件每一行前面加字符串?

    对于python中原来的文件,需要在每一行前面添加一个特舒符号,比如逗号或者“--”,需要先把原来的文件内容记录下之后,进行清空,再进行写入,另外需要注意的是r+和a+都是可写可读,不过a+是从文件末 ...

  4. Linux 运维常用命令

    参考: https://segmentfault.com/a/1190000009745139 http://blog.51cto.com/xuqq999/774714 .查看有多少个IP访问: aw ...

  5. 微软结合Veritas,使用Azure云中的混合存储

    导读 在与Symantec(Symantec)脱离10年(通常令人不愉快的关系)一周年之际,Veritas Technologies在2月22日宣布与Microsoft Corp. 建立了多年战略全球 ...

  6. 解决css中display:inline-block的兼容问题

    *display:inline; *zoom:1; 不多说,ie6/7直接在元素添加以上的属性即可.

  7. ExpandableListActivity

    main.xml: <?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:an ...

  8. boost::timer demo

    #include <iostream> #include <boost/timer.hpp> //timer的头文件 using namespace boost; //打开bo ...

  9. 075、Java面向对象之定义匿名对象

    01.代码如下: package TIANPAN; class Book { // 定义一个新的类 private String title; // 书的名字 private double price ...

  10. 防火墙、WAF、IPS、IDS都是什么

    防火墙 (Firewall) 别名防护墙,于1993发明并引入国际互联网. 他是一项信息安全的防护系统,依照特定的规则,允许或是限制传输的数据通过.在网络中,所谓的防火墙是指一种将内网和外网分开的方法 ...