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 ...
随机推荐
- CentOS 7网络故障
By francis_hao Nov 2,2017 在像往常一样打开了虚拟机后,打开xshell准备连接到centos,可是连不上,发现连接的网卡没有启动,使用systemctl启动netw ...
- dmsg命令使用
http://note.youdao.com/noteshare?id=6771284da9f10ac35652907898d63141
- 009.C++ const使用
1.引例 class complex { public: complex(, ) : re (r), im (i) {} complex& operator += (const complex ...
- array_unique后,数组本身的值并不会变
<?php $arr = [ ' ]; print_r($arr); print_r(array_unique($arr)); print_r($arr); //array_unique后,数组 ...
- zabbix 邮件配置
一.系统和版本 操作系统:centos7 zabbix版本: 3.2.5 二.安装sendmail yum -y install sendmail systemctl enable sendmail ...
- [机器学习]-Adaboost提升算法从原理到实践
1.基本思想: 综合某些专家的判断,往往要比一个专家单独的判断要好.在”强可学习”和”弱可学习”的概念上来说就是我们通过对多个弱可学习的算法进行”组合提升或者说是强化”得到一个性能赶超强可学习算法的算 ...
- [LeetCode] 26. Remove Duplicates from Sorted Array ☆
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- package.json文档
之前在博客中写过一篇关于 " node.js的安装配置 " 的文章,里面有提到利用 gulp watch 来监听文档的变化.其中需要 package.json 文件才能实现效果,所 ...
- vue 和react
React 和 Vue 有许多相似之处,它们都有: 使用 Virtual DOM 提供了响应式 (Reactive) 和组件化 (Composable) 的视图组件. 将注意力集中保持在核心库,而将其 ...
- HDU 1930 CRT
也是很模板的一道题,给出一些数,分割,模数固定是4个互质的. /** @Date : 2017-09-16 23:54:51 * @FileName: HDU 1930 CRT.cpp * @Plat ...