题目传送门

 /*
题意:求连续子序列的和不小于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. 原来,表名和字段名不能在pdo中“参数化查询”

    https://stackoverflow.com/questions/182287/can-php-pdo-statements-accept-the-table-or-column-name-as ...

  2. Intel为Google的物联网平台Brillo推出开发板Edison

    Brillo* is a solution from Google* for building connected devices. Incorporating aspects of the Andr ...

  3. MySQL的引入,绿色包下载和应用

    一.下载MySQL绿色版 1.下载地址: 以下是MySQL最新绿色版链接(都是来源于oracle官网),点击以下链接直接下载. 1.1.官网链接:https://www.oracle.com/inde ...

  4. Android之——经常使用手机号码功能

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47374415 有些Android手机中会带有一些经常使用号码的功能,比方订餐电话. ...

  5. GEO,IGSO,MEO,LEO

    GEO(Geosynchronous Eearth Orbit):地球静止轨道卫星 IGSO(Inclined Geosynchronous Satellite Orbit):倾斜轨道同步卫星 地球同 ...

  6. Nyquist–Shannon sampling theorem 采样定理

    Nyquist–Shannon sampling theorem - Wikipedia https://en.wikipedia.org/wiki/Nyquist%E2%80%93Shannon_s ...

  7. C# 中串口通信 serialport1.DataReceived 函数无法触发或者出发延时等等问题解决方法

    以前这个问题困扰我多天最后查资料一大堆,最后最终攻克了,看到非常多人做C#串口都遇到相同的问题,所以写一篇博文,以便学习交流. 一定要在com实例化的时候设置ReceivedBytesThreshol ...

  8. spring boot redis缓存入门

    摘要: 原创出处 泥瓦匠BYSocket 下载工程 springboot-learning-example ,工程代码注解很详细.JeffLi1993/springboot-learning-exam ...

  9. 深入探析c# Socket

    最近浏览了几篇有关Socket发送消息的文章,发现大家对Socket Send方法理解有所偏差,现将自己在开发过程中对Socket的领悟写出来,以供大家参考. (一)架构 基于TCP协议的Socket ...

  10. bzoj 5017 炸弹

    题目大意: 直线上有n个炸弹有坐标x和半径r 当一个炸弹被引爆时 若有炸弹的坐标在该炸弹坐标+-r范围内则另一个炸弹也被引爆 求先引爆每一个炸弹最终会引爆多少炸弹 思路: 可以想到n平方连边然后tar ...