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的连续子序列的 ...
随机推荐
- .NET 6 预览版 7:新功能已完成 ,将专注于改进
.NET 团队的项目经理 Richard Lander在宣布 .NET 6 Preview 7 时说:"这是 .NET 预览的又一季的结束.", 中文翻译:.NET 6 预览版 7 ...
- Spring Cloud Aliaba - Ribbon
Ribbon(有关介绍见RestTemplate末尾) Ribbon负载均衡实现策略 Ribbon负载均衡实现策略通过接口IRule进行实现,默认使用ZoneAvoidanceRule规则进行负载均衡 ...
- 用Ultraedit调试tk程序
tk没有自己的编译环境,Ultraedit中仅3步就可以打造一个tk调试环境: 1. 为UE添加tcl/tk语法高亮支持:从UE的网站上下载tcl/tk的wordfile: 2. 添加编译命令到菜单中 ...
- CF上部分树形DP练习题
本次 5 道题均来自Codeforce 关于树形DP的算法讲解:Here 791D. Bear and Tree Jumps 如果小熊每次能跳跃的距离为1,那么问题变为求树上任意两点之间距离之和. 对 ...
- CF1264D2 Beautiful Bracket Sequence
我们枚举每两个字符的空档,统计一个空档左边有 \(l\) 个左括号, 右边有 \(r\) 个右括号,左边有 \(u\) 个问号,右边有 \(v\) 个问号. 则对于 \(p\) 的答案 \(ans_p ...
- mysql几种连接方式区别
mysql的几种join 2017年03月19日 14:49:07 carl-zhao 阅读数:7845 标签: mysqlsqljoin 更多 个人分类: MySQL 版权声明:本文为博主原创文章, ...
- .Net Core NPOI读取Excel 并转为数据实体类
创建应用程序 这里直接创建Console程序 引用NPOI的NuGet包 PM> Install-Package NPOI -Version 2.5.1 直接Nuget包管理器添加 导入Exce ...
- SpringBoot中的静态资源访问
一.说在前面的话 我们之间介绍过SpringBoot自动配置的原理,基本上是如下: xxxxAutoConfiguration:帮我们给容器中自动配置组件: xxxxProperties:配置类来封装 ...
- spring学习日志三
一.回顾 1.1 依赖注入的方式. set方法来注入 <property name="属性名" /> 构造方法来注入<construtor-arg index=& ...
- qt 中的对象树
本节内容讲解了什么是对象树以及其所带来的 GUI 编程好处.最后说明了在对象树中析构顺序问题并举了个特殊的例子,来说明平时编程中需要注意的一个点. 什么是对象树? 我们常常听到 QObject 会用对 ...