51 nod 1007 正整数分组 (简单01背包) && csu 1547: Rectangle
http://www.51nod.com/onlineJudge/questionCode.html#problemId=1007¬iceId=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的更多相关文章
- 51 Nod 1007 正整数分组【类01背包】
1007 正整数分组 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 2 4分为1组, ...
- 51nod 1007 正整数分组【01背包变形】
1007 正整数分组 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 ...
- 51Nod 1007 正整数分组(01背包)
将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1,是所有方案中相差最少的. Input 第1行:一个数N,N为正整数的数量. ...
- 51Nod 1007:正整数分组(01背包)
1007 正整数分组 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 ...
- 51Nod 1007 正整数分组 -简单DP
题意: 将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1,是所有方案中相差最少的. N<=100 sum<=100 ...
- 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, ...
- 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行:一个 ...
- POJ 3624 Charm Bracelet 简单01背包
题目大意:有n件珠宝,每个珠宝的魅力值为v,重量为w,求在重量不超过m的情况下能达到的最大魅力值. 题目思路:简单的01背包,由于二维数组会超内存所以应该压缩成一维数组. dp[i][j],表示选取i ...
- 2、Charm Bracelet( poj 3624)简单0-1背包
题意:有n件手镯,总重量不能超过M,每个手镯有一个体重W[i]和魅力V[i],问在不超过M的情况下能获得的魅力总和 思路:把M当背包总容量,用0-1背包写 代码: #include <iostr ...
随机推荐
- React.js 基本环境安装
安装 React.js React.js 单独使用基本上是不可能的事情.不要指望着类似于 jQuery 下载放到 <head /> 标签就开始使用.使用 React.js 不管在开发阶段生 ...
- Objective -C Object initialization 对象初始化
Objective -C Object initialization 对象初始化 1.1 Allocating Objects 分配对象 Allocation is the process by w ...
- was--创建概要文件(典型)
1.第一步 2 .创建 3.创建 4 .典型 5 下一步 6 下一步 7.下一步 8.输入用户和密码,下一步 9.下一步 10.下一步 11.下一步 12.下一步 13.下一步 14.创建 ...
- 临时笔记 Protection
如果操作系统不使用处理器的多任务机制,它仍然需要为栈创建至少一个TSS 当程序通过调用门改变特权级的时候,处理器执行下面的步骤切换栈,并且执行被调用的程序在新的特权级 1. 使用目标代码段的DPL从T ...
- ActiveSync日志分析
Here are the steps to exporting the ActiveSync log data: Create a folder on the root of C:\ ...
- 《哈佛商业评论》2017年第5期:4星。成功CEO具有4种行为特质:果断、激励参与、主动适应、稳扎稳打。股东价值最大化的理念有重大缺陷。
老牌管理学杂志,每期都值得精度.本期几个比较重要的观点:谦逊的CEO能带来更好的业绩:飞利浦创新过度导致业绩下滑:股东最大化的理念有重大缺陷,后果之一是大宗股票的临时持有者可能干预公司事务,强迫公司采 ...
- java 文件另存为
FileUtils.copyFile(new File(), new File());
- Java基础(十四)--装箱、拆箱详解
Java中基本数据类型都有相对应的包装类 什么是装箱?什么是拆箱? 在Java SE5之前,Integer是这样初始化的 Integer i = new Integer(10); 而在从Java SE ...
- js使用canvas在前端压缩图片
HTML代码: <input id="file" type="file"> JS代码: var eleFile = document.querySe ...
- React入门介绍(2)- React Component-React组件
React Component-React组件 允许用户自由封装组件是React非常突出的特性,用户可将自己创建的组件像普通的HTML标签一样插入页面,React.CreateClass方法就是用来创 ...