codeforces279B

题意:求最长的连续子序列。。。

input

4 5
3 1 2 1

output

3

input

3 3
2 2 3

output

1 

<法一>由于数据范围是(1 ≤ n ≤ 10^5; 1 ≤ t ≤ 10^9) ,不会超时,可以直接模拟,不会超时。 

#include <stdio.h>
#include <assert.h>
#include <string.h>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1e6+10;
typedef long long ll;
ll n;
ll t;
ll a[maxn];
ll sum = 0;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> t;
	ll j = 1;
	ll ans = 0;
	ll cnt = -1;
	for (ll i = 1; i <= n; ++i) {
		cin >> a[i];
		sum += a[i];
		ans++;
		while (sum > t) {
			sum -= a[j];
			j++;
			ans--;
		}
		cnt = max(cnt, ans);
	}
	cout << cnt << endl;
//	system("pause");
	return 0;
}

<法二>

利用dp或二分也能做出来。(目前还没学会。。。过段时间再补)

猜你喜欢

转载自blog.csdn.net/zsnowwolfy/article/details/82839238
今日推荐