首先我们发现金字塔的高度不会超过 ,不超过 200,所以金字塔最多有 种,可以预处理出所有金字塔,并按照石头数量从小到大排序。
记 表示考虑了第 个金字塔,使用了恰好 个石头的字典序最小方案(这里的字典序最小是指首先金字塔数最少,然后最大(石头数量最多)的金字塔最大,然后次大的金字塔最大…)
然后我们发现对于每个可以被划分为若干个金字塔的石块数量,可以被划分为至多 个金字塔。所以我们可以将已经使用的金字塔的编号压缩为一个 unsigned long long,表示一个方案。
对于转移,可以按照 01背包的方式转移。
时间复杂度
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define F(i,l,r) for(ll i = l;i <= r;i++)
using namespace std;
struct dat{ ll sz,x; char c; }a[405];
ull f[1000005]; ll n,t;
void out(ull x){
cout << "Case " << ++t << ": ";
if((x & 511) <= 400 && (x >> 63)){
stack<ll> v;
while((x & 511) <= 400 && (x & 511)) v.push(x & 511), x >>= 9;
while(v.size()){
cout << a[v.top()].x << a[v.top()].c;
v.pop(); if(v.size()) cout << " ";
} cout << "\n";
}else cout << "impossible\n";
cin >> n;
}
int main(){ f[0]--;//使金字塔数最少的优先
F(i,1,200) a[i] = {a[i - 1].sz + i * i,i,'H'},
a[i + 200] = {a[i + 198].sz + i * i,i,'L'};
sort(a + 1,a + 401,[](dat a,dat b)
{ return (a.sz == b.sz ? a.c > b.c : a.sz < b.sz); });
for(ull i = 400;i > 3;i--) for(ll j = 1e6;j >= a[i].sz;j--)
f[j] = max(f[j],(f[j - a[i].sz] << 9) + i);//字典序的大小关系与unsigned long long的大小关系对应
cin >> n; while(n) out(f[n]);
return 0;
}