题目:
![](https://i-blog.csdnimg.cn/blog_migrate/8fd73e9de584baa78aa326d263da18db.png)
思路:
贪心不行
原想法是,连续把用时少的脑子加上当前用时最多的题目,,全错!
其实这道题可以用背包的思路,
核心:两个脑子相差的时间越少越好
于是可以把,某科目题目用时总和的一半当作背包最大容量,
当然时间就是限制条件
用普通背包状态转移方程求即可
代码:
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int s1, s2, s3, s4;
int a[25], b[25], c[25], d[25];
int result[4];
int dp[2000];
void guihua(int s, int u[],int i) {
int num=0, t=0;
if (s == 1) {
result[i] = u[1];
return;
}
if (s == 2) {
result[i] = max(u[1], u[2]);
return;
}
if (s > 2) {
for (int j = 1; j <= s; j++) {
num += u[j];
}
t = num / 2;
for (int j = 0; j <= t; j++)
{
dp[j] = 0;
}
for (int j = 1; j <= s; j++) {
for (int k = t; k >= u[j]; k--) {
dp[k] = max(dp[k], dp[k - u[j]] + u[j]);
}
}
result[i] = num - dp[t];
return;
}
}
int main() {
cin >> s1>>s2>>s3>>s4;
for (int i = 1; i <= s1; i++) {
cin >>a[i];
}
for (int i = 1; i <= s2; i++) {
cin >> b[i];
}
for (int i = 1; i <= s3; i++) {
cin >> c[i];
}
for (int i = 1; i <= s4; i++) {
cin >> d[i];
}
guihua(s1, a, 0);
guihua(s2, b, 1);
guihua(s3, c, 2);
guihua(s4, d, 3);
cout << result[0] + result[1] + result[2] + result[3];
return 0;
}