01背包问题
题目链接
题目思路
先排序贪心找到需要的最少瓶子 再01背包求求所需瓶子内本来的水的最大值即可
因为总水量是一定的 如果最后方案瓶子中原来的水越多 则从外面往里倒的水越少即所需时间越少
我们可以用 f[m][k] 来表示 总容量为m 已经用了k个瓶子时的最大Vnomove
则有 f[j][k] = max(f[j][k], f[j - b[i]][k - 1] + a[i])
ac代码
#include <bits/stdc++.h>
using namespace std;
const int N = 110, M = 10010, INF = 1e6;
int n;
int suma, sumb;
int f[M][N];
struct Node
{
int a, b;
bool operator < (const Node &W)const
{
return b > W.b;
}
}c[N];
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i ++ ) scanf("%d", &c[i].a), suma += c[i].a;
for (int i = 1; i <= n; i ++ ) scanf("%d", &c[i].b);
int cnt = 0;
sort(c + 1, c + n + 1);
while (sumb < suma) sumb += c[ ++ cnt].b;
printf("%d ", cnt);
memset(f, -INF, sizeof f);
f[0][0] = 0;
for (int i = 1; i <= n; i ++ )
for (int j = sumb; j >= c[i].b; j -- )
for (int k = 1; k <= cnt; k ++ )
f[j][k] = max(f[j][k], f[j - c[i].b][k - 1] + c[i].a);
int ans = -1;
for (int i = suma; i <= sumb; i ++ ) ans = max(ans, f[i][cnt]);
printf("%d\n", suma - ans);
return 0;
}