csu 1547(01背包)
1547: Rectangle
Time Limit: 1 Sec Memory Limit: 256 MB
Submit: 996 Solved: 277
[Submit][Status][Web Board]
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 题意:在 2*m 的矩形里面放若干宽度为1和2的矩形,问 m 最小要多大?
题解:首先,对于宽度为2的矩形,我们直接加上就好了,这是它所需要的容积,对于宽度为 1 的矩形,我们长度先对其排序,这样就保证了选的矩形尽可能的可以并排放下,对sum/2做01背包,得到容量为sum/2的
背包能够放的最大容量m,用sum-dp[sum/2]即为所需最小容量。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int x[],y[],v[];
int dp[];
int main()
{
int tcase,n;
scanf("%d",&tcase);
while(tcase--){
scanf("%d",&n);
int ans = ,cnt=,sum = ;
for(int i=;i<=n;i++){
scanf("%d%d",&x[i],&y[i]);
if(x[i]==){
ans+=y[i];
}else{
v[++cnt] = y[i];
sum+=v[cnt];
}
}
memset(dp,,sizeof(dp));
sort(v+,v+cnt+);
for(int i=;i<=cnt;i++){
for(int j=sum/;j>=v[i];j--){
dp[j] = max(dp[j],dp[j-v[i]]+v[i]);
}
}
int res = ans+sum-dp[sum/];
printf("%d\n",res);
} }
csu 1547(01背包)的更多相关文章
- 51 nod 1007 正整数分组 (简单01背包) && csu 1547: Rectangle
http://www.51nod.com/onlineJudge/questionCode.html#problemId=1007¬iceId=15020 求出n个数的和sum,然后用s ...
- CSU 1547 Rectangle(dp、01背包)
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1547 Description Now ,there are some rectang ...
- CSU 1547: Rectangle (思维题加一点01背包)
1547: Rectangle Submit Page Summary Time Limit: 1 Sec Memory Limit: 256 Mb Submitted: ...
- hdu 2602 Bone Collector(01背包)
题意:给出包裹的大小v,然后给出n块骨头的价值value和体积volume,求出一路下来包裹可以携带骨头最大价值 思路:01背包 1.二维数组(不常用 #include<iostream> ...
- UVALive 4870 Roller Coaster --01背包
题意:过山车有n个区域,一个人有两个值F,D,在每个区域有两种选择: 1.睁眼: F += f[i], D += d[i] 2.闭眼: F = F , D -= K 问在D小于等于一定限度的时 ...
- POJ1112 Team Them Up![二分图染色 补图 01背包]
Team Them Up! Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7608 Accepted: 2041 S ...
- Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)
传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...
- 51nod1085(01背包)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1085 题意: 中文题诶~ 思路: 01背包模板题. 用dp[ ...
- *HDU3339 最短路+01背包
In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
随机推荐
- [pool www] user has not been defined
[02-Dec-2014 00:28:58] ALERT: [pool www] user has not been defined [02-Dec-2014 00:28:58] ERROR: fai ...
- 前端基础----jquery
一.jQuery是什么? <1> jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team. <2>jQuery是继 ...
- oracle中的substr()函数
SUBSTR(cExpression,nStartPosition [,nCharactersReturned])其中,cExpression指定要从其中返回字符串的字符表达式或备注字段:nStart ...
- VS2010中使用Github进行版本控制【补充】
http://blog.csdn.net/softwave/article/details/8499264 前面我在文章<VS2010结合Github进行版本控制>中介绍了VS 2010中 ...
- wx.request 获取不到post传递的值
微信小程序的wx.request请求,method设为POST并向后台传递数据,但从后台返回的信息来看后台并没有获得传递的数据 wx.request({ url: 'url' ...
- libuv的多线程之间传递消息
官网上给出的例子http://nikhilm.github.io/uvbook/threads.html#inter-thread-communication,中文理解在后边 Inter-thread ...
- OpenCV---Numpy数组的使用以及创建图片
一:对头像的所有像素进行访问,并UI图像进行像素取反 (一)for循环取反 import cv2 as cv import numpy as np def access_pixels(image): ...
- thinkphp 5 where 组合条件map数组or
if($inviterId>0) { $arr = Db::table("tablename")-> where("pid=$inviterId") ...
- HDU 5700 优先队列(或者multiset) 或 线段树
题目大意:有n个区间,求k个区间,使得这k个区间相交的区间内数字之和最大.数列的数字均>=0 优先队列思路: 按照左端点sort,然后枚举左端点,假设他被覆盖过k次,然后用优先队列来维护最右端即 ...
- Codeforces 765F Souvenirs
time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standa ...