Optimización de la cola monótona de múltiples mochilas

Portal de temas

Al resolver varias mochilas, nuestro método más simple es tratar todos los elementos como independientes y resolver la mochila 01. La complejidad del tiempo es O (NV ∑ K) O (NV \ sum K)O ( N VK ) , entonces podemos usar la optimización binaria, la complejidad del tiempo esO (NV log ∑ k) O (NVlog \ sum k)O ( N V l o gk ) , pero todavía no podemos solucionar algunos problemas. En este momento, necesitamos utilizar colas monótonas para optimizar.

Primero consideramos la ecuación de transición de estado ordinaria: f [i] [j] = max (f [i - 1] [j - k ∗ w] + k ∗ v), (j - k ∗ w> = 0) f [ i] [j] = máx (f [i-1] [jk * w] + k * v), (jk * w> = 0)f [ i ] [ j ]=m a x ( f [ yo-1 ] [ j-kw ]+kv ) ,( j-kw>=0 )
Entonces podemos considerar ajustar el ciclo para optimizar la matriz unidimensional, es decir, para asegurarnos de que cada vez se transforme desde el tiempo anterior.
Obtenga la ecuaciónf [j] = max (pre [j - k ∗ w] + k ∗ v), (j - k ∗ w> = 0) f [j] = max (pre [jk * w] + k * v), (jk * w> = 0)f [ j ]=m a x ( p r e [ j-kw ]+kv ) ,( j-kw>=0 )

Entonces encontramos que f [j] f [j]f [ j ] solo se puede determinar mediantepre [j - k ∗ w] pre [jk * w]p r e [ j-kw ] transformado.
Ponemosf [m] f [m]f [ m ] (m es la capacidad total de la mochila) agrupación:
Inserte la descripción de la imagen aquí
lo que necesitamos obtener esmax (pre [j - k ∗ w] + k ∗ v) max (pre [jk * w] + k * v)m a x ( p r e [ j-kw ]+kv ) , por lo que mantenemos unmax (pre [j - k ∗ w] + k ∗ v) max (pre [jk * w] + k * v)m a x ( p r e [ j-kw ]+kEl valor máximo de v ) .
Entonces podemos obtener:
Inserte la descripción de la imagen aquí
encontramos que para f [j + k * v], k se incrementa en 1 cada vez, y se requiere que w se agregue al valor de cada elemento subsiguiente, por lo que necesitamos hacer alguna conversión.
Inserte la descripción de la imagen aquí
Código:

#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ls p<<1
#define rs p<<1|1
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
//#define int long long
#define pii pair<int,int>
#define ull unsigned long long
#define pdd pair<double,double>
#define lowbit(x) x&-x
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
char *fs,*ft,buf[1<<20];
#define gc() (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<20,stdin),fs==ft))?0:*fs++;
inline int read()
{
    
    
    int x=0,f=1;
    char ch=gc();
    while(ch<'0'||ch>'9')
    {
    
    
        if(ch=='-')
            f=-1;
        ch=gc();
    }
    while(ch>='0'&&ch<='9')
    {
    
    
        x=x*10+ch-'0';
        ch=gc();
    }
    return x*f;
}
using namespace std;
const int N=2e4+55;
const int inf=0x3f3f3f3f;
const int mod=998244353;
const double eps=1e-6;
const double PI=acos(-1);
int q[N],f[N],pre[N];
void solve()
{
    
    
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
    
    
        int v,w,s;
        cin>>v>>w>>s;
        memcpy(pre,f,sizeof f);
        for(int j=0;j<v;j++)
        {
    
    
            int head=0,tail=-1;
            for(int k=j;k<=m;k+=v)
            {
    
    
                if(head<=tail&&(k-q[head])/v>s)
                    head++;
                if(head<=tail)
                    f[k]=max(f[k],pre[q[head]]+(k-q[head])/v*w);
                while(head<=tail&&pre[q[tail]]-(q[tail]-j)/v*w<=pre[k]-(k-j)/v*w)
                    tail--;
                q[++tail]=k;
            }
        }
    }
    cout<<f[m]<<endl;
}
signed main()
{
    
    
//    int t;
//    cin>>t;
//    while(t--)
    solve();
    return 0;
}


Supongo que te gusta

Origin blog.csdn.net/Joker_He/article/details/109498686
Recomendado
Clasificación