Uva-562 Dividing coins (转换成01背包求解)
来源:互联网 发布:大数据与彩票 编辑:程序博客网 时间:2024/06/07 08:44
#include <bits/stdc++.h>using namespace std;int dp[30000], a[101];int main(){ int T; scanf("%d", &T); while(T--){ int n, tot = 0; scanf("%d", &n); memset(dp, 0, sizeof(dp)); for(int i = 1; i <= n; ++i){ scanf("%d", &a[i]); tot += a[i]; } for(int i = 1; i <= n; ++i){ for(int j = tot / 2; j >= a[i]; --j){ dp[j] = max(dp[j], dp[j - a[i]] + a[i]); } } printf("%d\n", tot - 2 * dp[tot / 2]); }}/*题意:将100个数分成两组,使得两组中数字的和之差的绝对值最小。思路:转变成01背包,设所有数的和为tot,我们假设有一个背包的容量为tot / 2,然后跑一遍背包,dp[i]表示背包容量为i时能取到的最大数和。这样dp[tot / 2]就是最优方案其中一组数的和,另一组就是tot - dp[tot / 2]。转移方程:dp[j] = max(dp[j], dp[j - a[i]] + a[i]),答案:tot - 2 * dp[tot / 2]。*/
阅读全文
0 0
- Uva-562 Dividing coins (转换成01背包求解)
- UVA 562 Dividing coins(dp + 01背包)
- uva 562 - Dividing coins(01背包)
- UVA 562 Dividing coins(01背包)
- UVA 562 Dividing coins(DP:01背包)
- UVA 562 Dividing coins 01背包 Commencel
- UVA 562 - Dividing coins(01背包)
- UVA - 562 Dividing coins 经典01背包
- UVA - 562 Dividing coins (01背包)
- UVA 562 Dividing coins 01背包
- uva 562 Dividing coins (01背包)
- UVA - 562 - Dividing coins (01背包)
- UVA 562Dividing coins 01背包
- UVA 562Dividing coins(01 背包问题)
- UVA - 562 Dividing coins(01背包)
- uva 562 Dividing coins 01背包
- UVA 562 Dividing coins(01背包)
- UVA-562 Dividing coins(01背包)
- 利用python对一段英文文本进行分词,分句
- 声反馈抑制使用matlab/simulink仿真
- 开始Python学习
- HTTP状态码:400\500 错误代码(个人总结)
- jenkins安装推荐插件
- Uva-562 Dividing coins (转换成01背包求解)
- Oracle自带的一些ASM维护工具 (kfod/kfed/amdu)
- Kubernetes是什么
- sql 连续两个月活跃的用户
- 笨方法学python 42课笔记:物以类聚
- Kotlin语言学习之when表达式使用
- Excel高效实战: 提高桌面生产力(系列06-10)
- VBoxManage: error: Nonexistent host networking interface, name 'vboxnet0' (VERR_INTERNAL_ERROR)
- got error 28 from storage engine mysql