码迷,mamicode.com
首页 > 其他好文 > 详细

例题1.21 子序列 UVa1121

时间:2015-03-15 18:28:24      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:二分查找

1.题目描述:点击打开链接

2.解题思路:本题是典型的二分搜索题,二分答案后验证是否满足和大于等于S即可。

3.代码:

#define _CRT_SECURE_NO_WARNINGS 
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<functional>
using namespace std;

typedef long long LL;
#define N 100000+10
int a[N];
LL sum[N];
int n;
LL s;
bool ok(int len)
{
	for (int L = 0; L <= n - len;L++)
	if (sum[L + len] - sum[L] >= s)
		return true;
	return false;
}
int main()
{
	//freopen("t.txt", "r", stdin);
	while (~scanf("%d%d", &n, &s))
	{
		memset(a, 0, sizeof(a));
		memset(sum, 0, sizeof(sum));
		for (int i = 1; i <= n; i++)
		{
			cin >> a[i];
			sum[i] = sum[i - 1] + a[i];
		}
		if (sum[n] < s)printf("0\n");
		else
		{
			int L = 0, R = n;
			while (L < R)
			{
				int M = L + (R - L) / 2;
				if (ok(M))R = M;
				else L = M + 1;
			}
			cout << L << endl;
		}
	}
	return 0;
}

例题1.21 子序列 UVa1121

标签:二分查找

原文地址:http://blog.csdn.net/u014800748/article/details/44278543

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!