LOJ6039 "Yali training 2017 Day5 'jewelry

topic

Notice \ (c_i \ leq 300 \) we obviously can use \ (c_i \) to carry out his things

A natural idea is based on our \ (c_i \) grouping, the volume of items within each group are the same, so the descending order according to the value, into a plurality of articles, so we have transformed into a problem packet knapsack problem

So we have such a \ (DP \) , \ (dp_ {I, J} = \ max dp_ {I-. 1, JK \ Times I} + W_ {I, K} \) , \ (W_ {I, k} \) represents the \ (I \) front group \ (K \) value of an article, but the complexity is still too high packet backpack

Not difficult to find, \ (J \) and \ (ji \ times k \) is in \ (\ mod \ i \) equal to the sense, so we can make for the same group according to \ (\ mod \ i \) the value classification

Can also be found in the \ (DP \) presence decision monotonic, if \ (jk \ times i \) than \ (jp \ times i (p > k) \) in \ (J \) more preferably, greater then for the \ (J \) is \ (jk \ times i \) , or better than \ (JP \ Times I \) , since \ ({i, k} w_ \) are all positive differential after, \ (JK \ times i \) is growing faster than \ (jp \ times i \) , so we can direct partition

Complexity is \ (O (mc \ log m ) \) Code

#include<bits/stdc++.h>
#define re register
#define LL long long
#define max(a,b) ((a)>(b)?(a):(b))
inline int read() {
    char c=getchar();int x=0;while(c<'0'||c>'9')c=getchar();
    while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+c-48,c=getchar();return x;
}
int n,m,nw,T;
std::vector<LL> w[305];
LL dp[50005],g[50005],f[50005];
inline int cmp(LL A,LL B) {return A>B;}
void solve(int l,int r,int x,int y) {
    if(l>r) return;int mid=l+r>>1;
    f[mid]=g[mid];int id=mid;
    for(re int i=x;i<=y&&i<mid;++i) {
        if(mid-i>w[nw].size()) continue;
        LL k=g[i]+w[nw][mid-i-1];
        if(k>f[mid]) f[mid]=k,id=i;
    }
    if(l==r)return;
    solve(l,mid-1,x,id);solve(mid+1,r,id,y);
}
int main() {
    n=read(),m=read();
    for(re int c,v,i=1;i<=n;i++)c=read(),v=read(),w[c].push_back((long long)v),T=max(c,T);
    for(re int i=1;i<=T;i++) {
        if(!w[i].size()) continue;
        std::sort(w[i].begin(),w[i].end(),cmp);
        for(re int j=1;j<w[i].size();++j) w[i][j]+=w[i][j-1];
    }
    for(re int i=T;i;i--) {
        if(!w[i].size())continue;nw=i;
        for(re int tot=0,j=0;j<i;j++,tot=0) {
            for(re int k=j;k<=m;k+=i) g[++tot]=dp[k];
            solve(1,tot,1,tot);
            for(re int h=1,k=j;k<=m;k+=i,++h) dp[k]=f[h];
        }
        for(re int j=1;j<=m;j++)dp[j]=max(dp[j],dp[j-1]);
    }
    for(re int i=1;i<=m;i++) printf("%lld ",dp[i]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/asuldb/p/12155151.html