跳到正文

题解

题解 P9483

dead_X约 6 分钟
原文
文章信息与版本

文章信息

发布
2023年8月9日
收录
2025年12月27日
更新
2026年11月7日
篇幅
约 6 分钟
题目
P9483

版本

  1. v2当前
  2. v1

前言

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

思路

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

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

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

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

  • depdep 小于等于另一个决策。
  • costcost 小于等于另一个决策。
  • 对于任意 tt,ci≥tc_i\geq t 的 ii 的数量小于等于另一个决策。

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

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

  • 我们不需要记录 cost>1013cost>10^{13} 的决策,因为 ai=109a_i=10^9 只需要约 101210^{12} 的代价。
  • 我们不需要记录轻子树 depdep 超过 10310^3 的决策,因为打表发现没有。
  • 预处理 ci≥tc_i\geq t 的 ii 的数量的数组 dtd_t,由于上一条剪枝只需要开 1010 左右的数组。

代码

C++
// 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;
}
原文
存档评论6 条抓取于