当前位置:网站首页>Set partition minimum difference problem (01 knapsack)
Set partition minimum difference problem (01 knapsack)
2022-08-04 14:14:00 【Harris-H】
Set partition minimum difference problem(01背包)
A sequence is divided into two sets,Minimize the difference between the sum of the two set numbers.
Consider a backpack,容量为 s u m 2 \dfrac{sum}{2} 2sum,转化为判定问题.
#include<bits/stdc++.h>
using namespace std;
const int N=5e3+5;
int n,s;
int a[N];
bool dp[N];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]),s+=a[i];
int m = s>>1;
dp[0] = true;
for(int i=1;i<=n;i++)
for(int j=m;j>=a[i];j--)
dp[j]|=dp[j-a[i]];
int ans = 0;
for(int i=m;i;i--){
if(dp[i]){
ans=s-(i<<1);
break;
}
}
printf("%d\n",ans);
return 0;
}
If you want to output the scheme,Just open another one p r e pre preArray records the number used,然后dfs输出路径.
边栏推荐
- 编程思想_编程有必要给孩子学吗?
- leetcode 48. Rotate Image 旋转图像(Medium)
- 人像分割技术解析与应用
- Centos7 install mysql version rapidly
- 让Web页面中的编辑器支持黏贴或直接拖拽来添加图片「建议收藏」
- 《中国综合算力指数》《中国算力白皮书》《中国存力白皮书》《中国运力白皮书》在首届算力大会上重磅发出
- ssm learning experience (final chapter)
- 并发刺客(False Sharing)——并发程序的隐藏杀手
- c#之winform(软件开发)
- How to find the location of a pdf file in endnote literature
猜你喜欢
开发者独立搭建一个跨模态搜索应用有多难?
开放麒麟 openKylin 版本规划敲定:10 月发布 0.9 版并开启公测,12 月发布 1.0 版
How to stress the MySQL performance indicators TPS\QPS\IOPS?
自监督学习未来是掩码自编码器?KAIST最新《自监督学习掩码自编码器》研究进展
阿里老鸟终于把测试用例怎么写说的明明白白了,小鸟必看
The Internet of things application development trend
token 过期后,如何自动续期?
《中国综合算力指数》《中国算力白皮书》《中国存力白皮书》《中国运力白皮书》在首届算力大会上重磅发出
解题-->在线OJ(十八)
zabbix自定义图形
随机推荐
Button control switch 4017 digital circuit chip
AlphaFold 如何实现 AI 在结构生物学中的全部潜力
第四讲 SVN
【无标题】
ssm学习心得(完结篇
Crawler - action chain, xpath, coding platform use
SQL语句的写法:Update、Case、 Select 一起的用法
Centos7 install mysql version rapidly
职场漫谈:为什么越是内卷的行业越有人争着抢着往里冲?好奇怪的说...
博途1200/1500PLC斜坡指令RAMP(带暂停功能)
记录都有哪些_js常用方法总结
从理论到实践:MySQL性能优化和高可用架构,一次讲清
router---dynamic route matching
电子行业MES管理系统有哪些特殊功能
基于 Next.js实现在线Excel
token 过期后,如何自动续期?
如何在ubuntu环境下安装postgresql并配置远程访问
大势所趋之下的nft拍卖,未来艺术品的新赋能
1375. 二进制字符串前缀一致的次数-前序遍历法
手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果