UVA4731:Cellular Network

时间:2022-02-08 19:56:51

根据排序不等式可知,逆序和最小(就是两个向量坐标一个递增一个递减,那么乘起来就最小)

所以排一下序,然后做一下线性dp即可

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define MAXN 105
#define ll long long
using namespace std;
double f[MAXN][MAXN];
int n,m;
double c[MAXN];
double s[MAXN];
int a[MAXN];
int sum;
bool comp(const int &p1,const int &p2){
return (p1>p2);
}
void solve(){
scanf("%d%d",&n,&m);
sum=;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
sum+=a[i];
}
sort(a+,a+n+,comp);
for(int i=;i<=n;i++){
c[i]=1.0*a[i]/sum;
s[i]=s[i-]+c[i];
}
for(int i=;i<=n;i++){
f[i][]=i*s[i];
for(int j=;j<=m;j++){
f[i][j]=;
for(int k=j-;k<i;k++){
f[i][j]=min(f[i][j],f[k][j-]+(s[i]-s[k])*i);
}
}
}
printf("%.4f\n",f[n][m]);
}
int main()
{
// freopen("data.in","r",stdin);
int T;
scanf("%d",&T);
while(T--){
solve();
}
return ;
}