博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ3061 ZOJ3123 Subsequence【前缀和+二分搜索+尺取法】
阅读量:6969 次
发布时间:2019-06-27

本文共 2691 字,大约阅读时间需要 8 分钟。

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14836   Accepted: 6272

Description

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.

Input

The first line is the number of test cases. For each test case the program has to read the numbers N and S, separated by an interval, from the first line. The numbers of the sequence are given in the second line of the test case, separated by intervals. The input will finish with the end of file.

Output

For each the case the program has to print the result on separate line of the output file.if no answer, print 0.

Sample Input

210 155 1 3 5 10 7 4 9 2 85 111 2 3 4 5

Sample Output

23

Source

问题链接:。

题意简述:求长度为n的序列的一个子序列的最小长度,满足该子序列和不小于S。

问题分析

有两个方法可以解决该问题,一是前缀和+二分搜索,二是尺取法。

1.前缀和+二分搜索

计算前缀和是一个关键。有了前缀和,判断前缀和的差值是否大于s,就可以找到满足条件的最小子序列长度。

需要注意的是,计算前缀和时需要有prefixsum[0]=0这个元素。

二分搜索可以使用函数lower_bound()来实现。

2.尺取法

尺取法是先求出最前面的子序列,使之满足子序列和大于s;

重复后面一步,直到n个元素都使用到;

去掉子序列的第1个元素,子序列的后面再加上其他元素,满足子序列大于s的子序列,找出最小的长度。

程序说明

这个问题和《》应该说是同一个问题,只是输入格式略有不同而已。

使用前缀和+二分搜索的方法时,原来的元素是不必存储的,有前缀和数组就够了。

参考链接

AC的C++语言程序(前缀和+二分查找)如下:

/* POJ3061 ZOJ3123 Subsequence */#include 
#include
using namespace std;const int N = 100000;int prefixsum[N+1];int main(){ int t, n, s, val, ans; cin >> t; while(t--) { cin >> n >> s; // 输入数据,计算前缀和 prefixsum[0] = 0; for(int i=1; i<=n; i++) { cin >> val; prefixsum[i] = prefixsum[i - 1] + val; } if(prefixsum[n] < s) ans = 0; else { ans = n; for(int i=0; prefixsum[i] + s < prefixsum[n]; i++) { int pos = lower_bound(prefixsum + i, prefixsum + n, prefixsum[i] + s) - prefixsum; ans = min(ans, pos - i); } } cout << ans << endl; } return 0;}

AC的C++语言程序(尺取法)如下:

/* POJ3061 ZOJ3123 Subsequence */#include 
using namespace std;const int N = 100000;int a[N+1];int solve(int n, int s){ int res = n + 1; int start=0, end=0, sum=0; for(;;) { while(end < n && sum < s) sum += a[end++]; if(sum < s) break; res = min(res, end - start); sum -= a[start++]; } return (res > n) ? 0 : res;}int main(){ int t, n, s; cin >> t; while(t--) { cin >> n >> s; for(int i=0; i
> a[i]; cout << solve(n, s) << endl; } return 0;}

转载于:https://www.cnblogs.com/tigerisland/p/7563664.html

你可能感兴趣的文章
Linux SendMail发送邮件失败诊断案例(四)
查看>>
SpringMVC+hibernate4事务处理
查看>>
大型网站架构演化历程
查看>>
[osg][原]自定义osgGA漫游器
查看>>
JSX 语法
查看>>
Day8 Servlet
查看>>
iOS 集成Protobuf,转换proto文件
查看>>
ZooKeeper伪集群环境搭建
查看>>
[Chromium]怎样安全的使用PostTask
查看>>
python 合并字典,相同 key 的 value 如何相加?
查看>>
Android开发之深入理解Android Studio构建文件build.gradle配置
查看>>
Martini源码剖析
查看>>
【spring boot】【log4jdbc】使用log4jdbc打印mybatis的sql和Jpa的sql语句运行情况
查看>>
BZOJ3265: 志愿者招募加强版(线性规划)
查看>>
Java提高:采用异常链传递异常
查看>>
SQL Server中LIKE %search_string% 走索引查找(Index Seek)浅析
查看>>
在WPF中制作正圆形公章
查看>>
dataframe 合并(append, merge, concat)
查看>>
几种常用网络传输协议
查看>>
Http请求头和响应头
查看>>