题目链接:http://poj.org/problem?id=3061

题意:给n个正整数和一个数S,求出总和不小于S的连续子序列的长度的最小值,如果无解输出0;

我们可以用sum[i]表示前i项的和;然后二分枚举找答案即可时间复杂度为O(n*logn)的;

#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<math.h>
using namespace std;
#define N 120000
#define PI 4*atan(1.0)
#define mod 110119
#define met(a, b) memset(a, b, sizeof(a))
typedef long long LL; int a[N], n, s, sum[N]; int Judge(int len)
{
for(int i=; i+len-<=n; i++)
{
if(sum[i+len-]-sum[i-]>=s)
return ;
}
return ;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d %d", &n, &s); for(int i=; i<=n; i++)
{
scanf("%d", &a[i]);
sum[i] = sum[i-] + a[i];
} int L = , R = n, Min = ; while(L <= R)
{
int Mid = (L+R)/;
if(Judge(Mid))
{
Min = Mid;
R = Mid-;
}
else
L = Mid+;
}
printf("%d\n", Min);
}
return ;
}

还可以用尺取法,就是用两个指针控制一下头和尾,然后移动即可;时间复杂度是O(n)的;

#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<math.h>
using namespace std;
#define N 120000
#define PI 4*atan(1.0)
#define mod 110119
#define met(a, b) memset(a, b, sizeof(a))
typedef long long LL; int a[N], n, s; int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d %d", &n, &s); for(int i=; i<=n; i++)
scanf("%d", &a[i]); int L = , R = , ans = n+, sum = ; while()
{
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);
}
return ;
}

Subsequence---poj3061(尺取法||二分)的更多相关文章

  1. poj3061 Subsequence(尺取法)

    https://vjudge.net/problem/POJ-3061 尺取发,s和t不断推进的算法.因为每一轮s都推进1所以复杂度为O(n) #include<iostream> #in ...

  2. poj3061 Subsequence ,尺取法

    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...

  3. POJ3061 尺取法

    题目大意:从给定序列里找出区间和大于等于S的最小区间的长度. 前阵子在zzuli OJ上见过类似的题,还好当时补题了.尺取法O(n) 的复杂度过掉的.尺取法:从头遍历,如果不满足条件,则将尺子尾 部增 ...

  4. poj3061尺取法

    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...

  5. POJ 3061 Subsequence ( 尺取法)

    题目链接 Description A sequence of N positive integers (10 < N < 100 000), each of them less than ...

  6. poj 3061 题解(尺取法|二分

    题意 $ T $ 组数据,每组数据给一个长度 $ N $ 的序列,要求一段连续的子序列的和大于 $ S $,问子序列最小长度为多少. 输入样例 2 10 15 5 1 3 5 10 7 4 9 2 8 ...

  7. Codeforces 1156C 尺取法 / 二分

    题意:给你一个数组,问里面最多能匹配出多少对,满足abs(a[i] - a[j]) >= k; 思路:首先肯定要排序. 思路1(尺取法):看了dreamoon的代码明白的.我们可以寻找一个最长的 ...

  8. 【UVALive】2678 Subsequence(尺取法)

    题目 传送门:QWQ 分析 一开始没看到都是正整数根本不会做...... 看到了就是水题了.(但还是sb WA了一发) 尺取法搞一搞 代码 #include <bits/stdc++.h> ...

  9. poj3061 Subsequence【尺取法】

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

随机推荐

  1. Explaining Delegates in C# - Part 4 (Asynchronous Callback - Way 1)

    So far, I have discussed about Callback, Multicast delegates, Events using delegates, and yet anothe ...

  2. osgExp只能将3dmax中的动画导出为路径动画osg::AnimationPath,而不能导出osgAnimation::Animation。osg播放骨骼动画应该使用FBX格式

    通过实际的模型测试,导出为.osg文本格式,搜索animation,只能搜索到AnimationPathCallback,而搜索不到osgAnimation相关类 在OSGExp1.5.0源代码中搜索 ...

  3. zabbix监控第一台服务器

    客户机的IP是192.168.0.80,主机名是wls12c 1. 安装客户端, 1.1 新建zabbix的用户 groupadd zabbix useradd -g zabbix zabbix 1. ...

  4. 音频——H5 audio

    分享站又有新功能了:将文件站上的语音文件正确播放出来.效果图: 暂停: 播放: 实现的效果:类似于音乐播放器一般,但是较之更简单一些,可以正常播放语音,有拖动.快进后退效果便可. 思路: 首先想到的便 ...

  5. 使用taro开发钉钉的E应用报错 You are currently using minified code outside of NODE_ENV === "production". This means that you are running a slower development build of Redux. You can use loose-envify (https://git

    今天测试taro转钉钉E应用的时候,在模拟器上没事,但是在真机上却报错了: You are currently using minified code outside of NODE_ENV === ...

  6. 【Java并发编程五】信号量

    一.概述 技术信号量用来控制能够同时访问某特定资源的活动的数量,或者同时执行某一给定操作的数据.计数信号量可以用来实现资源池或者给一个容器限定边界. 信号量维护了一个许可集,许可的初始量通过构造函数传 ...

  7. Esper学习之七:EPL语法(三)

    1.Aggregation 和SQL一样,EPL也有Aggregation,即聚合函数.语法如下: aggregate_function([all|distinct] expression) aggr ...

  8. 【基础】httpclient注意事项

    一.HttpClient有默认的执行器RetryExec,其默认的重试策略是DefaultHttpRequestRetryHandler. RetryExec在执行http请求的时候使用的是底层的基础 ...

  9. xmlWriter

    MemoryStream msXml = new MemoryStream(); XmlTextWriter xmlWriter = new XmlTextWriter(msXml, Encoding ...

  10. Window 命令行神器:cmder

    http://cmder.net/ https://github.com/cmderdev/cmder/releases/   官网下载地址 http://www.360doc.com/content ...