AcWing1090. 绿色通道 | ybt1603:绿色通道

#include <bits/stdc++.h>
using namespace std;
const int N=5e4+10;
int w[N],f[N],n,m,q[N];
bool check(int limit){
    int hh=0,tt=0;
    for(int i=1;i<=n;i++){
        while(hh<=tt&&q[hh]<i-limit-1) hh++;
        f[i]=f[q[hh]]+w[i];
        while(hh<=tt&&f[q[tt]]>=f[i]) tt--;
        q[++tt]=i;
    }
    for(int i=n-limit;i<=n;i++){
        if(f[i]<=m) return true;
    }
    return false;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%d",&w[i]);
    int l=0,r=n;
    while(l<r){
        int mid=l+r>>1;
        if(check(mid)) r=mid;
        else l=mid+1;
    }
    printf("%d\n",l);
    return 0;
}
posted @ 2025-03-30 08:23  九三青梧  阅读(16)  评论(0)    收藏  举报