http://www.51nod.com/onlineJudge/questionCode.html#problemId=1007&noticeId=15020

求出n个数的和sum,然后用sum/2作为背包容量,让n个数去放,求出一个最大价值,那么这就是其中一组的和,另外一组的和就是sum-dp[sum/2];

注意这里的体积和价值都是a[i];

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1000000000
#define N 2510
#define mod 1000000000
using namespace std; int a[],dp[];
int main()
{
// freopen("a.txt","r",stdin);
int n,sum=,n1=;
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
n1=sum/;
memset(dp,,sizeof(dp));
for(int i=n-;i>=;i--)
for(int j=n1;j>=;j--)
{
if(j>=a[i])
{
dp[j]=max(dp[j],dp[j-a[i]]+a[i]);
}
}
//printf("%d\n",dp[n1]);
printf("%d\n",abs(sum-dp[n1]-dp[n1]));
return ;
}

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1547

上面这题的变形,把a==1的矩形长度累加起来,然后做一次01背包,就能得知放置这些宽度为1的矩形所需要的最小长度是多少,

但是要注意可能sum/2的背包可能放不下,那就需要取两个数的最大值,最后加上宽度为2的矩形长度即可。

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1000000000
#define N 2510
#define mod 1000000000
using namespace std;
int dp[];
int main()
{
//freopen("a.txt","r",stdin);
int t,n,a,b,c[],n1,n2;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
n1=n2=;
memset(dp,,sizeof(dp));
memset(c,,sizeof(c));
int k=;
for(int i=;i<n;i++)
{
scanf("%d%d",&a,&b);
if(a==) {n1+=b;c[k++]=b;}
n2+=b;
}
// printf("%d\n",k);
for(int i=;i<k;i++)
for(int j=n1/;j>=c[i];j--)
if(j>=c[i])
dp[j]=max(dp[j],dp[j-c[i]]+c[i]);
// printf("%d\n",dp[n1/2]);
printf("%d\n",max(n1-dp[n1/],dp[n1/])+n2-n1);
}
return ;
}

51 nod 1007 正整数分组 (简单01背包) && csu 1547: Rectangle的更多相关文章

  1. 51 Nod 1007 正整数分组【类01背包】

    1007 正整数分组 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 2 4分为1组, ...

  2. 51nod 1007 正整数分组【01背包变形】

    1007 正整数分组 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 ...

  3. 51Nod 1007 正整数分组(01背包)

    将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1,是所有方案中相差最少的. Input 第1行:一个数N,N为正整数的数量. ...

  4. 51Nod 1007:正整数分组(01背包)

    1007 正整数分组  基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 ...

  5. 51Nod 1007 正整数分组 -简单DP

    题意: 将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1,是所有方案中相差最少的. N<=100 sum<=100 ...

  6. 51 Nod 1007 dp

    1007 正整数分组 1 秒 131,072 KB 10 分 2 级题   将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1, ...

  7. 1007 正整数分组 1010 只包含因子2 3 5的数 1014 X^2 Mod P 1024 矩阵中不重复的元素 1031 骨牌覆盖

    1007 正整数分组 将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1,是所有方案中相差最少的.   Input 第1行:一个 ...

  8. POJ 3624 Charm Bracelet 简单01背包

    题目大意:有n件珠宝,每个珠宝的魅力值为v,重量为w,求在重量不超过m的情况下能达到的最大魅力值. 题目思路:简单的01背包,由于二维数组会超内存所以应该压缩成一维数组. dp[i][j],表示选取i ...

  9. 2、Charm Bracelet( poj 3624)简单0-1背包

    题意:有n件手镯,总重量不能超过M,每个手镯有一个体重W[i]和魅力V[i],问在不超过M的情况下能获得的魅力总和 思路:把M当背包总容量,用0-1背包写 代码: #include <iostr ...

随机推荐

  1. 面相切面编程AOP以及在Unity中的实现

    一.AOP概念 AOP(Aspect-Oriented Programming,面向切面的编程),它是可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术.它是 ...

  2. iOS开发 - CoreData框架 数据持久化

    Core Data Core Data是iOS5之后才出现的一个框架,它提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite数据库文件中,也能够将保存在数据库中的数据还 ...

  3. AJPFX:递归与非递归之间的转化

    在常规表达式求值中: 输入为四则运算表达式,仅由数字.+.-.*./ .(.) 组成,没有空格,要求求其值. 我们知道有运算等级,从左至右,括号里面的先运算,其次是* ./,再是+.- : 这样我们就 ...

  4. 工厂方法模式及php实现

    工厂方法模式: 工厂方法模式(Factory Method Pattern)又称为工厂模式,也叫虚拟构造器(Virtual Constructor)模式或者多态工厂(Polymorphic Facto ...

  5. poj3662 Telephone Lines

    思路: 二分+最短路.最短路也可以用来计算从a到达b所需的边权不超过x的边的数量. 实现: #include <cstdio> #include <cmath> #includ ...

  6. idea 调试工具的使用

    原文:https://blog.csdn.net/hao_hl1314/article/details/53120918 Intellij IDEA Debug调试区工具的使用方法 快捷键F9     ...

  7. Katalon Studio(二) 进阶战の Jenkins集成 analytics.katalon 集成

    本教程只针对Katalon Studio 与CI工具之一Jenkins的集成与脚本集的测试报告可视化简单操作. 1.新建一个job 2.新建一个自由风格的job 3.构建触发器 4.构建Windows ...

  8. CentOS 7 配置本地yum 源

    1. 加载 CentOS的ISO镜像并挂载: [root@localhost files]# mount /media/files/CentOS-7-x86_64-DVD-1611.iso /mnt/ ...

  9. servlet 生命周期 与 初始化

    一. 生命周期 Servlet 通过调用 init () 方法进行初始化. Servlet 调用 service() 方法来处理客户端的请求. Servlet 通过调用 destroy() 方法终止( ...

  10. discuz 插件核心函数hookscript分析.

    function hookscript($script, $hscript, $type = 'funcs', $param = array(), $func = '', $scriptextra = ...