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 ...
随机推荐
- BZOJ1367 [Baltic2004]sequence 【左偏树】
题目链接 BZOJ1367 题解 又是一道神题,, 我们考虑一些简单的情况: 我们先假设\(b_i\)单调不降,而不是递增 对于递增序列\(\{a_i\}\),显然答案\(\{b_i\}\)满足\(b ...
- 关于JBoss基本说明文档及基本使用安装
关于JBoss JBoss是全世界开发者共同努力的成果,一个基于J2EE的开放源代码的应用服务器.在不 到12个月的时间里有一百万以上的拷贝被下载.JBoss是第一位的J2EE应用服务器. J ...
- 《JavaScript高级程序设计(第三版)》-3
相等操作符 相等和不相等 在转换不同的数据类型时,相等和不想等操作符遵循下面基本规则: 如果有一个操作符数是布尔值,则在比较相等性之前先将其转换为数值——false转换为0,而true转换为1: 如果 ...
- 简单的并发服务器(多个线程各自accept)
基于之前讲述的简单循环服务器,做一个多个线程各自accept的服务器demo 由于多个线程各自accept,容易造成数据错误,需要在accept前后枷锁 先看下客户端 客户端创建socket,初始化服 ...
- Turkey HSD检验法/W法
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录视频) https://study.163.com/course/introduction.htm?courseId=1005269003&u ...
- K8S Link
https://www.cnblogs.com/linuxk/p/9783510.html https://www.cnblogs.com/fengzhihai/p/9851470.html
- sqlserver 2008备份与还原
一.SQL数据库的备份: 1.依次打开 开始菜单 → 程序 → Microsoft SQL Server 2008 → SQL Server Management Studio → 数据库:Dside ...
- Python学习笔记(三十四)—内置模块(3)base64
摘抄自:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431954588 ...
- 关于Cookie跨域的问题研究
Cookie是一个伟大的发明,它允许Web开发者保留他们的用户的登录状态.但是当你的站点有一个以上的域名时就会出现问题了.在Cookie规范上说,一个cookie只能用于一个域名,不能够发给其它的域名 ...
- Date、String、Calendar相互转化
Date是在Jdk1.0出现的专门用来处理时间的类,但是由于Date在国际化方面存在限制,在Jdk1.1推出Calendar,现在Date的很多方法都已经过时,都迁移到Calendar上. 1.Dat ...