当前位置:网站首页>集合划分差最小问题(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输出路径。
边栏推荐
猜你喜欢
随机推荐
leetcode 48. Rotate Image 旋转图像(Medium)
智能电视可以打开小程序应用,再也不用头痛内存了
router---mode
基于 Next.js实现在线Excel
Unity插件:使用PopulationSystem制作行走交流的路人
物联网应用发展趋势
Kyushu Cloud attended the Navigator Online Forum to discuss the current status, challenges and future of 5G MEC edge computing
考研上岸又转行软件测试,从5k到13k完美逆袭,杭州校区小哥哥拒绝平庸终圆梦!
nVisual secondary development - Chapter 2 nVisual API operation guide Swagger use
开发者独立搭建一个跨模态搜索应用有多难?
解题-->在线OJ(十八)
metaRTC5.0新版本支持mbedtls(PolarSSL)
Convolutional Neural Network Basics
Utility function---string processing
开放麒麟 openKylin 版本规划敲定:10 月发布 0.9 版并开启公测,12 月发布 1.0 版
第四讲 SVN
1375. 二进制字符串前缀一致的次数-前序遍历法
自监督学习未来是掩码自编码器?KAIST最新《自监督学习掩码自编码器》研究进展
idea removes spark logs
CCF GLCC officially opened | Kyushu Cloud open source experts bring generous bonuses to help universities promote open source








