尺取法 poj-3061Subsequence

尺取法,借鉴挑战程序设计竞赛,尺取法是通过对数组的下标的起点和终点,根据实际情况交替推进两个端点,最后得出答案。

题目:

DescriptionA 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.

题目大意:给定长度为n的整列整数a0, a1,…an-1以及一个整数S,求出总和不小于S的连续子序列的长度的最小值。如果解不存在,则输出0.

将i从0到i的和都给存进sum数组, 也就是说sum[i] = a0+a1+…+ai-1,那么对于区间[s, t)来说,数列和为sum[t] - sum[s],预处理计算好sum后,就可以通过二分搜索快速确定使序列和不小于S的结尾t的最小值,然后t-s后就是这段区间的长度,一次次进行res = min(res, t-s), 来获得最小的区间。

注意这里二分搜索是找到一个sum[t]使其减去sum[s]恰好大于S,也就是找到一个数恰好比sum[s]+S大一点点,说到这里可能你已经想到了,没错,就是利用lower_bound()来进行快速操作。

废话少说, 上代码了:

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

const int max_n = 100005;
int n, S;
int a[max_n];
int sum[max_n+1];

void slove();

int main()
{
	int c;
	
	scanf("%d", &c);
	
	while (c--)
	{
		scanf("%d %d", &n, &S);

		for (int i=0; i<n; i++)
		{
			scanf("%d", &a[i]);
		}

		slove();
	}
	
	return 0;
}

void slove()
{
	for (int i=0; i<n; i++)
	{
		//接下来下标就是从1开始的了 
		sum[i+1] = sum[i]+a[i];
	}

	//如果序列总和还没有S大肯定无解,直接输出0 
 	if (sum[n] < S)
	 {
	  printf("0\n");
	  return;
	 }

	//序列最大就为n,所以把res设置为n 
	int res = n;

	for (int s=0; sum[s] + S <= sum[n]; s++)
	{
		int t = lower_bound(sum+s, sum+n, sum[s]+S) - sum;
  
  		res = min(res, t-s);
	}

	printf("%d\n", res);
}

算法的复杂度为O(nlogn),虽然已经能够解决这个问题了,但是还有更高效的方法。

具体想法是有一个s = t = sum = 0;让t先开始走,然后sum进行加和,当找到一个sum使其不小于S时,就跳出循环,将t-s的值与res进行必要。然后在让sum-a[s], s++,继续寻找下一个满足的地方。不断更新res的值,最终找到最小的。

具体实现如下:

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

void slove();

const int max_n = 100005;
int n, S;
int a[max_n];

int main()
{
	int c;
 
	scanf("%d", &c);

	while (c--)
	{
		scanf("%d %d", &n, &S);

		for (int i=0; i<n; i++)
		 {
			  scanf("%d", &a[i]);
		 }

		slove();
	}
		
	return 0;
}

void slove()
{
	int res = n+1;

	int s = 0, t = 0, sum = 0;
	
	for (; ;)
	{
		while (t<n && sum<S)
		{
			sum+=a[t++];
		}
		
		if (sum < S)
		{
			break;
		}

		res = min(res, t-s);
		sum-=a[s++];
	}

	if (res > n)
	{
		res = 0;
	}
	
	printf("%d\n", res);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值