当前位置:网站首页>集合划分差最小问题(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输出路径。
边栏推荐
猜你喜欢

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

开发者独立搭建一个跨模态搜索应用有多难?

企业应当实施的5个云安全管理策略

Kyushu Cloud attended the Navigator Online Forum to discuss the current status, challenges and future of 5G MEC edge computing

量化细胞内的信息流:机器学习时代下的研究进展

MySQL【窗口函数】【共用表表达式】

idea永久激活教程(新版)

并发程序的隐藏杀手——假共享(False Sharing)

按键控制开关4017芯片数字电路

Execution failed for task ‘:xxx:generateReleaseRFile‘.
随机推荐
idea永久激活教程(新版)
九州云出席领航者线上论坛,共话5G MEC边缘计算现状、挑战和未来
电子行业MES管理系统有哪些特殊功能
1375. 二进制字符串前缀一致的次数-前序遍历法
Unity插件:使用PopulationSystem制作行走交流的路人
word2003按空格键为什么会出现小数点
Interviewer: Tell me the difference between NIO and BIO
router---dynamic route matching
Utility function---string processing
字符串类的设计与实现_C语言字符串编程题
PAT甲级:1038 Recover the Smallest Number
ssm学习心得(完结篇
How to Identify Asynchronous I/O Bottlenecks
Kyushu Cloud attended the Navigator Online Forum to discuss the current status, challenges and future of 5G MEC edge computing
数据库的基本概念
第六届未来网络发展大会,即将开幕!
浙江大学团队使用基于知识图谱的新方法,从空间分辨转录组数据中推断细胞间通信状况
信创是什么意思?涉及哪些行业?为什么要发展信创?
How to install postgresql and configure remote access in ubuntu environment
leetcode 48. Rotate Image 旋转图像(Medium)