Uva--10163(动规,递推)
2014-08-25 10:02:20
| Problem C.Storage Keepers |
Background
Randy Company has N (1<=N<=100) storages. Company wants some men to keep them safe. Now there are M (1<=M<=30) men asking for the job. Company will choose several from them. Randy Company employs men following these rules:
1. Each keeper has a number Pi (1<=Pi<=1000) , which stands for their ability.
2. All storages are the same as each other.
3. A storage can only be lookd after by one keeper. But a keeper can look after several storages. If a keeper��s ability number is Pi, and he looks after K storages, each storage that he looks after has a safe number Uj=Pi div K.(Note: Uj, Pi and K are all integers). The storage which is looked after by nobody will get a number 0.
4. If all the storages is at least given to a man, company will get a safe line L=min Uj
5. Every month Randy Company will give each employed keeper a wage according to his ability number. That means, if a keeper��s ability number is Pi, he will get Pi dollars every month. The total money company will pay the keepers every month is Y dollars.
Now Randy Company gives you a list that contains all information about N,M,P, your task is give company a best choice of the keepers to make the company pay the least money under the condition that the safe line L is the highest.
Input
The input file contains several scenarios. Each of them consists of 2 lines:
The first line consists of two numbers (N and M), the second line consists of M numbers, meaning Pi (I=1..M). There is only one space between two border numbers.
The input file is ended with N=0 and M=0.
Output
For each scenario, print a line containing two numbers L(max) and Y(min). There should be a space between them.
Sample Input
2 1
7
1 2
10 9
2 5
10 8 6 4 1
5 4
1 1 1 1
0 0
Sample Output
3 7
10 10
8 18
0 0
思路:这题其实也可以归纳为变相的01背包,对于一个应聘的人选或不选,dp[i][j][k]表示前 i 个人管理 k 个仓库,使得safe线达到 j 的最小薪水和。
状态转移方程:dp[i][j][k + m] = min(dp[i - 1][j][k] + p[i] , dp[i - 1][j][k + m]) (k = p[i] / j,表示能加入第 i 个人达到 j 的safe线能多管多少个仓库)
1 /************************************************************************* 2 > File Name: k.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Fri 22 Aug 2014 09:23:15 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 const int INF = 1 << 30; 16 17 int N,M; 18 int p[35]; 19 int sum[35]; 20 int dp[35][1005][105]; 21 22 int main(){ 23 while(scanf("%d%d",&N,&M) == 2){ 24 if(!N && !M) break; 25 for(int i = 1; i <= M; ++i){ 26 scanf("%d",&p[i]); 27 sum[i] = (i == 1 ? p[i] : sum[i - 1] + p[i]); 28 } 29 int top = sum[min(N,M)] / N; 30 if(top == 0){ 31 printf("0 0\n"); 32 continue; 33 } 34 for(int i = 0; i <= M; ++i){ 35 for(int j = 0; j <= top; ++j){ 36 for(int n = 0; n <= N; ++n){ 37 dp[i][j][n] = INF; 38 } 39 dp[i][j][0] = 0; 40 } 41 } 42 for(int i = 1; i <= M; ++i) 43 for(int j = 1; j <= top; ++j) 44 for(int n = 0; n + p[i] / j <= N; ++n) 45 dp[i][j][n + p[i] / j] = min(dp[i - 1][j][n] + p[i],dp[i - 1][j][n + p[i] / j]); 46 47 for(int j = top; j >= 1; --j){ 48 if(dp[M][j][N] != INF){ 49 printf("%d %d\n",j,dp[M][j][N]); 50 break; 51 } 52 } 53 } 54 return 0; 55 }

浙公网安备 33010602011771号