POJ3061——Subsequence(尺取法)
Subsequence
给定长度为n的数列整数a0,a1,a2…an-1以及整数S。求出总和不小于S的连续子序列的长度的最小值,如果解不存在输出0。
反复推进区间的开头和末尾,来求取满足条件的最小区间的方法称为取尺法。
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
// #define _ ios::sync_with_stdio(false)
// #define cin.tie(0)
using namespace std;
// #define rep(i,x,y) for(int i=x;i<y;i++)
typedef long long ll;
const int MAXN=2e5+5;
int main()
{
int t,n,s;
int a[MAXN];
cin>>t;
while(t--)
{
cin>>n>>s;
for(int i=0;i<n;i++)
cin>>a[i];
int res=n+1;
for(;;)
{
while(t<n&&sum<s)
sum+=a[t++];
if(sum<s)
break;
res=min(res,t-i);
sum-=a[i++];
}
if(res>n)
res=0;
cout<<res<<endl;
}
return 0;
}
POJ3061——Subsequence(尺取法)的更多相关文章
- 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的连续子序列的 ...
随机推荐
- 日志导致jvm内存溢出相关问题
生产环境日志级别为info,请看如下这行代码: LOGGER.debug("the DTO info: {}", JSON.toJSONString(DTO)); 这段代码主要有两 ...
- sqlplus登录用户被锁问题
oracle有三个默认的用户名和密码: 1.用户名:sys密码:change_on_install 2.用户名:system密码:manager 3.用户名:scott密码:tiger 当登录用户 ...
- LoadableComponent类的使用
通过继承LoadableComponent类,测试程序可以判断浏览器是否加载了正确的页面,只需要重写isLoaded和load二个方法,此方法有助于页面对象的页面访问操作更加稳定 1.LoginPag ...
- Vue CLI安装报错 npm ERR! Exit handler never called!
安装Vue CLI时报错: npm install –g vue-cli 试了四种办法 1.把全局安装-g放到后面 npm install @vue/cli –g 2.命令行输入 npm 缓存清理命令 ...
- 入门Kubernetes-StatefulSets
前言: 前面文中对通过DaemonSet.存储资源对象,实现了在指定节点中运行一个守护进程. 在真实的业务场景中,部署的服务都是有状态的.且有数据需要持久化的:那么如何实现呢? 那么接下来学习一种更加 ...
- SpringBoot集成Druid
maven <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-b ...
- C# 异步锁
参考网址: https://www.cnblogs.com/Alicia-meng/p/13330640.html 使用SemaphoreSlim 实现 当多个任务或线程并行运行时,难以避免的对某些有 ...
- WPF设计自定义控件
在实际工作中,WPF提供的控件并不能完全满足不同的设计需求.这时,需要我们设计自定义控件. 这里LZ总结一些自己的思路,特性如下: Coupling UITemplate Behaviour Func ...
- msql中@RequestParam、@Param、@PathVariable的用法
@RequestParam的用法 1.可以对传入参数指定参数名,将请求参数绑定至方法参数 // 下面的对传入参数指定为aa,如果前端不传aa参数名,会报错 @RequestParam(value=&q ...
- git 拉取代码指定分支
问题背景: 新项目还在开发阶段,没有正式对外发布,所以开发同事合并代码到develop上(或者其他名称分支上),而不是到master分支上 通过git拉取代码的时候,默认拉取的是master分支,如下 ...