Storage Keepers - UVa 10163 dp
2014-08-06 01:36
387 查看
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 17
1 2
10 9
2 5
10 8 6 4 1
5 4
1 1 1 1
0 0
Sample Output
3 710 10
8 18
0 0
题意:找到最大的安全值,然后使得花费最小。
思路:先二分找到最大的安全值,然后dp[i][j]表示前i个人保管j个物品时的最小花费,然后递推。
AC代码如下:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int num[40],n,m,dp[40][110],INF=1000000000; int solve(int k) { if(k==0) return 10000; int ans=0,i; for(i=1;i<=m;i++) ans+=num[i]/k; return ans; } int main() { int i,j,k,l,r,mi; while(~scanf("%d%d",&n,&m) && n+m) { for(i=1;i<=m;i++) scanf("%d",&num[i]); sort(num+1,num+1+m); l=0;r=num[m]; while(l<r) { mi=(l+r+1)/2; if(solve(mi)>=n) l=mi; else r=mi-1; } if(l==0) { printf("0 0\n"); continue; } for(i=0;i<=m+1;i++) for(j=0;j<=n+1;j++) dp[i][j]=INF; dp[0][0]=0; for(i=1;i<=m;i++) { k=num[i]/l; for(j=n;j>=k;j--) dp[i][j]=min(dp[i][j+1],min(dp[i-1][j],dp[i-1][j-k]+num[i])); for(j=k;j>=0;j--) dp[i][j]=min(dp[i][j+1],min(dp[i-1][j],num[i])); } printf("%d %d\n",l,dp[m] ); } }
相关文章推荐
- uva 10163 Storage Keepers (dp)
- HDU 5445 Food Problem、UVa 10163 Storage Keepers、POJ 3260 The Fewest Coins(两次dp)
- DP(两次) UVA 10163 Storage Keepers
- UVA 10163 Storage Keepers 两次dp
- UVA 10163-Storage Keepers(DP)
- UVA-10163 Storage Keepers (DP多次)
- UVA 10163 Storage Keepers(两次DP)
- UVA 10163 Storage Keepers(两次DP)
- UVA 10163 - Storage Keepers(dp)
- 100道动态规划——13 UVA 10163 Storage Keepers 有约束条件下的DP,递推,不能使用结构体作为基本单位
- UVA 10163 - Storage Keepers(dp)
- UVA 10163 - Storage Keepers(dp)
- uva_10163 - Storage Keepers ( 普通DP )
- UVa 10163 Storage Keepers(两次DP)
- uva 10163 Storage Keepers (DP)
- UVA - 10163 - Storage Keepers(两次dp)
- UVA 10163 Storage Keepers
- UVA-10163 Storage Keepers DP
- UVA 10163 Storage Keepers(dp + 背包)
- UVA 10163 十六 Storage Keepers