题目:http://poj.org/problem?id=3061

尺取法解题

import java.util.Scanner;;

public class Main {

    public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
int test=reader.nextInt();
while(test>0){ int N=reader.nextInt();
int S=reader.nextInt();
long array[]=new long[N];
for(int i=0;i<N;i++){
array[i]=reader.nextLong();
}
long sum=0;
int start=0;
int end=0;
long step=0;
long result=99999999999999L; while(true){
while(end<N && sum<S){
sum=sum+array[end];
end++;
}
if(sum<S){
break;
}
step=end-start;
result=Math.min(result, step);
sum=sum-array[start];
start++;
}
if(result==99999999999999L){
System.out.println("0");
}else{
System.out.println(result);
}
test--;
}
} }

20:19:15

2019-02-26

Subsequence——POJ3061的更多相关文章

  1. poj3061 Subsequence(尺取法)

    https://vjudge.net/problem/POJ-3061 尺取发,s和t不断推进的算法.因为每一轮s都推进1所以复杂度为O(n) #include<iostream> #in ...

  2. poj3061 Subsequence(尺取)

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

  3. POJ3061 Subsequence 尺取or二分

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

  4. poj3061 Subsequence&&poj3320 Jessica's Reading Problem(尺取法)

    这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针 ...

  5. POJ3061 Subsequence

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16520   Accepted: 7008 Desc ...

  6. poj3061 Subsequence【尺取法】

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

  7. poj3061 Subsequence ,尺取法

    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...

  8. POJ3061——Subsequence(尺取法)

    Subsequence POJ - 3061 给定长度为n的数列整数a0,a1,a2-an-1以及整数S.求出总和不小于S的连续子序列的长度的最小值,如果解不存在输出0. 反复推进区间的开头和末尾,来 ...

  9. [POJ3061]Subsequence(二分,前缀和)

    题目链接:http://poj.org/problem?id=3061 题意:给一个长为n的数列和整数s,求一个连续的子序列,使得这个子序列长度最短并且不小于这个整数s. 统计[1~i]的子序列和su ...

随机推荐

  1. 广义线性模型(Generalized Linear Models)

    在线性回归问题中,我们假设,而在分类问题中,我们假设,它们都是广义线性模型的例子,而广义线性模型就是把自变量的线性预测函数当作因变量的估计值.很多模型都是基于广义线性模型的,例如,传统的线性回归模型, ...

  2. delphi 浮点 精度

    double 没有问题, Single有问题 '0.7' 0.69999999999999996 Single; // 4 byte real Double; // 8 byte real

  3. c++builder自定义控件

    c++builder自定义控件 http://docwiki.embarcadero.com/CodeExamples/XE8/en/RegisterComponents_%28C%2B%2B%29 ...

  4. 关于Web项目出现懒加载异常的解决方案

    manytomany关系中,使用 fetch = FetchType.LAZY 来做懒加载,加快些性能.但是却一直出错,原因是session被关闭,要保持session,需要事务. Hibernate ...

  5. Balls(poj 3783)

    The classic Two Glass Balls brain-teaser is often posed as: “Given two identical glass spheres, you ...

  6. objective-C: NSString应该用initWithFormat? 还是 stringWithFormat?

    今天在看书上的一段代码时,发现NSString实例化时,有时用的是initWithFormat方法,有时用的是stringWithFormat,到底应该如何选择呢? 区别: 1.initWithFor ...

  7. 【冷门】 C# 小技巧之获取变量名称

    今天在自我规范程序设计的时候,变量名匹配字符串来自配置文件,网上找了一会儿发现也有朋友在找寻这种方式,很不容易找到一个解决方案来自http://www.th7.cn/Program/net/20140 ...

  8. centos7 安装docker-ce ,最新版本docker,docker阿里云加速

    直接用yum install docker -y安装的docker版本为1.12,但是docker发展很快,现在都17.06.2了.docker-ce是指docker的社区版 卸载老版本的 docke ...

  9. AddComponentMenu

    [AddComponentMenu] The AddComponentMenu attribute allows you to place a script anywhere in the " ...

  10. Kafka核心概念(转)

    转自:https://blog.csdn.net/liyiming2017/article/details/82805479 1.Kafka集群结构 实际上kafka的结构图是有些区别的,现在我们看下 ...