尺取法 POJ 3601 Subsequence
/*
题意:求连续子序列的和不小于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的更多相关文章
- POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13955 Accepted: 5896 Desc ...
- 尺取法 poj 2566
尺取法:顾名思义就是像尺子一样一段一段去取,保存每次的选取区间的左右端点.然后一直推进 解决问题的思路: 先移动右端点 ,右端点推进的时候一般是加 然后推进左端点,左端点一般是减 poj 2566 题 ...
- 尺取法 POJ 3320 Jessica's Reading Problem
题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...
- 尺取法 || POJ 2739 Sum of Consecutive Prime Numbers
给一个数 写成连续质数的和的形式,能写出多少种 *解法:先筛质数 然后尺取法 **尺取法:固定区间左.右端点为0,如果区间和比目标值大则右移左端点,比目标值小则右移右端点 ...
- POJ 3061 Subsequence(尺取法)
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18145 Accepted: 7751 Desc ...
- POJ 3061 Subsequence ( 尺取法)
题目链接 Description A sequence of N positive integers (10 < N < 100 000), each of them less than ...
- 题解报告:poj 3061 Subsequence(前缀+二分or尺取法)
Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...
- POJ 3061 Subsequence 二分或者尺取法
http://poj.org/problem?id=3061 题目大意: 给定长度为n的整列整数a[0],a[1],--a[n-1],以及整数S,求出总和不小于S的连续子序列的长度的最小值. 思路: ...
- POJ 3061 Subsequence 尺取法
转自博客:http://blog.chinaunix.net/uid-24922718-id-4848418.html 尺取法就是两个指针表示区间[l,r]的开始与结束 然后根据题目来将端点移动,是一 ...
随机推荐
- centos 安装python2.7
安装pip sudo yum -y install epel-release sudo yum -y install python-pip 下载解压Python-2.7.3 #wget http:// ...
- 一处折腾笔记:Android内嵌html5加入原生微信分享的解决的方法
有一段时间没有瞎折腾了. 这周一刚上班萌主过来反映说:微信里面打开聚客宝.分享功能是能够的(这里是用微信自身的js-sdk实现的).可是在android应用里面打开点击就没反应了:接下来狡猾的丁丁在产 ...
- [RxJS] Implement RxJS `concatMap` by Waiting for Inner Subscriptions to Complete
Unlike mergeMap and switchMap, concatMap focuses on when "inner" subscriptions "compl ...
- Android网络通信之Socket
在移动APP开发中.网络通信数据传输是必定存在的.移动APP离开了网络通信数据传输的功能方式,就好比一潭死水,永远都 是原来的样子. 提到网络通信传输数据.首先出如今程序猿脑海中的是HTTP协议传输, ...
- 浅谈 ZipArchive 类
Microsoft .NET Framework 4.5 新增了 ZipArchive 类 Microsoft Windows 8 Consumer Preview 操作系统已经内置了 Microso ...
- A + B Problem II(杭电1002)
/*A + B Problem II Problem Description I have a very simple problem for you. Given two integers A an ...
- MySQL InnoDB类型数据库的恢复
MySQL的数据库文件直接复制便可以使用,但是那是指“MyISAM”类型的表. 而使用MySQL-Front直接创建表,默认是“InnoDB”类型,这种类型的一个表在磁盘上只对应一个“*.frm”文 ...
- onDestroy强制退出后,process crash的处理
from http://bbs.9ria.com/thread-248722-1-1.html 一般情况,我们在执行测试的过程中都会调用tearDwon方法,以Robotium为例,我们在te ...
- IDEA--java版本修改(jdk1.8改成jdk1.7)
转载:https://blog.csdn.net/huyishero/article/details/61916516
- 【iOS系列】-iOS的多线程解析
[iOS系列]-iOS的多线程解析 iOS的多线程实现技术: 1:GCD -- Grand Central Dispatch 是基于C语言的底层API 用Block定义任务,使用起来非常灵活便捷 提供 ...