显然可以状压dp。显然过不了。
考虑暴力模拟退火。每次随机改变一个数所属集合即可。
并不明白要怎么调参。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
#define N 21
#define T0 1E5
#define T1 1E-6
#define v 0.996
int n,m,a[N],sum[N],id[N],size[N];
double ave,ans=;
void anneal()
{
for (int i=;i<=m;i++) id[i]=i,sum[i]=a[i],size[i]=;
for (int i=m+;i<=n;i++) id[i]=rand()%m+,sum[id[i]]+=a[i],size[id[i]]++;
double T=T0,tot=;
for (int i=;i<=m;i++) tot+=(sum[i]-ave)*(sum[i]-ave);
if (m<n)
while (T>T1)
{
int x=rand()%n+;
while (size[id[x]]==) x=rand()%n+;
int y=rand()%m+;
while (id[x]==y) y=rand()%m+;
if (rand()<exp((-2.0*a[x]*(a[x]-sum[id[x]]+sum[y]))/T)*RAND_MAX)
{
tot+=*a[x]*(a[x]-sum[id[x]]+sum[y]);
sum[y]+=a[x],sum[id[x]]-=a[x];
size[y]++,size[id[x]]--;
id[x]=y;
}
T*=v;
ans=min(ans,tot);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("bzoj2428.in","r",stdin);
freopen("bzoj2428.out","w",stdout);
const char LL[]="%I64d\n";
#else
const char LL[]="%lld\n";
#endif
srand();
n=read(),m=read();
for (int i=;i<=n;i++) ave+=a[i]=read();
ave/=m;
for (int i=;i<=;i++) anneal();
printf("%.2lf",sqrt(ans/m));
return ;
}