P2392 考前临时抱佛脚(变形背包)

P2392 考前临时抱佛脚

P2392 考前临时抱佛脚

在这里插入图片描述
以为是贪心问题结果全wa
思路
我们需要使耗时尽可能接近 t / 2, 其实就是一个0-1背包问题,体积和价值都是时间,背包的体积为t / 2,可求出在t / 2的时间内最短的耗时时间,那么总耗时即为max(v, t - v);

样例输入:

1 2 1 3		
5
4 3
6
2 4 3

样例输出:

20

代码:

#include <iostream>
#include <cstring>
using namespace std;

const int N = 10005;

int f[N];
int res;
int s[5], a[N];
int check(int u, int v)
{
	memset(f, 0, sizeof f);
	
	for(int i = 1; i <= s[u]; i ++ )
	{
		for(int j = v / 2; j >= 0; j -- )
			if(j - a[i] >= 0) f[j] = max(f[j], f[j - a[i]] + a[i]);
	}
	
	return max(f[v / 2], v - f[v / 2]);
}

int main()
{
	for(int i = 1; i <= 4; i ++ )
		cin >> s[i];
		
	for(int i = 1; i <= 4; i ++ )
	{
		int sum = 0;
		for(int j = 1; j <= s[i]; j ++ )
			cin >> a[j], sum += a[j];
			
		res += check(i, sum);
	}
	
	cout << res << endl;
	
	return 0;
}

 

posted @ 2022-03-24 09:28  panse·  阅读(33)  评论(0)    收藏  举报