POJ 3061 - Subsequence - 尺取法 或 前缀和+二分

Subsequence

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 30339 Accepted: 12756

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

2
10 15
5 1 3 5 10 7 4 9 2 8
5 11
1 2 3 4 5

Sample Output

2
3

Source

Southeastern Europe 2006

题目大意

给定一个序列,包含N个正整数(10 <N <100 000),每个正整数均小于或等于10000,给定一个小于100000000的正整数S。求序列中最短的连续区间的长度,要求其区间和大于或等于S 。

思路

第一种方法是我们可以O(n)求前缀和,枚举各个左端点点,然后用lower_bound找到第一个右端点,复杂度为O(nlogn),每次长度取min即可(具体见代码)

第二种方法是尺取法,左端点l和右端点r初始化为0

  • 如果sum < s,右端点r++,sum加上a[r],直到sum大于等于s
  • 不管有没有经过上一步骤,只要sum >= s,长度就取min
  • 左端点l ++,sum减去a[l],达到枚举每个左端点的目的,然后继续前两步骤

左端点和右端点始终向右移,时间复杂度为O(n)(实现见代码)

Tip:poj数据弱好像没有答案为1的情况,见第一种方法代码的注释

代码

前缀和枚举左端点+二分

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
const int INF = 0x3f3f3f3f;
ll qpow(ll base, ll n){
	ll ans = 1;
	while (n){
		if (n & 1) ans = ans * base % mod;
		base = base * base % mod;
		n >>= 1;
	}
	return ans;
}
int sum[N];
int main()
{
	int t;
	cin >> t;
	while (t --){
		int n, s;
		cin >> n >> s;
		for (int i = 1; i <= n; ++ i) {
			scanf("%d", &sum[i]);
			sum[i] += sum[i - 1];
		}
		if (sum[n] < s) {
			printf("0\n");
			continue;
		}
		int ans = INF;
		for (int l = 1; sum[n] - sum[l - 1] >= s; ++ l){
			int r = lower_bound(sum + l, sum + n + 1, sum[l - 1] + s) - sum;
			//一开始我写的是lower_bound(sum + l + 1, sum + n + 1, sum[l - 1] + s) - sum
			//那样写显然是错的,漏了a[i]本身就大于等于s的情况,即长度为1的情况
			ans = min(ans, r - l + 1);
		}
		printf("%d\n", ans);
	}
	return 0;
}

尺取法

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
const int INF = 0x3f3f3f3f;
ll qpow(ll base, ll n){
	ll ans = 1;
	while (n){
		if (n & 1) ans = ans * base % mod;
		base = base * base % mod;
		n >>= 1;
	}
	return ans;
}
int a[N];
int main()
{
	int t;
	cin >> t;
	while (t --){
		int n, s;
		cin >> n >> s;
		for (int i = 1; i <= n; ++ i) {
			scanf("%d", &a[i]);
		}
		int sum = 0, l = 0, r = 0;
		int ans = INF;
		while (r <= n){
			while (r <= n && sum < s){
				sum += a[++ r];
			}
			if (sum < s) break;//如果sum < s,那没必要移左端点了
			ans = min(ans, r - l);
			sum -= a[++ l];
		}
		if (ans == INF) ans = 0;
		printf("%d\n", ans);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值