Green channel (monotonic queue optimization, dp, two points)

Insert picture description here

Idea: To find the maximum minimum value, first think of dichotomy. We found that this time t satisfies the monotonicity of the answer, so we dichotomy the answer. When we check, we find that we judge whether the minimum value of the interval of length x is less than t, dp[i] Means 1-i-1 position satisfies the meaning of the question and the i position must be written, then you can get dp[i]=min(dp[j])+w[i] we can use a monotonic queue to maintain 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;

}


Guess you like

Origin blog.csdn.net/qq_43619680/article/details/109344899