POJ3061 Subsequence

题目大意:给定长度为n的整列整数a[0],a[1],……a[n-1],以及整数S,求出总和不小于S的连续子序列的长度的最小值。

PS:用二分或者直尺法。省赛热身。

#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
const int INF = 0x3fffff;
vector<int> v;
int n, S;

int work() {
	int ans = INF;
	int l=0, r = 0;
	int sum = 0;
	for(int i = 0; i < n && sum < S; i++) {
		sum += v[i];
		r = i;
	}
	if(sum < S) return 0;
	while(r < n && l <= r) {
	    if(sum<S && r < n) {
			sum += v[++r];
		}
		if(sum >= S && r<n) {
			ans = min(ans, r-l+1);
			sum = sum - v[l++];
		}
	}
	return ans;
}

int main()
{
	int T;
	//freopen("in", "r", stdin);
	scanf("%d", &T);
	int t;
	while(T--) {
		scanf("%d%d", &n, &S);
		v.clear();
		for(int i = 0; i < n; i++) {
			scanf("%d", &t);
			v.push_back(t);
		}
		int res = work();
		printf("%d\n", res);
	}
    return 0;
}

POJ3061 Subsequence

时间: 2024-10-27 03:14:46

POJ3061 Subsequence的相关文章

poj3061 Subsequence&amp;&amp;poj3320 Jessica&#39;s Reading Problem(尺取法)

这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针(t)要么不动要么也往前走.满足这种特点的就可以考虑尺取法. poj3061 比较简单,也可以用二分做,时间复杂度O(n*logn).用尺取法可以O(n)解决. #include<iostream> #include<cstdio> #include<cstdlib> #i

POJ3061 Subsequence 尺取or二分

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 o

poj3061 Subsequence ,尺取法

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 sequen

poj3061(Subsequence)尺取法

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 o

POJ3061 Subsequence(二分前缀和法+尺取法)

二分+前缀和法 满足条件的子序列长度在(0,n)之间,sum[x+i]-sum[i]为从从第i个元素开始序列长度为x的元素的和.前缀和可在O(n)的时间内统计 sum[i]的值.再用二分找出满足条件的最小的子序列长度. #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<queue> #

POJ3061 Subsequence(二进制前缀和法律+仿真足)

二分法+前缀和法律 满足子序列长度的条件(0,n)之间,sum[x+i]-sum[i]从i元素开始序列长度x和.前缀和可在O(n)的时间内统计 sum[i]的值.再用二分找出满足条件的最小的子序列长度. #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<queue> #include&

[POJ3061]Subsequence(二分,前缀和)

题目链接:http://poj.org/problem?id=3061 题意:给一个长为n的数列和整数s,求一个连续的子序列,使得这个子序列长度最短并且不小于这个整数s. 统计[1~i]的子序列和sum(i),(sum(0)=0).然后求一个区间[i,j]的和即为sum(j)-sum(i-1) (i > 0). 由于给定序列没有负数,因此sum是个严格不减的序列. 转换成一个求最大值最小的问题,可以二分枚举序列长度,在前缀和上计算子序列[i-1,i+m-1]的和.如果存在一个满足子序列和≥s的,

POJ-3061 Subsequence 二分或尺取

题面 题意:给你一个长度为n(n<100000)的数组,让你找到一个最短的连续子序列,使得子序列的和>=m  (m<1e9) 题解: 1 显然我们我们可以二分答案,然后利用前缀和判断是否可行,这样是O(nlgn)的   注意没有答案 ans输出0 1 #include<cstdio> 2 #include<cstdlib> 3 #include<iostream> 4 #include<cstring> 5 using namespace

Subsequence poj3061 (二分)

http://poj.org/problem?id=3061 题意:找出一个连续子序列比m大,求最短符合题意的连续子序列的长度为多少?   #include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <vector> #include <algorithm> #include <map> #include <