A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive
elements of the sequence, the sum of which is greater than or equal to S.

尺取法

设置两个指针s,t。

维护一个区间的信息。然后依据指针的移动,更新区间信息。

时间复杂度 O(n).

同类型练习题:

poj 3320  Jessica's Reading Problem

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 100000 + 100;
int a[maxn]; int main() {
int T, n, S;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &S);
for(int i=1; i<=n; ++i) scanf("%d", &a[i]);
int ans = n+1;
int tmp = 0, s = 1, t = 1;
while(true)
{
while(t<=n && tmp < S) {
tmp += a[t];
t++;
}
if(tmp < S) break;
ans = min(ans, t-s);
tmp -= a[s++];
} if(ans<=n) printf("%d\n", ans);
else printf("0\n");
}
return 0;
}

poj3061 Subsequence ,尺取法的更多相关文章

  1. POJ3061——Subsequence(尺取法)

    Subsequence POJ - 3061 给定长度为n的数列整数a0,a1,a2-an-1以及整数S.求出总和不小于S的连续子序列的长度的最小值,如果解不存在输出0. 反复推进区间的开头和末尾,来 ...

  2. POJ 3061 Subsequence 尺取法

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

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

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

  4. POJ3061 Subsequence 尺取or二分

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

  5. POJ 3061 Subsequence(尺取法)

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

  6. POJ 3061 Subsequence 尺取法,一个屌屌的O(n)算法

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9050   Accepted: 3604 Descr ...

  7. poj3061 Subsequence(尺取法)

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

  8. poj3061 Subsequence&&poj3320 Jessica's Reading Problem(尺取法)

    这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针 ...

  9. 尺取法 poj3061 poj3320

    尺取法就是反复推进区间的开头和结尾,来求满足条件的最下区间. poj3061 http://poj.org/problem?id=3061 给定一个都是正整数的序列,要我们求总和不小于S的连续子序列的 ...

随机推荐

  1. add Admob with Cocos2d-x on iOS

    add Admob with Cocos2d-x on iOS (2013-02-27 14:12:00) 转载▼ 标签: c=blog&q=it&by=tag" targe ...

  2. RDIFramework.NET V2.9版本号 WinFom部分新增与修正的功能

    RDIFramework.NET  V2.9版本号 WinFom部分新增与修正的功能 转眼间RDIFramework.NET框架走了快6个年头了,随着一个版本号一个版本号的升级改造,如今已经越来越完美 ...

  3. Android适屏

    总结一下自己的适屏经验,仅仅希望自己不断进步,不断完好,假设有热心肠的"前辈"指导一下,不胜感激! Android5.0已经出来了,说是这个版本号对Android屏幕适配做了非常多 ...

  4. Hadoop之文件系统Shell

    概述: 文件系统(FS)Shell包括各种类-Shell的命令.直接和Hadoop分布式文件系统(HDFS)交互,也支持对其它文件系统的支持.比如:本地文件系统FS,HFTP FS,S3 FS,和其它 ...

  5. LinkedList 方法知识点

    package test_day_9; import java.util.Iterator; import java.util.LinkedList; public class LinkedListD ...

  6. 转:Java修改Excel单元格的数据及格式

    https://blog.csdn.net/aking21alinjuju/article/details/6001153?locationNum=2 继前两节的Java读取.写入Excel后,本期将 ...

  7. MySql悲观锁总结与实践

    mysql(for update)悲观锁总结与实践 https://blog.csdn.net/zmx729618/article/details/52701972 悲观锁,正如其名,它指的是对数据被 ...

  8. Linux平台Oracle多个实例启动说明

    环境说明:oracle实例1的SID为orcl(为默认启动的实例),第二个实例的SID为orcl2 启动步骤:  1)启动数据库实例完成后,启动数据库监听服务 #lsnrctl   start 2)切 ...

  9. 在阿里云的iis上安装php扩展

    具体参照http://jingyan.baidu.com/article/11c17a2c5ce349f447e39d6d.html

  10. 16.unix网络编程一卷 unp.h

    unix网络编程 --ubuntu下建立编译环境 1.安装编译器,安装build-essential sudo apt-get install build-essential 2.下载本书的头文件 下 ...