The main topic: There are N (1<=n<=100) warehouses need to be supervised, there are M (1<=m<=30) candidates, each has the ability to attribute Pi (1<=pi<=1000). All warehouses are the same, each warehouse can only be guarded by one person, one can guard multiple warehouses, when a person guarding U warehouse, each warehouse security is uj=pi/u, the total safety is min Uj. It takes a PI element to hire a person with a competency value of pi. The maximum total safety, and the minimum cost in such cases.
First DP once, to find out the possible maximum total security Max, again DP, to find the minimum cost of security for Max.
First DP: Using D[I][J] to indicate the maximum total safety that can be combined with the I personal Guard J Warehouse, using A[i] to represent the ability value of the individual.
State transition equation: D[i][j]=max {d[i-1][j],min {d[i-1][j-u],a[i]/u}} (A[i]/u>d[i-1][j] && u>0)
The second DP: with D[I][J] for the I personal Guard J Warehouse and the total security is not less than max when the minimum cost, can be greedy thinking optimization, only take a[i]>=max of the person, in DP when the most direct consideration of each person he can control the warehouse (A[i]/max).
State transition equation: d[i][j]=min {d[i-1][j],d[i-1][j-a[i]/max]+a[i]}
#include <stdio.h> #include <stdlib.h>int a[50];int d[50][110];int co[50][110];int Main (void) {int i,j,u,v, P,N,M,MINP,MAX;SCANF ("%d%d", &n,&m); while ((n!=0) | | (m!=0)) {for (i=1;i<=m;i++) {scanf ("%d", &a[i]);} for (i=1;i<m;i++) {for (j=m-1;j>=i;j--) {if (a[j]<a[j+1]) {p=a[j];a[j]=a[j+1];a[j+1]=p;}}} for (i=1;i<=n;i++) {d[1][i]=a[1]/i;} D[1][0]=10000000;for (i=2;i<=m;i++) {d[i][0]=10000000;for (j=1;j<=n;j++) {u=d[i-1][j];for (v=1;v<=j;v++) { if (a[i]/v<=u) {break;} if (A[i]/v>d[i-1][j-v]) {minp=d[i-1][j-v];} else{minp=a[i]/v;} if (minp>u) {u=minp;}} D[i][j]=u;}} Max=d[m][n];if (max==0) {printf ("0 0\n");} Else{p=m;for (i=1;i<=m;i++) {if (A[i]<max) {p=i-1;break;}} for (i=1;i<=n;i++) {if (A[1]/i>=max) {co[1][i]=a[1];} else{co[1][i]=10000000;}} for (i=2;i<=p;i++) {for (j=1;j<=n;j++) {minp=co[i-1][j];u=a[i]/max;if (u>=j) {if (A[I]<MINP) {minp=a[i];}} Else{if (CO[I-1][J-U]+A[I]<MINP) {minp=co[i-1][j-u]+a[i];}} CO[I][J]=MINP;}} printf ("%d%d\n", Max,co[p][n]);} scanf ("%d%d",&N,&M);} return 0;}
UVA 10163-storage Keepers (DP)