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. [思路] 尺取法五分钟裸裸裸~刷水刷出了罪恶 ...
随机推荐
- ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第十一天(非原创)
文章大纲 一.课程介绍二.订单系统介绍三.项目源码与资料下载四.参考文章 一.课程介绍 一共14天课程(1)第一天:电商行业的背景.淘淘商城的介绍.搭建项目工程.Svn的使用.(2)第二天:框架的整合 ...
- idea 清屏(控制台)快捷键
eclipse清屏快捷键为鼠标右键+R 而在idea中默认并没有清屏console的快捷键 所以需要我们自行设置: 1,ctrl+alt+s打开settings 2,找到keymap 3,搜索 cle ...
- linux服务器安装nodejs运行环境
安装nodejs运行环境 第一步:到node官网下载相应版本的安装包,将安装包放置服务器上,路径为 usr/local/node(可根据自身情况进行修改) 第二步:解压 ***.tar.xz格式文件需 ...
- 前端Json数据模拟神器mockJs使用教程
一般项目做法: <html> <head> <script src="http://requirejs.org/docs/release/2.1.16/comm ...
- 零基础逆向工程25_C++_02_类的成员权限_虚函数_模板
1 类的成员权限 1.1 小结: 1.对外提供的函数或者变量,发布成public的 但不能随意改动. 2.可能会变动的函数或者变量,定义成private的 这样编译器会在使用的时候做检测. 3.只有结 ...
- Android stutdio2.2 启动模拟器出现“/dev/kvm is not found.”解决方法
第一次启动avd,Android stutdio会自动安装Intel HAXM,而且表面看是成功的,再次启动会出现“/dev/kvm is not found.”,这说明Intel HAXM没有安装成 ...
- PHP函数:mysql_fetch_assoc指针重置
本文目前主要讨论mysql_fetch_assoc“指针”如何重置的问题 要了解mysql_fetch_assoc,先看看它与mysql_fetch_row和mysql_fetch_array的关系. ...
- linux下 ps命令日常
要对系统中进程进行监测控制,查看状态,内存,CPU的使用情况,使用命令:/bin/ps (1) ps :是显示瞬间进程的状态,并不动态连续: (2) top:如果想对进程运行时间监控,应该用 top ...
- 使用kubeadm搭建Kubernetes集群
记录在石墨日记中,经常更新,懒得再复制了,直接点击下面链接查看吧 移步到此: https://shimo.im/docs/22WbxxQa1WUV9wsN/
- mybatis-动态sql2
mybatis的动态sql中常用的有 if where foreach set 项目沿用之前的. 1.dao层添加接口: package com.java1234.map ...