题目传送门

 /*
题意:求连续子序列的和不小于s的长度的最小值
尺取法:对数组保存一组下标(起点,终点),使用两端点得到答案
1. 记录前i项的总和,求[i, p)长度的最小值,用二分找到sum[p] - s[i] >= s的p
2. 除了O (nlogn)的方法,还可以在O (n)实现,[i, j)的区间求和,移动两端点,更新最小值,真的像尺取虫在爬:)
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std; typedef long long ll;
const int MAXN = 1e5 + ;
const int INF = 0x3f3f3f3f;
int a[MAXN];
ll sum[MAXN]; int main(void) //POJ 3601 Subsequence
{
int t; scanf ("%d", &t);
while (t--)
{
memset (sum, , sizeof (sum));
int n, s;
scanf ("%d%d", &n, &s);
for (int i=; i<=n; ++i) {scanf ("%d", &a[i]); sum[i] = sum[i-] + a[i];} if (sum[n] < s) {puts (""); continue;} int ans = n;
for (int i=; sum[i]+s<=sum[n]; ++i)
{
int p = lower_bound (sum+i, sum++n, sum[i] + s) - sum;
ans = min (ans, p - i);
} printf ("%d\n", ans);
} return ;
} /*
2
10 15
5 1 3 5 10 7 4 9 2 8
5 11
1 2 3 4 5
*/

 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std; typedef long long ll;
const int MAXN = 1e5 + ;
const int INF = 0x3f3f3f3f;
int a[MAXN]; int main(void) //POJ 3601 Subsequence
{
int t; scanf ("%d", &t);
while (t--)
{
int n, s;
scanf ("%d%d", &n, &s);
for (int i=; i<=n; ++i) scanf ("%d", &a[i]); int ans = n + ; int i = , j = ; ll sum = ;
while ()
{
while (j <= n && sum < s) sum += a[j++];
if (sum < s) break;
ans = min (ans, j - i);
sum -= a[i++];
} if (ans == n + ) puts ("");
else printf ("%d\n", ans);
} return ;
} /*
2
10 15
5 1 3 5 10 7 4 9 2 8
5 11
1 2 3 4 5
*/

O (n)

尺取法 POJ 3601 Subsequence的更多相关文章

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

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

  2. 尺取法 poj 2566

    尺取法:顾名思义就是像尺子一样一段一段去取,保存每次的选取区间的左右端点.然后一直推进 解决问题的思路: 先移动右端点 ,右端点推进的时候一般是加 然后推进左端点,左端点一般是减 poj 2566 题 ...

  3. 尺取法 POJ 3320 Jessica's Reading Problem

    题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...

  4. 尺取法 || POJ 2739 Sum of Consecutive Prime Numbers

    给一个数 写成连续质数的和的形式,能写出多少种 *解法:先筛质数 然后尺取法 **尺取法:固定区间左.右端点为0,如果区间和比目标值大则右移左端点,比目标值小则右移右端点               ...

  5. POJ 3061 Subsequence(尺取法)

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18145   Accepted: 7751 Desc ...

  6. POJ 3061 Subsequence ( 尺取法)

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

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

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

  8. POJ 3061 Subsequence 二分或者尺取法

    http://poj.org/problem?id=3061 题目大意: 给定长度为n的整列整数a[0],a[1],--a[n-1],以及整数S,求出总和不小于S的连续子序列的长度的最小值. 思路: ...

  9. POJ 3061 Subsequence 尺取法

    转自博客:http://blog.chinaunix.net/uid-24922718-id-4848418.html 尺取法就是两个指针表示区间[l,r]的开始与结束 然后根据题目来将端点移动,是一 ...

随机推荐

  1. OpenWrt 安装python-sqlite3失败

    https://dev.openwrt.org/ticket/12239 #12239 reopened defect Sqlite3 missing in python 汇报人: dgspai@- ...

  2. javaEE之------ApectJ的切面技术===标签

    如今比較流行了aop技术之中的一个========标签 实现步骤: 一,导入aop标签 方法,打开aop包.里面就有. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5 ...

  3. lmhostid获取hostid为空问题

    lmhostid获取hostid为空问题 问题描写叙述 今天迁移曾经的一个装有flexlm的虚拟机,结果发如今迁移后启动时报错 ... Wrong hostid on SERVER line for ...

  4. OpenvSwitch代码分析之bridge和port

    ovs-vsctl add-br br0 会在数据库里面加入新bridge的信息ovs-vsctl add-port br0 eth0 会在数据库里面加入新的port信息 void bridge_ru ...

  5. CocoaPoda在iOS开发中的使用

    CocoaPoda在iOS开发中的使用 CocoaPods 简介 CocoaPods是iOS开发中不可避免的依赖管理第三方的工具,能简化一些第三方库文件需要添加编译参数及依赖库的繁复工作 CocoaP ...

  6. C++ 虚函数与纯虚函数 浅析

    [摘要] 在虚函数与纯虚函数的学习中.要求理解虚函数与纯虚函数的定义,了解虚函数与纯虚函数在实例化上的差异.掌握两者在实现上的必要性.熟悉纯虚函数在子类与孙类的函数类型.本文即针对上述问题展开阐述. ...

  7. 2016/05/17 thinkphp3.2.2 ① Ajax 使用 ②前端验证

    显示效果: ①Ajax使用:   注意传值的所有过程用的是小写,及时数据库列的名称中有大写字母 控制器部分: AjaxController.class.php <?php namespace H ...

  8. 哨兵和docker容器

    1,redis哨兵的配置 redis-6379配置文件内容如下 cat redis-6379.conf port 6379 daemonize yes logfile "6379.log&q ...

  9. wpa_supplicant介绍【转】

    本文转载自:https://zhuanlan.zhihu.com/p/24246712 一.什么是wpa_spplicant wpa_supplicant本是开源项目源码,被谷歌修改后加入Androi ...

  10. 一步一步学Silverlight 2系列(5):实现简单的拖放功能

    述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...