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

题目大意:

给定长度为n的整列整数a[0],a[1],……a[n-1],以及整数S,求出总和不小于S的连续子序列的长度的最小值。

思路:

方法一:

首先求出各项的和sum[i],这样可以在O(1)的时间内算出区间上的总和,这样,枚举每一个起点i,然后二分搜索出结果大于sum[i]+tot的最小下标。(tot是题目中的S)

总的时间为O(nlogn)

方法二:

设以a[s]开始的总和最初大于S时的连续子序列为a[s]+a[s+1]+……a[t-1],这时,a[s+1]+a[s+1]+……a[t-2]<a[s]+a[s+1]+……a[t-2]<S,所以如果从a[s+1]开始总和最初超过S的连续子序列是a[s+1]+……a[t‘-1],则t<=t'。

故可以设计如下算法:

1.初始s=t=sum=0

2.只要依然有sum<S,就不断将sum加上a[t],并且t=t+1;

3.如果2中无法满足sum>=s则终止,否则ans=min(ans,t-s);

4.将sum减去a[s],s+=1后回到2

总复杂度为O(N)

1.二分法

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=100000+10;
const int INF=0x7fffffff;
int a[MAXN],sum[MAXN];
int search(int L,int R,int target) //(L,R]
{
while(L<R-1)
{
int m=(L+R)>>1;
if(sum[m]<target)
L=m;
else
R=m;
}
return R;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,tot;
scanf("%d%d",&n,&tot);
sum[0]=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum[i]=sum[i-1]+a[i];
}
int ans=INF;
for(int i=1;i<=n&&sum[i]+tot<=sum[n];i++)
{
int t=search(i,n,tot+sum[i]);//sum[t]-sum[i]>=tot -> sum[t]>=tot+sum[i]
ans=min(ans,t-i);
}
if(ans==INF)
printf("0\n");
else
printf("%d\n",ans);
}
return 0;
}

2.尺取法

#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=100000+10;
const int INF=0x7fffffff;
int a[MAXN]; int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,tot;
scanf("%d%d",&n,&tot);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int s=0,t=0,sum=0,ans=INF;
while(true)
{
while(sum<tot && t<n)
{
sum+=a[t++];
}
if(sum<tot) break;
ans=min(ans,t-s);
sum-=a[s++];
}
if(ans==INF)
printf("0\n");
else
printf("%d\n",ans);
}
return 0;
}

POJ 3061 Subsequence 二分或者尺取法的更多相关文章

  1. POJ 3061 Subsequence ( 二分 || 尺取法 )

    题意 : 找出给定序列长度最小的子序列,子序列的和要求满足大于或者等于 S,如果存在则输出最小长度.否则输出 0(序列的元素都是大于 0 小于10000) 分析 : 有关子序列和的问题,都可以考虑采用 ...

  2. poj 3061 Subsequence 二分 前缀和 双指针

    地址 http://poj.org/problem?id=3061 解法1 使用双指针 由于序列是连续正数 使用l r 表示选择的子序列的起始 每当和小于要求的时候 我们向右侧扩展 增大序列和 每当和 ...

  3. Poj 3061 Subsequence(二分+前缀和)

    Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12333 Accepted: 5178 Descript ...

  4. POJ 3061 Subsequence 二分查找

    题目大意:给出长度为n的一个序列,给出一个数字S,求长度最短的序列和大于等于S的连续子序列,输出该长度,如果没有答案输出0. 题目思路:看数据范围,这道题就是卡时间的.我们可以用sum[i]记录前i项 ...

  5. Atcoder Beginner Contest 155D(二分,尺取法,细节模拟)

    二分,尺取法,细节模拟,尤其是要注意a[i]被计算到和a[i]成对的a[j]里时 #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> ...

  6. 题解报告:poj 3061 Subsequence(前缀+二分or尺取法)

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

  7. POJ 3061 Subsequence【二分答案】||【尺取法】

    <题目链接> 题目大意: 给你一段长度为n的整数序列,并且给出一个整数S,问你这段序列中区间之和大于等于S的最短区间长度是多少. 解题分析:本题可以用二分答案做,先求出前缀和,然后枚举区间 ...

  8. poj 3061(二分 or 尺取法)

    传送门:Problem 3061 https://www.cnblogs.com/violet-acmer/p/9793209.html 马上就要去上课了,先献上二分AC代码,其余的有空再补 题意: ...

  9. POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13955   Accepted: 5896 Desc ...

随机推荐

  1. iterator的使用和封个问题

    这篇文章的内容还是不错的: http://www.cnblogs.com/zhuyf87/archive/2012/12/08/2808290.html for (vector<int>: ...

  2. 如何覆盖GCE的DHCP MTU选项

     如何覆盖GCE的DHCP MTU选项 在GCE上托管的Linux IPSec隧道不能打开谷歌,这与MTU有关.谷歌管理员认为"改变这个值是一件困难的事情"https://cl ...

  3. C#结构函数

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. 调用google翻译

    1. [代码]maven依赖     ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <dependency>     <groupId>org.a ...

  5. Python print 语句(Python 2 与 Python 3)

    1. python 3.x 中的 print print 在 Python 3.x 的环境里是内置函数(built-in function): python 3 的 print 语句支持(python ...

  6. SQL Server performance for alter table alter column change data type

    最近在搞一个升级脚本,发现有一张业务表中的某些字段长度需要调整,直接使用alter table alter column进行修改发现修改一列要用十几分钟!!!两三个列那用时简直不能容忍啊!google ...

  7. IFC数据模式架构的四个概念层

    IFC模型体系结构由四个层次构成, 从下到上依次是 资源层(Resource Layer).核心层(Core Layer).交互层(Interoperability Layer).领域层(Domain ...

  8. CMDB学习之二数据采集

    首先也要调用插件的方式,来写采集数据插件,在src目录下创建一个插件 plugins ,然后在plugins下创建disk.py ,memory.py, network.py等等 src plugin ...

  9. dp水题

    hdu 2084: #include <stdio.h> #include <iostream> #include <string.h> using namespa ...

  10. 小米开源文件管理器MiCodeFileExplorer-源码研究(1)-2个模型Model

    上篇说到,把小米的Java代码整理成了5个包,其中1个是net.micode.fileexplorer.model.这个包就2个模型类,最基本了,FileInfo和FavoriteItem. pack ...