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的连续子序列的 ...
随机推荐
- SICP习题练习
练习1.6 new-if的三个参数会先被执行,这样就会无限循环下去 练习1.7 (define (sqrt-iter last-guess guess x) (if (good-enough? las ...
- phpstorm配置php脚本执行
1.到设置中配置 2.配置具体项 3.完了就可以执行php脚本了
- scapy基础-网络数据包结构
网络层次模型,数据包的组成是学习scapy的基础,下文主要关注模型中各个层次的用途,ethernet II和ip包数据结构. 1.五层模型简介 名称 作用 包含协议 应用层 面向程序对程序的传输 ...
- maven pom 详细配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- Objective-C 声明属性
创建: 2018/01/24 完成: 2018/01/25 遗留: TODO 声明属性(declared property) 属性的声明与功能 属性的声明 @property 读写 @proper ...
- eccharts-gl 3D立体柱状图
echarts-gl继承于echarts echarts-gl官方实例https://echarts.baidu.com/examples/index.html#chart-type-globe 代码 ...
- vim 跳行查看日志
VIM 跳跃行号 一.显示行号 在命令模式下输入 :set nu 或者 :set number 即可显示行号 二.跳跃行号 在编辑模式下输入 ngg 或者 nG [n为指定的行数(如25)] ...
- 计算误差——ACM计算几何中的精度问题
浮点数为何会有精度问题 占字节数 数值范围 十进制精度位数 float 4 -3.4e-38~3.4e38 6~7 double 8 -1.7e-308~1.7e308 14~15 如果内存不是很 ...
- K Seq HihoCoder - 1046 || BZOJ4504 k个串
这题与超级钢琴类似,然而重复的不重复计算贡献.. 那么先求出数组nxt,nxt[i]表示第i个元素之后的第一个与其相等的元素的下标,不存在则nxt[i]=0 考虑取的区间左端点为1时的情况. 将读入序 ...
- RabbitMQ二:AMQP协议
参考这个:http://kb.cnblogs.com/page/73759/ 参考这个:http://www.cnblogs.com/charlesblc/p/6286875.html 写的挺好 Ra ...