题解:P13494 【MX-X14-T4】分门别类
为啥带
先考虑怎么判
现在考虑最小化集合数量。直接贪心构造比较复杂,我们考虑 dp。
不妨把相同的数塞在一起,那么相当于我们有
于是我们不妨设
于是考虑转移。我们枚举这一种数有
容易发现直接转移的复杂度是正确的。枚举
有了 dp 之后构造方案是简单的。我们可以通过记录
int tc;
int n,k;
int a[N],bac[N];
map <int,int> c;
int f[N][N];
int pre[N][N];
vector <int> ans[N];
stack <int> even,odd;
void solve(){
c.clear();
cin>>n;
fr1(i,1,n) cin>>a[i],c[a[i]]++;
if(n&1) return cout<<"-1\n",void();
for(auto i:c){
if(i.se>n/2) return cout<<"-1\n",void();
}
k=c.size();
int tot=0;
for(auto i:c) tot++,a[tot]=i.se,bac[tot]=i.fi;
fr1(i,0,k){
fr1(j,0,n) f[i][j]=1e9;
}
f[0][0]=0;
fr1(i,0,k-1){
fr1(j,0,n){
fr1(c,0,min(a[i+1],j)){
int to=j+a[i+1]-2*c;
if(f[i+1][to]>max(f[i][j],j+a[i+1]-c)){
pre[i+1][to]=j;
f[i+1][to]=max(f[i][j],j+a[i+1]-c);
}
}
}
}
int res=f[k][0];
cout<<res<<'\n';
while(!even.empty()) even.pop();
while(!odd.empty()) odd.pop();
fr1(i,1,res) ans[i].clear(),even.push(i);
int u=k,t=0;
while(u){
int fr=pre[u][t];
int c=(fr+a[u]-t)/2;
vector <int> dlo,dle;
fr1(i,c+1,a[u]){
ans[odd.top()].pb(bac[u]);
dlo.pb(odd.top());
odd.pop();
}
while(c--){
ans[even.top()].pb(bac[u]);
dle.pb(even.top());
even.pop();
}
for(auto i:dle) odd.push(i);
for(auto i:dlo) even.push(i);
u--;
t=fr;
}
fr1(i,1,res){
cout<<ans[i].size()<<' ';
for(auto j:ans[i]) cout<<j<<' ';
cout<<'\n';
}
}