当前位置:网站首页>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输出路径.
边栏推荐
猜你喜欢

爬虫——selenium基本使用、无界面浏览器、selenium的其他用法、selenium的cookie、爬虫案例

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

基于 Next.js实现在线Excel

Redis 复习计划 - Redis主从数据一致性和哨兵机制

How to stress the MySQL performance indicators TPS\QPS\IOPS?

Centos7 install mysql version rapidly

砺夏行动|九州云章津楠:开源不是少数人的运动,大众化才是源泉

开放麒麟 openKylin 版本规划敲定:10 月发布 0.9 版并开启公测,12 月发布 1.0 版
将 Sentinel 熔断限流规则持久化到 Nacos 配置中心

如何通过使用“缓存”相关技术,解决“高并发”的业务场景案例?
随机推荐
没有Project Facets的解决方法
爬虫——动作链、xpath、打码平台使用
"C pitfalls and pitfalls" reading summary
VBS函数应用–getobject的使用获得Automation对象
从理论到实践:MySQL性能优化和高可用架构,一次讲清
Cockpit human-computer interaction "undercurrent", voice "down", multi-modal "up"
人像分割技术解析与应用
Problem solving-->Online OJ (18)
基于 Next.js实现在线Excel
Is there a replacement for the LM2596?LM2576 can
节省50%成本!京东云重磅发布新一代混合CDN产品
编程思想_编程有必要给孩子学吗?
php中的ceil和floo以及round函数「建议收藏」
Execution failed for task ‘:xxx:generateReleaseRFile‘.
Crawler - action chain, xpath, coding platform use
《社会企业开展应聘文职人员培训规范》团体标准在新华书店上架
Utility function---string processing
centos7安装mysql急速版
Analysis and application of portrait segmentation technology
MPLS实验