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. Linux centosVMware Linux监控平台介绍、zabbix监控介绍、安装zabbix、忘记Admin密码如何做

    一.Linux监控平台介绍 cacti.nagios.zabbix.smokeping.open-falcon等等 cacti.smokeping偏向于基础监控,成图非常漂亮 cacti.nagios ...

  2. scrapy shell中遇到的坑

    如果直接scrapy shell +网址  然后发现返回200 但是request和response的网址不同,那么可以使用百度短网址 https://dwz.cn/ 进行缩短.这样一般就能解决问题

  3. 「CF1037D」Valid BFS?

    传送门 Luogu 解题思路 考虑直接模拟 \(\text{BFS}\) 的过程. 对于每一个节点的儿子,先遍历在输入序列中靠前的,判断 \(\text{BFS}\) 是否匹配即可. 细节注意事项 注 ...

  4. Python 全国考级二级

    第1章  Python概述 [Python语言简介] Python是一种跨平台.开源.免费的解释型高级动态编程语言,是一种通用编程语言. Python支持命令式编程和函数式编程两种方式,并且完全支持面 ...

  5. Python - ORM(数据库相关)

    1. 概念 目标:类/对象操作 -> SQL语句 -> DB API -> 再在数据库中执行.ORM做前两部,因为ORM本身无法操作数据库.参考:https://baike.baid ...

  6. 中山普及Day13——普及

    又是迷之自信的说...估的230,考的50整,我欲上天呐!!! T1:深渊(怕不是黑暗种族聚集地???) 思路:动归.而且是简单动归.转移方程:Fi,j=max(Fi-1,j,Fi,j,Fi-1,(j ...

  7. GreenPlum 提取数据表信息

    参考: https://www.alberton.info/postgresql_meta_info.html https://my.oschina.net/Kenyon/blog/226600 列信 ...

  8. jQuery Validation Engine

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...

  9. Percona-Toolkit 之 pt-kill 用法

    生产环境中我们时常遇到这样的情况,数据库性能恶劣,需要马上杀掉部分会话,不然数据库就夯死.我们可以先找show processlist的输出来杀会话,但是比较麻烦.pt-kill为我们解决了杀会话问题 ...

  10. Linux centosVMware shell脚本中的逻辑判断、文件目录属性判断、if特殊用法、case判断

    一.shell脚本中的逻辑判断 格式1:if 条件 ; then 语句; fi 格式2:if 条件; then 语句; else 语句; fi 格式3:if …; then … ;elif …; th ...