题目传送门

 /*
题意:求连续子序列的和不小于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. CentOS系统如何设置服务开机自动运行

    centos安装好apache,mysql等服务器程序后,并没有设置成开机自动启动的,为避免重启后还要手动开启web等服务器,还是做下设置好,其实设置很简单,用chkconfig命令就行了.例如要开机 ...

  2. 单点登录cas常见问题(二) - 子系统是否会频繁訪问cas中心?

    这个问题的完整描写叙述是:用户成功登陆后.在訪问子系统的受限资源时,还须要訪问cas中心么,即子系统是否还会频繁訪问cas中心.cas中心会不会压力太大? 答案是:不会. 假设用户通过子系统A登录了c ...

  3. JS 省市区三级联动

    JS 省市区三级联动: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...

  4. Make 编译脚本上手

    考察下面的示例代码: main.c #include <stdio.h> int main(){ printf("hello world!"); return 0; } ...

  5. C语言 字符串操作 笔记

    /* C语言字符串的操作笔记 使用代码和注释结合方式记录 */ # include <stdio.h> # include <string.h> int main(void) ...

  6. Android实战简易教程-第四十枪(窃听风云之短信监听)

    近期在做监听验证码短信自己主动填入的功能,无意间想到了一个短信监听的办法. 免责声明:短信监听本身是一种违法行为,这里仅仅是技术描写叙述.请大家学习技术就可以.(哈哈) 本实例是基于bmob提供的后台 ...

  7. 设计模式-(14)装饰者模式 (swift版)

    一,概念 装饰者模式(Decorator):动态地为一个对象添加一些额外的职责,若要扩展一个对象的功能,装饰者提供了比继承更有弹性的替代方案. 多组合,少继承 二,UML图 抽象构件类(Compone ...

  8. caioj1275&&hdu4035: 概率期望值6:迷宫

    期望的大难题,%%ZZZ大佬的解释,不得不说这是一道好题(然而膜题解都没完全看懂,然后就去烦ZZZ大佬) 简单补充几句吧,tmp的理解是个难点,除以tmp的原因是,当我们化简时,子节点也有一个B*f[ ...

  9. UVA10462Is There A Second Way Left? —— 次小生成树 kruskal算法

    题目链接:https://vjudge.net/problem/UVA-10462 Nasa, being the most talented programmer of his time, can’ ...

  10. 工作笔记——sqlserver引号的运用

    一. sqlserver引号问题:因为要使用远程连接,所以sql语句要用单引号括起来 SELECT * FROM OPENQUERY ([192.168.***.***] ,'select * fro ...