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. webform 基础一

    WebForm是微软开发的一款产品,它将用户的请求和响应都封装为控件.让开发者认为自己是在操作一个windows界面.极大地提高了开发效率.区别于dreamweaver,可以用代码写,也可以把控件像w ...

  2. Spring框架学习-搭建第一个Spring项目

    步骤一:下载Spring开发包. 官网:https://spring.io/           下载地址:https://repo.spring.io/libs-release-local/org/ ...

  3. IOS 中使用token机制来验证用户的安全性

    登录的业务逻辑{    http:是短连接.         服务器如何判断当前用户是否登录?    // 1. 如果是即时通信类:长连接.    // 如何保证服务器跟客户端保持长连接状态? // ...

  4. R in action读书笔记(16)第十二章 重抽样与自助法之 置换检验

    第十二章:重抽样与自助法 本章,我们将探究两种应用广泛的依据随机化思想的统计方法:置换检验和自助法 12.1 置换检验 置换检验,也称随机化检验或重随机化检验. 有两种处理条件的实验,十个受试者已经被 ...

  5. Javascript IE 内存释放

    一个内存释放的实例 <SCRIPT LANGUAGE="JavaScript"><!--strTest = "1";for ( var i = ...

  6. SQL转Java代码小工具

    工作中使用SQL的时候很多,当使用hibernate的时候,经常遇到多行的SQL,通常在PL/SQL或其他地方写好SQL,测试没问题后,需要将SQL写到程序代码中,多行SQL需要拼接字符串,手动一行行 ...

  7. win7 快捷键 收集

    1. 轻松访问键盘快捷方式 按住右 Shift 八秒钟:启用和关闭筛选键 按左 Alt + 左 Shift + PrtScn (或 PrtScn):启用或关闭高对比度 按左 Alt + 左 Shift ...

  8. 洛谷 P3371 【模板】单源最短路径(堆优化dijkstra)

    题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入输出格式 输入格式: 第一行包含三个整数N.M.S,分别表示点的个数.有向边的个数.出发点的编号. 接下来M行每行包含三 ...

  9. 四则运算 来自 http://www.cnblogs.com/ys1101/p/4368103.html

    #include<stdio.h> #include<math.h> #include<windows.h> ; ; void add() { int a,b,c, ...

  10. OpenFlow_tutorial_3_Learn_Development_Tools

    一.Several Utilities OpenFlow Tutorial VM 中预装了一些OpenFlow特性的工具和一般通用网络的工具. 1.Openflow Controller:处于Open ...