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

思路

第一种方法是我们可以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 
#include 
#include 
#include 
#include 
#include 
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 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 
#include 
#include 
#include 
#include 
#include 
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 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;
}

 

你可能感兴趣的:(POJ 3061 - Subsequence - 尺取法 或 前缀和+二分)