绿色通道

https://www.acwing.com/problem/content/1092/

\(二分\ + \ 单调队列dp\)

#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
const int N = 5e4 + 7;
int a[N], f[N];
int q[N];
int n, t;

bool check(int x) {
    int hh = 0, tt = 0;
    for (int i = 1; i <= n; ++i) {
        if (q[hh] < i - x - 1) ++hh;
        f[i] = f[q[hh]] + a[i];
        while (hh <= tt && f[q[tt]] >= f[i]) --tt;
        q[++tt] = i;
    }
    for (int i = n - x; i <= n; ++i) 
        if (f[i] <= t) return true;
    return false;
}
    
int main() {
    IO;
    cin >> n >> t;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    int l = 0, r = n;
    while (l < r) {
        int mid = l + r >> 1;
        if (check(mid)) r = mid;
        else l = mid + 1;
    }
    cout << l << '\n';
    return 0;
}
posted @ 2021-02-04 01:58  phr2000  阅读(129)  评论(0编辑  收藏  举报