Select Luogu P2034 digital solution to a problem

Cackle

Monotonous queue optimization \ (DP \) of the first question, the results of died on some very strange things. . .

Face questions

Face questions

Solution

Define \ (dp_i \) said it was considering the former \ (i \) number, the maximum value can be selected.

Because the maximum period of continuous option \ (K \) a, so we can \ ([ik, i-1 ] \) select a breakpoint \ (J \) , indicates that the point is not selected, then the time \ ( = dp_i \ min (J-dp_ {} -sum_j. 1) + SUM [I] \) .

Then we found \ (dp_i \) endpoint \ (\ I) cases mandatory, not representative of all cases, so the (dp_i, dp_ {i-1 } \) \ take a \ (\ max \) so that it can include before considering \ (i \) in all cases of the two.

Code

#include<bits/stdc++.h>
#define del(a,i) memset(a,i,sizeof(a))
#define ll long long
#define inl inline
#define il inl void
#define it inl int
#define ill inl ll
#define re register
#define ri re int
#define rl re ll
#define mid ((l+r)>>1)
#define lowbit(x) (x&(-x))
#define INF 0x3f3f3f3f
using namespace std;
template<class T>il read(T &x){
    int f=1;char k=getchar();x=0;
    for(;k>'9'||k<'0';k=getchar()) if(k=='-') f=-1;
    for(;k>='0'&&k<='9';k=getchar()) x=(x<<3)+(x<<1)+k-'0';
    x*=f;
}
template<class T>il print(T x){
    if(x/10) print(x/10);
    putchar(x%10+'0');
}
ll mul(ll a,ll b,ll mod){long double c=1.;return (a*b-(ll)(c*a*b/mod)*mod)%mod;}
it qpow(int x,int m,int mod){
    int res=1,bas=x%mod;
    while(m){
        if(m&1) res=(res*bas)%mod;
        bas=(bas*bas)%mod,m>>=1;
    }
    return res%mod;
}
const int MAXN = 1e5+5;
int n,k,val,q[MAXN],l=1,r;
ll sum[MAXN],dp[MAXN],ans;
int main()
{
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    read(n),read(k);
    for(ri i=1;i<=n;++i) read(val),sum[i]=sum[i-1]+val;
    dp[1]=sum[1],q[++r]=1;
    for(ri i=2;i<=k;++i){
        dp[i]=sum[i];
        while(l<=r&&dp[q[r]-1]-sum[q[r]]<=dp[i-1]-sum[i]) --r;
        q[++r]=i;
    }
    for(ri i=k+1;i<=n;++i){
        while(l<=r&&q[l]<i-k) ++l;
        dp[i]=sum[i]+dp[q[l]-1]-sum[q[l]];
        while(l<=r&&dp[q[r]-1]-sum[q[r]]<=dp[i-1]-sum[i]) --r;
        q[++r]=i;
        dp[i]=max(dp[i],dp[i-1]);
    }
    print(dp[n]);
    return 0;
}

to sum up

A first track with a monotonically konjac queue optimization \ (the DP \) of the title, but not the final one.

This question is to ask the room where \ (Dalao \) \ (@ jklover \) , but on examination it?

So they should strengthen their ability to refuel ah! ! !

Guess you like

Origin www.cnblogs.com/TheShadow/p/11415173.html