Poj3061Subsequence
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;
题解: 尺取法:
这是他的执行过程。
先找到一个大于s的序列,然后对这个序列处理,直到小于s,然后在加入后面的数字,不断的循环。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[100000+10];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
long long sum=0;
int st=0,en=0;
int ans=0x3f3f3f3f;
while(1)
{
while(en<n&&sum<m)
sum+=a[en++];
if(sum<m)
break;
ans=min(ans,en-st);
sum-=a[st++];
}
if(ans==0x3f3f3f3f) ans=0;
printf("%d\n",ans);
}
return 0;
}
Poj3061Subsequence的更多相关文章
- poj--3061--Subsequence(贪心)
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10882 Accepted: 4498 Desc ...
- 【尺取法】POJ3061 & POJ3320
POJ3061-Subsequence [题目大意] 给定长度微n的数列整数及整数s.求出总和不小于s的连续子序列的长度的最小值.如果节不存在,则输出0. [思路] 尺取法五分钟裸裸裸~刷水刷出了罪恶 ...
随机推荐
- nodejs 实践:express 最佳实践系列
nodejs 实践:express 最佳实践系列 nodejs 实践:express 最佳实践(一) 项目结构 nodejs 实践:express 最佳实践(二) 中间件 nodejs 实践:expr ...
- JavaSE之Java基础(5)
21.简述正则表达式及其用途. 在编写处理字符串的程序时,经常会有查找符合某些复杂规则的字符串的需要.正则表达式就是用于描述这些规则的工具.换句话说,正则表达式就是记录文本规则的代码. 22.Java ...
- 面向对象设计与构造:oo课程总结
面向对象设计与构造:OO课程总结 第一部分:UML单元架构设计 第一次作业 UML图 MyUmlInteraction类实现接口方法,ClassUnit和InterfaceUnit管理UML图中的类和 ...
- Java并发(六):并发策略
通过多次优化实例来了解选择并发策略的正确姿势 通过模拟浏览器程序的渲染页面(Page-Rendering)功能,为了方便,假设HTML页面只会包含标签文本和图片以及URL; 第一个版本:串行加载页面元 ...
- JSON.parse() 和 JSON.stringify()的简单介绍
参考地址: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse ht ...
- OpenFirewall
1.写一份json文件:将要添加防火墙例外的应用程序和端口写入到json文件中 2.打开防火墙,读取json文件添加例外 /// <summary> /// Firewall.xaml 的 ...
- Grafana 安装使用
Grafana 安装使用 官方网址:https://grafana.com/ 官方文档:http://docs.grafana.org/ 安装 grafana 基于 RPM 的系统(CentOS,Fe ...
- lintcode中等难度5道题
1.整数转罗马数字 对任一个罗马数字可以 由12个罗马字符进行加法操作完成,且大数在左,小数在右,可以将一个数字进行拆分来求解 2.买卖股票的最佳时机 II 可将问题转换为只要相连的两天prices[ ...
- 正则表达式---01 js篇
本文主要针对js中正则表达式的实践操作,来让大家对正则表达式有一个入门清晰的了解. 正则表达式推荐学习网址:http://www.runoob.com/regexp/regexp-tutorial.h ...
- JavaWeb中五种转发方式(转)
今天本来是想找一下在jsp中实现转发的方式的,无意中看到了一篇文章,然后稍微综合了把服务器端的转发也包括在内. 1. RequestDispatcher.forward() 是在服务器端起作用,当 ...