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. js中数值各进制之间的转换

    十进制转换为二进制 toString()方法可把一个 Number 对象转换为一个字符串,并返回结果.语法如下: NumberObject.toString(radix); 其中,radix为可选.规 ...

  2. Vue和vue-template-compiler版本不一致

    vue项目,package.json中Vue和vue-template-compiler版本不一致时,执行npm run dev有时会报错, 提示vue和vue-template-compiler版本 ...

  3. Py西游攻关之基础数据类型(三)-元组

    Py西游攻关之基础数据类型 - Yuan先生 https://www.cnblogs.com/yuanchenqi/articles/5782764.html 六 tuple(元组) 元组被称为只读列 ...

  4. 【剑指Offer面试编程题】题目1520:树的子结构--九度OJ

    题目描述: 输入两颗二叉树A,B,判断B是不是A的子结构. 输入: 输入可能包含多个测试样例,输入以EOF结束. 对于每个测试案例,输入的第一行一个整数n,m(1<=n<=1000,1&l ...

  5. ionic3记录之APP运行时网络判断

    判断设备网路是否正常: 安装插件: ionic cordova plugin add cordova-plugin-network-information npm install --save@nat ...

  6. helm基本用法

    一.helm命令 helm search #关键字搜索charts helm pull #压缩下载chart到本地,可以使用--untar下载解压) helm install #部署chart到kub ...

  7. web应用基础架构

    1.web中间件 中间件是一类连接软件组件和应用的计算机软件,它包括一组服务.以便运行在一台或多台服务器上的多个软件通过网络进行交互.该技术所提供的互操作性,推动了一致分布式体系架构的演进,该架构通常 ...

  8. KDE Plasma 5.17 即将发布

    导读 Plasma 5.17上个月达到了beta版本,而下周将发布Plasma 5.17.0版本!KDE桌面的大更新只有几天了.因此,开发人员一直在整理它,同时也集思广益讨论Plasma 5.18应该 ...

  9. SDL 显示汉字

    #include "stdafx.h" #pragma comment(lib,"SDL.lib") #pragma comment(lib,"SDL ...

  10. 二、多线程基础-乐观锁_悲观锁_重入锁_读写锁_CAS无锁机制_自旋锁

    1.10乐观锁_悲观锁_重入锁_读写锁_CAS无锁机制_自旋锁1)乐观锁:就像它的名字一样,对于并发间操作产生的线程安全问题持乐观状态,乐观锁认为竞争不总是会发生,因此它不需要持有锁,将 比较-设置 ...