Subsequence——POJ3061
题目: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的更多相关文章
- poj3061 Subsequence(尺取法)
https://vjudge.net/problem/POJ-3061 尺取发,s和t不断推进的算法.因为每一轮s都推进1所以复杂度为O(n) #include<iostream> #in ...
- poj3061 Subsequence(尺取)
Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...
- POJ3061 Subsequence 尺取or二分
Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...
- poj3061 Subsequence&&poj3320 Jessica's Reading Problem(尺取法)
这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针 ...
- POJ3061 Subsequence
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16520 Accepted: 7008 Desc ...
- poj3061 Subsequence【尺取法】
Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...
- poj3061 Subsequence ,尺取法
A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...
- POJ3061——Subsequence(尺取法)
Subsequence POJ - 3061 给定长度为n的数列整数a0,a1,a2-an-1以及整数S.求出总和不小于S的连续子序列的长度的最小值,如果解不存在输出0. 反复推进区间的开头和末尾,来 ...
- [POJ3061]Subsequence(二分,前缀和)
题目链接:http://poj.org/problem?id=3061 题意:给一个长为n的数列和整数s,求一个连续的子序列,使得这个子序列长度最短并且不小于这个整数s. 统计[1~i]的子序列和su ...
随机推荐
- react.js的了解
React很大的特点就是“轻”,再加上VDOM这个很好的idea让React非常非常快(在上面那个测试里面0.3s左右就载入完毕).另外React和Angular一个很大的不同就是React采用的是o ...
- ceph journal更换位置
只在这里做简单的演示 ceotos7 环境 3个mon节点 3个osd节点 环境搭建我这里不再叙述 我们查看一下分区情况: [root@ceph_1 ~]# lsblkNAME MAJ: ...
- Python的urllib和urllib2模块
Python的urllib和urllib2模块都做与请求URL相关的操作,但他们提供不同的功能.他们两个最显着的差异如下: urllib2可以接受一个Request对象,并以此可以来设置一个URL的h ...
- linux find中的-print0和xargs中-0的奥妙
默认情况下, find 每输出一个文件名, 后面都会接着输出一个换行符 ('n'), 因此我们看到的 find 的输出都是一行一行的: 比如我想把所有的 .log 文件删掉, 可以这样配合 xargs ...
- Nginx 安装与使用
作者:吴秦出处:http://www.cnblogs.com/skynet/ 前言 nginx Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一 ...
- sublime 安装插件
安装Package Control 在安装插件之前,需要让sublime安装Package Control.打开Sublime Text的控制台,快捷键ctrl + ~,在控制台中输入以下代码. im ...
- jar 运行报错:找不到或无法加载主类
NIFEST.MF文件中指定的,如下所示:Manifest-Version: 1.0Class-Path: .Main-Class: com.webex.app.Main // ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 16—Recommender Systems 推荐系统
Lecture 16 Recommender Systems 推荐系统 16.1 问题形式化 Problem Formulation 在机器学习领域,对于一些问题存在一些算法, 能试图自动地替你学习到 ...
- shell编程——变量的数值计算
在shell脚本中,有时候会需要对数值类型的变量进行计算,通常我们用的是(()) [root@localhost collect]# ((a=1+2)) [root@localhost collect ...
- 安装mongodb时报错 configure: error: Cannot find OpenSSL's libraries
请安装这些包以便继续: apt-get install build-essential libexpat1-dev libgeoip-dev libpng-dev libpcre3-dev libss ...