CSU 1547 Rectangle(dp、01背包)
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1547
Description
Now ,there are some rectangles. The area of these rectangles is 1* x or 2 * x ,and now you need find a big enough rectangle( 2 * m) so that you can put all rectangles into it(these rectangles can't rotate). please calculate the minimum m satisfy the condition.
Input
There are some tests ,the first line give you the test number.
Each test will give you a number n (1<=n<=100)show the rectangles number .The following n rows , each row will give you tow number a and b. (a = 1 or 2 , 1<=b<=100).
Output
Each test you will output the minimum number m to fill all these rectangles.
Sample Input
2
3
1 2
2 2
2 3
3
1 2
1 2
1 3
Sample Output
7
4
Hint
Source
题意:
给你n个长方形(其中有宽为1的,也有宽为2的长方形),问你需要一个多大的宽为2的长方形才能将这些小长方形全部圈住(不能旋转长方形,即全部长方形为一个方向)。求最小m。
题解:
小长方形宽为2的时候 ans+= b, 直接加。所以我们只要讨论宽为1的小长方形。
全部的宽为1的长方形我们所要做的就是将它们分成长度尽可能接近的2堆,我们就需要用01背包来解决。
背包的容量为sum/2(sum为全部宽为1长方形的b的和),每一个长方形的价值为b,当然重量也为b
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
//#define LOCAL
#define eps 0.0000001
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = +;
const int mod = ;
int w[maxn];
int dp[maxn*maxn];
void solve()
{
ms(w, );
ms(dp, );
int n, a, b, ans=, sum = , cnt = ;
scanf("%d", &n);
for(int i=;i<n;i++){
scanf("%d%d", &a, &b);
if(a==) ans += b;
else w[++cnt] = b, sum+=b;
}
for(int i=;i<=cnt;i++){
for(int v = sum/; v>=w[i]; v--){
dp[v] = max(dp[v], dp[v-w[i]]+w[i]);
}
}
ans += max(dp[sum/], sum-dp[sum/]);
printf("%d\n", ans);
}
int main()
{
#ifdef LOCAL
freopen("jumping.in", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // LOCAL
int T;
scanf("%d", &T);
while(T--){
solve();
}
return ;
}
总结:
1)了解到了如果将一个数堆分成最接近的2堆,可以转变成01背包。
比赛时还是靠队友过了XD。
CSU 1547 Rectangle(dp、01背包)的更多相关文章
- CSU - 1547 Rectangle —— DP(01背包)
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1547 题解: 关键是怎么处理长度为1的长方形.当长度为1的长方形的个数cnt> ...
- USACO Money Systems Dp 01背包
一道经典的Dp..01背包 定义dp[i] 为需要构造的数字为i 的所有方法数 一开始的时候是这么想的 for(i = 1; i <= N; ++i){ for(j = 1; j <= V ...
- HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)
HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...
- POJ.3624 Charm Bracelet(DP 01背包)
POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...
- HDOJ(HDU).2546 饭卡(DP 01背包)
HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...
- HDOJ(HDU).2602 Bone Collector (DP 01背包)
HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...
- UVA.10130 SuperSale (DP 01背包)
UVA.10130 SuperSale (DP 01背包) 题意分析 现在有一家人去超市购物.每个人都有所能携带的重量上限.超市中的每个商品有其相应的价值和重量,并且有规定,每人每种商品最多购买一个. ...
- CSU 1547: Rectangle (思维题加一点01背包)
1547: Rectangle Submit Page Summary Time Limit: 1 Sec Memory Limit: 256 Mb Submitted: ...
- 51 nod 1007 正整数分组 (简单01背包) && csu 1547: Rectangle
http://www.51nod.com/onlineJudge/questionCode.html#problemId=1007¬iceId=15020 求出n个数的和sum,然后用s ...
随机推荐
- 【AndroidFramework】【EMMC拷机】混合拷机时盒子待机
[AndroidFramework][EMMC拷机]混合拷机时盒子待机 问题结论 暂时确定为误按键,不是故障.问题关闭.后续完全保证无按键(物理隔离)情况下如果出现待机,请反馈. 问题描述 [EMMC ...
- django shell的基本使用
作者:python技术人 博客:https://www.cnblogs.com/lpdeboke/ 在日常工作再发中,经常需要测试一些对象.函数.类...等是否正确,但是如果整体运行项目特别麻烦,并且 ...
- <每日一题> Day6:HDU递推专题完结
原题链接 这是我自己Clone的专题,A,B题解昨天发过了 C:参考代码: /* 很容易我们可以手推出n = 1, 2, 3时的情况,我们假设前n - 1 列已经放好,方法有dp[n - 1]种,第n ...
- java_第一年_JavaWeb(10)
JavaWeb的两种开发模式 JSP+JavaBean框架:JavaBean负责封装数据.提供方法,JSP负责处理用户请求和显示数据:只能开发较为简单的业务: JSP+JavaBean+Servlet ...
- python字符串的运算有哪些
python字符串的运算有哪些 1,链接符号 + 2,判断字符串是否在某个字符串中 ‘s’ in ‘this’ 返回bool 3,字符串索引 a="this a my" a[0], ...
- CSS文字,文本,背景,盒模型等记录
文字: font-family:" "; /*设置字体*/ font-size:6px;/*设置文字字号*/ color:red;/*设置文字颜色*/ font-weight:bo ...
- vue中对于图片是否正常加载的思考
问题:由于业务需要,我们需要判断图片能否正常的加载,如果未正常加载的话,需要显示一张默认图片: 方案:1,由于后台返回的是一个图片id数组,例如 imgList=['343313131','21333 ...
- document.body.scrollTop值为0的解决方法[转]
做页面的时候可能会用到位置固定的层,读取document.body.scrollTop来设置层的位置,像这样, window.onscroll=function () { ...
- Python之文件的读
python中文件的读操作:以只读的形式打开文件->逐行读取文件中的内容->关闭文件 代码如下 #文件的读 f = file(u'F:\\python\\homework.txt', 'r ...
- nbench
http://www.math.utah.edu/~mayer/linux/bmark.html nbench-byte-2.2.3 --> http://www.math.utah.edu/ ...