题解 P9483

· · 题解

前言

对不起。对不起。对不起。对不起。对不起。对不起。对不起。对不起。

思路

直接基于序列去动态构建决策树肯定死路一条。

考虑决策树的形态,一定是一棵完全二叉树(每个节点有 0/2 个孩子),代表右孩子放到了左孩子上。

尝试记录一棵决策树的信息,dep 和 dep 造成的代价和 cost 都与 a_i 无关,可以直接通过树的形态算出。而剩下每个叶子也能算出会贡献进答案的次数 c_i,也就是到根路径上走右孩子的次数。我们直接记录这些数的可重集即可作为一个合法状态。

最后考虑一个决策是严格比另一个决策优,当且仅当满足以下条件:

于是我们直接维护所有可能的决策进行 DP 转移,再删除掉不优的决策即可。注意到 n\leq 100 时决策的数量在 M=10^3 级别,而我们的算法复杂度为 O(M^3\log M),加上一些剪枝优化即可通过。

笔者的代码使用了下列剪枝,可以通过本题:

代码

// Problem: P9483 [NOI2023] 合并书本
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P9483
// Memory Limit: 512 MB
// Time Limit: 1500 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//♡_______♡
#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}
ll a[103];
struct node
{
    ll dep,cost;
    unsigned char occ[8];
    ll ld,rd;
    inline bool operator<(const node&t)const
    {
        if(dep!=t.dep) return dep<t.dep;
        if(cost!=t.cost) return cost<t.cost;
        for(int i=1; i<8; ++i)
            if(occ[i]!=t.occ[i]) return occ[i]<t.occ[i];
        return 0;
    }
};
vector<node> st[103];
node merge(const node &x,const node &y)
{
    node t=x;
    t.dep=(max(x.dep,y.dep)<<1)+1,
    t.cost=x.cost+y.cost+x.dep+y.dep;
    for(int i=0; i<7; ++i) t.occ[i+1]+=y.occ[i];
    t.occ[0]+=y.occ[0];
    t.ld=x.dep,t.rd=y.dep;
    return t;
}
bool cmp(const node &x,const node &y)
{
    if(x.dep<y.dep) return 1;
    if(x.cost<y.cost) return 1;
    for(int i=1; i<8; ++i) if(x.occ[i]<y.occ[i]) return 1;
    return 0;
}
const int N=100,G=1e3;
signed main()
{
    node e;
    memset(e.occ,0,sizeof(e.occ)),
    e.occ[0]=1,e.dep=e.cost=0,st[1].push_back(e);
    for(int i=2; i<=N; ++i)
        for(int j=1; j<=i-j; ++j)
        {
            for(node x:st[j]) for(node y:st[i-j])
            {
                if(y.dep<=G)
                {
                    node Z=merge(x,y);
                    if(st[i].empty()||cmp(Z,st[i].back()))
                        st[i].push_back(Z);
                }
                if(j<i-j&&x.dep<=G)
                {
                    node Z=merge(y,x);
                    if(st[i].empty()||cmp(Z,st[i].back()))
                        st[i].push_back(Z);
                }
            }
            stable_sort(st[i].begin(),st[i].end());
            vector<node> ans;
            for(node j:st[i])
                if(j.cost<1e13&&(ans.empty()||cmp(j,ans.back())))
                    ans.push_back(j);
            ans.swap(st[i]);
        }
    for(int T=read(); T--;)
    {
        int n=read();
        for(int i=1; i<=n; ++i) a[i]=read();
        sort(a+1,a+n+1);
        ll ans=1e18;
        for(int i=1; i<=n; ++i) a[i]+=a[i-1];
        for(node i:st[n])
        {
            ll sum=i.cost;
            for(int j=1; j<8; ++j) sum+=a[i.occ[j]];
            ans=min(ans,sum);
        }
        printf("%lld\n",ans);
    }
    return 0;
}