poj3061 Subsequence【尺取法】
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
Source
#include<cstdio>
#include<cstring>
#include <iostream>
using namespace std;
const int maxn=100005;
int a[maxn];
int n,s;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&s);
for(int i=0;i<n;++i)
scanf("%d",&a[i]);
int i=0,j=0,ans=n+1,sum=0;
while(1)
{
while(sum<s && i<n)
sum+=a[i++];
if(sum<s) break;
ans=min(ans,i-j);
sum-=a[j++];
}
if(ans>n) ans=0;
printf("%d\n",ans);
}
return 0;
}
poj3061 Subsequence【尺取法】的更多相关文章
- POJ3061——Subsequence(尺取法)
Subsequence POJ - 3061 给定长度为n的数列整数a0,a1,a2-an-1以及整数S.求出总和不小于S的连续子序列的长度的最小值,如果解不存在输出0. 反复推进区间的开头和末尾,来 ...
- POJ 3061 Subsequence 尺取法
转自博客:http://blog.chinaunix.net/uid-24922718-id-4848418.html 尺取法就是两个指针表示区间[l,r]的开始与结束 然后根据题目来将端点移动,是一 ...
- POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13955 Accepted: 5896 Desc ...
- POJ3061 Subsequence 尺取or二分
Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...
- POJ 3061 Subsequence(尺取法)
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18145 Accepted: 7751 Desc ...
- POJ 3061 Subsequence 尺取法,一个屌屌的O(n)算法
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9050 Accepted: 3604 Descr ...
- poj3061 Subsequence(尺取法)
https://vjudge.net/problem/POJ-3061 尺取发,s和t不断推进的算法.因为每一轮s都推进1所以复杂度为O(n) #include<iostream> #in ...
- poj3061 Subsequence&&poj3320 Jessica's Reading Problem(尺取法)
这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针 ...
- poj3061 Subsequence ,尺取法
A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...
- 尺取法 poj3061 poj3320
尺取法就是反复推进区间的开头和结尾,来求满足条件的最下区间. poj3061 http://poj.org/problem?id=3061 给定一个都是正整数的序列,要我们求总和不小于S的连续子序列的 ...
随机推荐
- HDU 5832A water problem
大数 判断整除 /* *********************************************** Author :guanjun Created Time :2016/8/14 1 ...
- HDU5768Lucky7
Lucky7 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- 00020970-0000-0000-C000-000000000046
00020970-0000-0000-C000-000000000046 System.InvalidCastException: 无法将类型为“Microsoft.Office.Interop.Wo ...
- mysqldump 导出数据表,和数据
目录 导出数据库表与数据 导出数据表数据 导出多个表数据 只导出数据 只导出创建表的数据 导出数据库表与数据 mysqldump -uroot -p caomall>tmp.sql 导出数据表数 ...
- 记录一次Mysql死锁排查过程
背景 以前接触到的数据库死锁,都是批量更新时加锁顺序不一致而导致的死锁,但是上周却遇到了一个很难理解的死锁.借着这个机会又重新学习了一下mysql的死锁知识以及常见的死锁场景.在多方调研以及和同事们的 ...
- Java序列化系列教程(下)
一引言 将 Java 对象序列化为二进制文件的 Java 序列化技术是 Java 系列技术中一个较为重要的技术点,在大部分情况下,开发人员只需要了解被序列化的类需要实现 Serializable 接口 ...
- [BZOJ1331]魔板
Description 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 我们知道魔板的每一个方格都有一种颜色.这8 ...
- 解决:阿里云ECS上启动tomcat后,第一次访问时间特别长
Re在ECS上启动tomcat后,第一次访问时间特别长 2017-04-25 10:16:04 INFO com.world.socket.ServerSocketListener 25- ...
- 【转载】Hadoop分布式文件系统HDFS的工作原理详述
转载请注明来自36大数据(36dsj.com):36大数据 » Hadoop分布式文件系统HDFS的工作原理详述 转注:读了这篇文章以后,觉得内容比较易懂,所以分享过来支持一下. Hadoop分布式文 ...
- [转]微信开发.Net 接入示例
本文转自:http://my.oschina.net/lcak/blog/219618 微信公众平台接口开发官方仅提供了 PHP 接入示例代码, 网上找到的.Net代码多半需要积分下载, 所以自己写了 ...