当前位置:网站首页>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输出路径.
边栏推荐
猜你喜欢
随机推荐
js深拷贝和浅拷贝具体使用区别_es6深拷贝和浅拷贝
如何查找endnote文献中pdf文件的位置
c#之winform(软件开发)
CCF GLCC正式开营|九州云开源专家携丰厚奖金,助力高校开源推广
MySQL【触发器】
idea永久激活教程(新版)
卷积神经网络 基础
SLAM 05.视觉里程计-2-特征法
"Social Enterprises Conducting Civilian Personnel Training Specifications" group standard on the shelves of Xinhua Bookstore
VBS函数应用–getobject的使用获得Automation对象
化繁为简,聊一聊复制状态机系统架构抽象
Analysis and application of portrait segmentation technology
vcl啥意思_oval
秋招攻略秘籍,吃透25个技术栈Offer拿到手软
按键控制开关4017芯片数字电路
Button control switch 4017 digital circuit chip
PAT甲级:1038 Recover the Smallest Number
MySQL性能指标TPS\QPS\IOPS如何压测?
文字编码 - Markdown 简明教程
开放麒麟 openKylin 版本规划敲定:10 月发布 0.9 版并开启公测,12 月发布 1.0 版









