1400*B. Books(双指针)

Problem - 279B - Codeforces 

Books - 洛谷

1400*B. Books(双指针)_第1张图片

1400*B. Books(双指针)_第2张图片 

解析:

        在一个区间中找一段连续区间,使其总和小于m,并且区间长度最长。

        双指针,遍历区间,每次删除头部的元素,然后往后一直找到满足的最大长度。维护一段动态区间。

#include
using namespace std;
#define int long long
const int N=1e5+5;
int n,m,a[N];
signed main(){
	scanf("%lld%lld",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
	}
	int sum=m,cnt=0;
	int j=0;
	while(j+1<=n&&sum>=a[j+1]){
		sum-=a[j+1];
		j++;
	}
	int res=j;
	for(int i=1;i<=n;i++){
		sum+=a[i];
		while(j+1<=n&&sum>=a[j+1]){
			sum-=a[j+1];
			j++;
		}
		res=max(res,j-i);
	}
	cout<

你可能感兴趣的:(codeforces,算法,宽度优先,开发语言,图论,c语言)