# uva--10163(dp,01背包，双肩包）

478人阅读 评论(0)

## 10163 Storage Keepers

##### 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  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 = minUj 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

想到了分成两个子问题，先找出最大的安全值L，再去确定达到L所需要的最小花费，一般既满足什么又得怎么样的时候都得分成两个子问题才行，以前也有个题，说什么长度不小于多少的最大子串和，当时也是分成两个子问题来的，先找最大子串和，然后再DP一次保证长度大于多少，扯远了……但是自己也想到了是背包，可是开始自己胡乱定义状态，明明都想到了背包这种经典模型，为何不直接套用方程呢，自己定义的状态乱七八糟根本不对。
求这两个子问题的时候都用到了01背包，看网上的名字也很可爱，叫双肩包~
求解L：
dp[j]=max( min(dp[j-k],man[i]/k , dp[j]); 选和不选两种情况，k是这个人选择的保护的个数，我现在只能先写成二维的再改成滚动数组，要不不知怎的现在还是一下子没法写出来。
求解花费：
dp[j]=min( dp[j-k]+man[i] , dp[j] ) ; k的范围就是man[i]/safe了，这样既满足了第一个条件，又能求出最小的花费。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<cstring>
#include<vector>
#include<algorithm>
#define INF 0X3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;

typedef long long ll;
typedef unsigned long long llu;
const int maxd=30+5;
const int maxn=100+5;
//==========================
int man[maxd];
int n,m;

bool cmp(int a,int b)
{
return a>b;
}

int get_L()
{
int dp[maxn];
mem(dp,0);
dp[0]=INF;
for(int i=1; i<=m; ++i)
for(int j=n; j>=0; --j)
for(int k=1; k<=j && man[i]>=k; ++k)
dp[j]=max( min(dp[j-k],man[i]/k) , dp[j] );
return dp[n];
}

int get_w(int safe)
{
if(safe==0) return 0;
int dp[maxn];
mem(dp,INF);
dp[0]=0;
for(int i=1; i<=m; ++i)
for(int j=n; j>0; --j)
{
int cnt=man[i]/safe;
for(int k=min(j,cnt); k>0; --k)
{

dp[j]=min(dp[j-k]+man[i],dp[j]);
}
}
return dp[n];
}

int main()
{
freopen("1.txt","r",stdin);
while(scanf("%d%d",&n,&m)==2 )
{
if(n==0 && m==0)
break;
for(int i=1; i<=m; ++i)
scanf("%d",&man[i]);
sort(man+1,man+m+1,cmp);
int L=get_L();
printf("%d %d\n",L,get_w(L));
}

return 0;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：17581次
• 积分：538
• 等级：
• 排名：千里之外
• 原创：38篇
• 转载：1篇
• 译文：0篇
• 评论：0条
文章分类
评论排行