## 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;
}


#### 01背包问题（DP解决）

2016-09-03 21:07:39

#### 01背包 ,完全背包,多重背包 dp (动态规划入门dp)

2017-04-12 22:44:42

#### DP——01背包问题使用迭代和动态规划（超详细——小白入门）

2017-12-19 13:57:40

#### 01背包（要求恰好装满）

2017-11-18 15:47:19

#### DP背包之01背包、完全背包、多重背包笔记

2013-12-23 13:32:36

#### DP背包问题小结(01背包，完全背包，需恰好装满或不需，一维DP、二维DP)

2016-03-17 22:53:17

#### HDU 2126（01背包扩展，记录方案数）

2016-02-11 23:01:29

#### 2017百度之星资格赛 hdu6083 度度熊的午饭时光 （01背包+字典序路径）

2017-08-07 18:20:55

#### 程序猿(媛)和它(她)的双肩包

2016-02-29 22:39:09

#### 01背包的两种初始化问题

2017-07-25 09:07:04

## 不良信息举报

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