标签:dp
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
#define N 1005
#define M 105
const int MAX = 0x3f3f3f;
int n, m, ans;
int dp[N], num[M];
int solve() { //先找出最低安全值的最大值
dp[0] = MAX;
for (int i = 0; i < n; i++) {
for (int j = m; j >= 0; j--) {
for (int k = 1; k <= j && k <= num[i]; k++) {
dp[j] = max(dp[j], min(dp[j - k], num[i] / k));
}
}
}
}
int find() {//通过上面函数找出的最低安全值的最大值,找出最低酬劳
if(ans == 0) return 0;
for (int i = 1; i <= m; i++)
dp[i] = MAX;
dp[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = m; j >= 0; j--) {
for (int k = min(j, num[i] / ans); k > 0; k--)
dp[j] = min(dp[j], dp[j - k] + num[i]);
}
}
}
int main() {
while (scanf("%d%d", &m, &n) == 2) {
memset(dp, 0, sizeof(dp));
if (m == 0 && n == 0) break;
for (int i = 0; i < n; i++) scanf("%d", &num[i]);
solve();
ans = dp[m];
find();
printf("%d %d\n", ans, dp[m]);
}
return 0;
}
uva 10163 Storage Keepers (DP)
标签:dp
原文地址:http://blog.csdn.net/llx523113241/article/details/45874023