当前位置:网站首页>集合划分差最小问题(01背包)
集合划分差最小问题(01背包)
2022-08-04 14:09:00 【Harris-H】
集合划分差最小问题(01背包)
一个序列分为两个集合,使得两个集合数的和差值最小。
考虑背包,容量为 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;
}
如果要输出方案,就再开一个 p r e pre pre数组记录使用的数,然后dfs输出路径。
边栏推荐
猜你喜欢
LM2596有没有可以替代的?LM2576可以
"C pitfalls and pitfalls" reading summary
谁说 Mysql 单表最大 2000 W ?我硬要塞它 1 个亿
【毕设选题推荐】机器人工程专业毕设选题推荐
如何查找endnote文献中pdf文件的位置
MPLS experiment
Theory 1: Deep Learning - Detailed Explanation of the LetNet Model
理论篇1:深度学习之----LetNet模型详解
职场漫谈:为什么越是内卷的行业越有人争着抢着往里冲?好奇怪的说...
nVisual二次开发——第二章 nVisual API操作指南Swagger使用
随机推荐
Execution failed for task ‘:xxx:generateReleaseRFile‘.
ACL 2022 | 社会科学理论驱动的言论建模
如何通过使用“缓存”相关技术,解决“高并发”的业务场景案例?
How to find the location of a pdf file in endnote literature
基于 Next.js实现在线Excel
SLAM 05.视觉里程计-2-特征法
并发程序的隐藏杀手——假共享(False Sharing)
Various problems with npm install
JSX use
Interviewer: How to view files containing abc string in /etc directory?
CCF GLCC officially opened | Kyushu Cloud open source experts bring generous bonuses to help universities promote open source
Lecture 4 SVN
南瓜科学产品升级 开启益智探索新篇章
中大型商业银行堡垒机升级改造就用行云管家!必看!
九州云出席领航者线上论坛,共话5G MEC边缘计算现状、挑战和未来
Analysis and application of portrait segmentation technology
Map common traversal methods - keySet and entrySet
数据库的基本概念
谁说 Mysql 单表最大 2000 W ?我硬要塞它 1 个亿
word2003按空格键为什么会出现小数点