POJ_3061_Subsequence_(尺取法)
描述
http://poj.org/problem?id=3061
给定长度为n的数列整数以及整数S.求出总和不小于S的连续子序列的长度的最小值,如果解不存在输出0.
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11604 | Accepted: 4844 |
Description
Input
Output
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
Source
分析
尺取法.
定义区间左右端点l和r.l从1开始循环到n,r向后移动,直到区间和>=s,此时为左端点为l时的最短长度.对于左端点为l+1的情况,使得区间和>s的右端点一定>=r,就让r右移直到满足条件.如果r=n仍无法满足,那对于之后的l都无法满足,即可break.此题用二分O(nlogn),用尺取法O(n).
p.s.
1.此题还可用二分做,复杂度为O(nlogn).
#include<cstdio>
#include<algorithm>
using std :: min; const int maxn=;
int q,n,s;
int a[maxn]; void solve(int n,int s)
{
int ans=n+;
int r=,sum=;
for(int l=;l<=n;l++)
{
while(r<=n&&sum<s)
{
sum+=a[r++];
}
if(sum<s) break;
ans=min(ans,r-l);
sum-=a[l];
}
if(ans>n) ans=;
printf("%d\n",ans);
} void init()
{
scanf("%d",&q);
while(q--)
{
scanf("%d%d",&n,&s);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
solve(n,s);
}
} int main()
{
freopen("Subsequence.in","r",stdin);
freopen("Subsequence.out","w",stdout);
init();
fclose(stdin);
fclose(stdout);
return ;
}
POJ_3061_Subsequence_(尺取法)的更多相关文章
- 5806 NanoApe Loves Sequence Ⅱ(尺取法)
传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K ...
- POJ3061 尺取法
题目大意:从给定序列里找出区间和大于等于S的最小区间的长度. 前阵子在zzuli OJ上见过类似的题,还好当时补题了.尺取法O(n) 的复杂度过掉的.尺取法:从头遍历,如果不满足条件,则将尺子尾 部增 ...
- POJ 2739 Sum of Consecutive Prime Numbers(尺取法)
题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Description S ...
- CF 701C They Are Everywhere(尺取法)
题目链接: 传送门 They Are Everywhere time limit per test:2 second memory limit per test:256 megabytes D ...
- nyoj133_子序列_离散化_尺取法
子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 给定一个序列,请你求出该序列的一个连续的子序列,使原串中出现的所有元素皆在该子序列中出现过至少1次. 如2 8 ...
- Codeforces 676C Vasya and String(尺取法)
题目大概说给一个由a和b组成的字符串,最多能改变其中的k个字符,问通过改变能得到的最长连续且相同的字符串是多长. 用尺取法,改变成a和改变成b分别做一次:双指针i和j,j不停++,然后如果遇到需要改变 ...
- POJ 3061 (二分+前缀和or尺取法)
题目链接: http://poj.org/problem?id=3061 题目大意:找到最短的序列长度,使得序列元素和大于S. 解题思路: 两种思路. 一种是二分+前缀和.复杂度O(nlogn).有点 ...
- POJ 3320 尺取法,Hash,map标记
1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识 ...
- HDU 5358 尺取法+枚举
题意:给一个数列,按如下公式求和. 分析:场上做的时候,傻傻以为是线段树,也没想出题者为啥出log2,就是S(i,j) 的二进制表示的位数.只能说我做题依旧太死板,让求和就按规矩求和,多考虑一下就能发 ...
随机推荐
- [.Net MVC] 使用 log4net 日志框架
项目:后台管理平台 意义:项目开发中提出增加日志功能,对关键的操作.程序运行中的错误信息进行记录,这对程序部署后的调试有很大意义. 注:本文只是对网上搜集的信息进行了整合,以备今后查询. 关键字:.N ...
- [PR & ML 5] [Introduction] Decision Theory
- 在Linux系统下安装大于mysql5.5版本的数据库
linux下mysql 5.5的安装方法: 1.安装所需要系统库相关库文件 gcc等开发包,在安装linux系统的时候安装. 2.创建mysql安装目录 # mkdir -p /usr/lo ...
- 在JQuery和Js中,如何让ajax执行完后再继续往下执行 async
async (默认: true) 默认设置下,所有请求均为异步请求.如果需要发送同步请求,请将此选项设置为 false.注意,同步请求将锁住浏览器,用户其它操作必须等待请求完成才可以执行. var t ...
- top每个参数的意义
上图来源于本人虚拟机,作为每个参数解释的参考 10:32:20:现在的时间 up 1 min : 服务器运行的时长 1 user:现在只有1个用户登录 load average: 0.74, 0.25 ...
- 当在Win8下安装msi类型的文件出现errorcode 2503的解决方法
Win8安装程序出现2502.2503错误解决方法 在Win8中,在安装msi安装包的时候常常会出现代码为2502.2503的错误.其实这种错误是由于安装权限不足造成的,可以这种msi的安装包不像其他 ...
- pycharm3.x 注册码
PyCharm 3.0 注册码 PyCharm3 序列号 License Key 用户名:yueting3527 注册码: ===== LICENSE BEGIN ===== 93347-120420 ...
- 把Mvc4项目部署到虚拟目录之后找不到control想到的文件路径规范的问题
最近部署的项目的时候由于端口不够用,想到了把Mvc项目部署到虚拟目录中,结果发现图片,js设置control都找不到了.项目是mvc4+easyui开发的,大量的代码都是在js中调用control,写 ...
- repo manifest xml 文件修改后提交命令
git push origin dev(本地分支):refs/for/tv/Internal_Jb910_develop_t
- 设置BootStrap导航条的高度
只要加上这段css就可以覆盖Bootstrap.css的代码,定制符合自己的样式 .navbar { min-height: 40px; } .nav > li > a { padding ...