绿色通道(单调队列优化,dp,二分)

在这里插入图片描述

思路:找最小值最大首先想到二分,我们发现这个时间t对答案来说满足单调性,所以我们二分答案,check时,我们发现就是判断长度为x的区间最小值是否小于t,dp[i]表示1——i-1位置满足题意且i位置必须写题,那么可以得dp[i]=min(dp[j])+w[i]我们用单调队列维护dp[j]即可

#pragma GCC optimize(2)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<iomanip>
#include<cstring>
#include<time.h>
 
using namespace std;
typedef long long ll;
#define SIS std::ios::sync_with_stdio(false)
#define space putchar(' ')
#define enter putchar('\n')
#define lson root<<1
#define rson root<<1|1
typedef pair<int,int> PII;
const int mod=1e9+7;
const int N=2e6+10;
const int M=1e5+10;
const int inf=0x3f3f3f3f;
const int maxx=2e5+7;
const double eps=1e-6;
int gcd(int a,int b)
{
    
    
    return b==0?a:gcd(b,a%b);
}
 
ll lcm(ll a,ll b)
{
    
    
    return a*(b/gcd(a,b));
}
 
template <class T>
void read(T &x)
{
    
    
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-')
            op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op)
        x = -x;
}
template <class T>
void write(T x)
{
    
    
    if(x < 0)
        x = -x, putchar('-');
    if(x >= 10)
         write(x / 10);
    putchar('0' + x % 10);
}
ll qsm(int a,int b,int p)
{
    
    
    ll res=1%p;
    while(b)
    {
    
    
        if(b&1)
            res=res*a%p;
        a=1ll*a*a%p;
        b>>=1;
    }
    return res;
}
int n, m;
int dp[N];
int q[N];
int w[N];
bool check(int x){
    
    
    int hh=0,tt=0;
    for(int i=1;i<=n;i++){
    
    
        if(q[hh]<i-x-1)hh++;
        dp[i]=dp[q[hh]]+w[i];
        while(hh<=tt&&dp[q[tt]]>=dp[i])tt--;
        q[++tt]=i;
    }
    for(int i=n-x;i<=n;i++)
        if(dp[i]<=m)return true;
    return false;
    
}
int main()
{
    
    
    cin>>n>>m;
    int l=0,r=n;
    for(int i=1;i<=n;i++)cin>>w[i];
    while(l<=r)
    {
    
    
        int mid=l+r>>1;
        if(check(mid)) r=mid-1;
        else l=mid+1;
    }
    cout<<r+1<<endl;

  



    
      
       
      

    return 0;

}


猜你喜欢

转载自blog.csdn.net/qq_43619680/article/details/109344899