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

秋招攻略秘籍,吃透25个技术栈Offer拿到手软

AVR学习笔记之熔丝位
![LeetCode 1403 Minimum subsequence in non-increasing order [greedy] HERODING's LeetCode road](/img/fd/c827608b96f678a67c7e920c51d8c5.png)
LeetCode 1403 Minimum subsequence in non-increasing order [greedy] HERODING's LeetCode road

Execution failed for task ‘:xxx:generateReleaseRFile‘.

Interviewer: Tell me the difference between NIO and BIO

卷积神经网络 基础

Lixia Action | Kyushu Yunzhang Jinnan: Open source is not a movement for a few people, popularization is the source

Theory 1: Deep Learning - Detailed Explanation of the LetNet Model

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

centos7安装mysql急速版
随机推荐
How to install postgresql and configure remote access in ubuntu environment
metaRTC5.0新版本支持mbedtls(PolarSSL)
FreeConfig.h文件
博途1200/1500PLC斜坡指令RAMP(带暂停功能)
《中国综合算力指数》《中国算力白皮书》《中国存力白皮书》《中国运力白皮书》在首届算力大会上重磅发出
Oracle RAC环境下vip/public/private IP的区别
SLAM 05.视觉里程计-2-特征法
企业应当实施的5个云安全管理策略
VBS函数应用–getobject的使用获得Automation对象
MPLS实验
橄榄枝大课堂APP正式启动上线
odoo15 大部分模块都用的附件整理成一独立模块
职场漫谈:为什么越是内卷的行业越有人争着抢着往里冲?好奇怪的说...
解题-->在线OJ(十八)
router---dynamic route matching
PAT甲级:1040 Longest Symmetric String
php中的ceil和floo以及round函数「建议收藏」
没有Project Facets的解决方法
Problem solving-->Online OJ (18)
Convolutional Neural Network Basics