动态规划(算法竞赛、蓝桥杯)--单调队列优化绿色通道

1、B站视频链接:E45 单调队列优化DP 绿色通道_哔哩哔哩_bilibili

cpp 复制代码
#include <bits/stdc++.h> 
using namespace std;
const int N=5e4+10;
int n,tim,w[N],f[N],q[N];

bool check(int m){
  int h=1,t=0;
  for(int i=1; i<=n; i++){
    while(h<=t && f[q[t]]>=f[i-1]) t--;
    q[++t]=i-1;
    if(q[h]<i-m) h++;
    f[i]=f[q[h]]+w[i];
    if(i>n-m && f[i]<=tim) return 1;//r指针左移 
  }
  return 0;
}
int main(){
  cin>>n>>tim;
  for(int i=1;i<=n;i++) cin>>w[i];
  int l=-1,r=n+1;
  while(l+1<r){
    int mid=l+r>>1;
    if(check(mid)) r=mid;
    else l=mid;
  }
  cout<<r-1; //空题段长度
}
相关推荐
Darkwanderor8 小时前
什么数据量适合用什么算法
c++·算法
zc.ovo8 小时前
河北师范大学2026校赛题解(A,E,I)
c++·算法
py有趣8 小时前
力扣热门100题之环形链表
算法·leetcode·链表
py有趣8 小时前
力扣热门100题之回文链表
算法·leetcode·链表
月落归舟10 小时前
帮你从算法的角度来认识二叉树---(二)
算法·二叉树
SilentSlot11 小时前
【数据结构】Hash
数据结构·算法·哈希算法
样例过了就是过了12 小时前
LeetCode热题100 柱状图中最大的矩形
数据结构·c++·算法·leetcode
wsoz12 小时前
Leetcode哈希-day1
算法·leetcode·哈希算法
阿Y加油吧12 小时前
LeetCode 二叉搜索树双神题通关!有序数组转平衡 BST + 验证 BST,小白递归一把梭
java·算法·leetcode