51Nod 1007 正整数分组
来源:互联网 发布:淘宝数据包采集 编辑:程序博客网 时间:2024/06/06 02:18
将一堆正整数分为2组,要求2组的和相差最小。
例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1,是所有方案中相差最少的。
Input
第1行:一个数N,N为正整数的数量。第2 - N+1行,N个正整数。(N <= 100, 所有正整数的和 <= 10000)
Output
输出这个最小差
Input示例
512345
Output示例
1
#include<stdio.h>#include<algorithm>#include<string.h>#define N 10010using namespace std;int a[N]; int dp[N];int main(){ int n; int sum; while(scanf("%d",&n)!=EOF) { sum=0; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); sum+=a[i]; //挑选出一些数字,是的越靠近sum/2,那么就是背包问题了 } memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) { for(int j=sum/2; j>=a[i]; j--) //01背包问题 { dp[j]=max(dp[j],dp[j-a[i]]+a[i]); } } printf("%d\n",sum-dp[sum/2]-dp[sum/2]); } return 0;}
0 0
- 51nod 1007 正整数分组
- 51Nod 1007 正整数分组
- 51nod 1007 正整数分组
- 51nod-1007 正整数分组
- 51nod 1007 正整数分组
- 51Nod 1007 正整数分组
- 51nod 1007 正整数分组
- 51nod 1007正整数分组
- 51Nod 1007 正整数分组
- 51nod 1007 正整数分组
- 51Nod 1007 正整数分组
- 51nod 1007 正整数分组
- 【51nod】1007 正整数分组
- 51nod 1007 正整数分组
- 51nod 1007 正整数分组
- 51nod-正整数分组
- 51nod 正整数分组
- 51nod 正整数分组
- 守望先锋加载loading在Android上的实现
- HDU 2073 无限的路(水)
- 文章标题
- 顺序表的12种基本操作
- 抽象工厂模式
- 51Nod 1007 正整数分组
- spark2.0 tfidf
- 数学期望动态规划
- The 'Apple Push Notification' feature is only available to users enrolled in Apple Developer Program
- 文章标题
- Java 配置文件 .properties 读取
- C++ const对象详解
- 洛谷 P3398 仓鼠找sugar
- 李群、李代数在计算机视觉中的应用