当前位置:网站首页>集合划分差最小问题(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输出路径。
边栏推荐
- AlphaFold 如何实现 AI 在结构生物学中的全部潜力
- LeetCode 1403 Minimum subsequence in non-increasing order [greedy] HERODING's LeetCode road
- oracle+RAC+linux5.1所需要安装的包
- Analysis and application of portrait segmentation technology
- Lecture 4 SVN
- 如何确定异步 I/O 瓶颈
- Is the code more messy?That's because you don't use Chain of Responsibility!
- Fuse bit of AVR study notes
- [LeetCode] 38. Appearance sequence
- 基于 Next.js实现在线Excel
猜你喜欢
节省50%成本!京东云重磅发布新一代混合CDN产品
Is the code more messy?That's because you don't use Chain of Responsibility!
How to stress the MySQL performance indicators TPS\QPS\IOPS?
如何通过使用“缓存”相关技术,解决“高并发”的业务场景案例?
idea永久激活教程(新版)
如何才能有效、高效阅读?猿辅导建议“因材因时施教”
MySQL性能指标TPS\QPS\IOPS如何压测?
Interviewer: How to view files containing abc string in /etc directory?
Crawler - basic use of selenium, no interface browser, other uses of selenium, cookies of selenium, crawler cases
谁说 Mysql 单表最大 2000 W ?我硬要塞它 1 个亿
随机推荐
PAT甲级:1038 Recover the Smallest Number
工具函数---字符串处理
c#之winform(软件开发)
爬虫——动作链、xpath、打码平台使用
南瓜科学产品升级 开启益智探索新篇章
Crawler - basic use of selenium, no interface browser, other uses of selenium, cookies of selenium, crawler cases
阿里老鸟终于把测试用例怎么写说的明明白白了,小鸟必看
按键控制开关4017芯片数字电路
如何查找endnote文献中pdf文件的位置
Map common traversal methods - keySet and entrySet
SMART S7-200PLC串行自由口通讯(耐压测试仪)
How to install postgresql and configure remote access in ubuntu environment
华为手机切换屏幕效果_华为p40页面切换效果怎么换
烂大街的缓存穿透、缓存击穿和缓存雪崩,你真的懂了?
智能电视可以打开小程序应用,再也不用头痛内存了
"Social Enterprises Conducting Civilian Personnel Training Specifications" group standard on the shelves of Xinhua Bookstore
C# 动态加载卸载 DLL
基于 Next.js实现在线Excel
idea removes spark logs
Centos7 install mysql version rapidly