CF730J Bottles

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;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值