当前位置:网站首页>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输出路径.
边栏推荐
猜你喜欢

按键控制开关4017芯片数字电路
![[LeetCode] 38. Appearance sequence](/img/d6/092796b57844d5d30f3ed123a1b98a.png)
[LeetCode] 38. Appearance sequence

Execution failed for task ‘:xxx:generateReleaseRFile‘.

职场漫谈:为什么越是内卷的行业越有人争着抢着往里冲?好奇怪的说...

烂大街的缓存穿透、缓存击穿和缓存雪崩,你真的懂了?

"C pitfalls and pitfalls" reading summary

SLAM 04.视觉里程计-1-相机模型

如何通过使用“缓存”相关技术,解决“高并发”的业务场景案例?

考研上岸又转行软件测试,从5k到13k完美逆袭,杭州校区小哥哥拒绝平庸终圆梦!

How to stress the MySQL performance indicators TPS\QPS\IOPS?
随机推荐
两款移相振荡器的对比
漏洞复现 - - - Alibaba Nacos权限认证绕过
ssm学习心得(完结篇
idea永久激活教程(新版)
基于 Next.js实现在线Excel
Execution failed for task ‘:xxx:generateReleaseRFile‘.
自监督学习未来是掩码自编码器?KAIST最新《自监督学习掩码自编码器》研究进展
This article sorts out the development of the main models of NLP
1375. 二进制字符串前缀一致的次数-前序遍历法
人像分割技术解析与应用
C# winforms 输入颜色转换颜色名
Convolutional Neural Network Basics
leetcode 48. Rotate Image (Medium)
router---Programmatic navigation
没有Project Facets的解决方法
Crawler - action chain, xpath, coding platform use
AVR学习笔记之熔丝位
如何通过使用“缓存”相关技术,解决“高并发”的业务场景案例?
MySQL【窗口函数】【共用表表达式】
MySQL【触发器】