According to the sort inequalities, the inverse order and the smallest (that is, two vector coordinates one increment one decrements, then multiply the smallest)
So arrange the order and then do a linear DP
#include <cstdio>#include<cstdlib>#include<algorithm>#include<cstring>#defineMAXN 105#definell Long Longusing namespacestd;DoubleF[MAXN][MAXN];intn,m;DoubleC[MAXN];DoubleS[MAXN];intA[MAXN];intsum;BOOLCompConst int&P1,Const int&p2) { return(p1>p2); }voidsolve () {scanf ("%d%d",&n,&m); Sum=0; for(intI=1; i<=n;i++) {scanf ("%d",&A[i]); Sum+=A[i]; } sort (A+1, a+n+1, comp); for(intI=1; i<=n;i++) {C[i]=1.0*a[i]/sum; S[i]=s[i-1]+C[i]; } for(intI=1; i<=n;i++) {f[i][1]=i*S[i]; for(intj=2; j<=m;j++) {F[i][j]=100000000000; for(intk=j-1; k<i;k++) {F[i][j]=min (f[i][j],f[k][j-1]+ (S[i]-s[k]) *i); }}} printf ("%.4f\n", F[n][m]);}intMain () {//freopen ("data.in", "R", stdin); intT; scanf ("%d",&T); while(t--) {solve (); } return 0;}
Uva4731:cellular Network