当前位置:网站首页>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输出路径.
边栏推荐
- [Niu Ke brush questions-SQL big factory interview questions] NO5. Analysis of a treasure store (e-commerce model)
- Makefile 语法及使用笔记
- 《C 陷阱与缺陷 》阅读概要
- 博途1200/1500PLC斜坡指令RAMP(带暂停功能)
- Is the code more messy?That's because you don't use Chain of Responsibility!
- 文盘Rust -- 配置文件解析
- TS - type
- 文字编码 - XML 教程
- 1375. 二进制字符串前缀一致的次数-前序遍历法
- word2003按空格键为什么会出现小数点
猜你喜欢
随机推荐
Kyushu Cloud attended the Navigator Online Forum to discuss the current status, challenges and future of 5G MEC edge computing
Rust from entry to proficient 04-variables
大势所趋之下的nft拍卖,未来艺术品的新赋能
华为手机切换屏幕效果_华为p40页面切换效果怎么换
State security organs conduct criminal arrest and summons review on Yang Zhiyuan, a suspect suspected of endangering national security
记录都有哪些_js常用方法总结
《中国综合算力指数》《中国算力白皮书》《中国存力白皮书》《中国运力白皮书》在首届算力大会上重磅发出
七夕当然要学会SQL优化好早点下班去找对象
FreeConfig.h文件
九州云出席领航者线上论坛,共话5G MEC边缘计算现状、挑战和未来
idea removes spark logs
四平方和,激光炸弹
Analysis and application of portrait segmentation technology
【毕设选题推荐】机器人工程专业毕设选题推荐
并发刺客(False Sharing)——并发程序的隐藏杀手
Oracle RAC环境下vip/public/private IP的区别
How to play the Tower of Hanoi
How to Identify Asynchronous I/O Bottlenecks
Various problems with npm install
错误 AttributeError type object 'Callable' has no attribute '_abc_registry' 解决方案








