CSU 1547: Rectangle
来源:互联网 发布:手机找工作软件 编辑:程序博客网 时间:2024/05/16 02:43
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.
题意:
现有很多长方形,边长为a,b。其中a只有两种可能:1或2。 你需要找到一个足够大的长方形,边长为2,m。能把现有的长方形统统装进去。
分析:
a为1的长方形,取尽可能接近的b的长方形一起放。 01背包问题解法,详见背包九讲。
AC代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;int T,n,ans,sum;int a[10005],b[10005];int dp[10005];int main(){ while(cin>>T) { while(T--) { cin>>n; sum=0,ans = 0; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); for(int i=0;i<n;i++) { cin>>a[i]>>b[i]; if(a[i]==1) sum+=b[i]; } for(int i=0;i<n;i++) { if(a[i]==2) { ans+=b[i]; } } int num = (sum>>1); memset(dp,0,sizeof(dp)); for(int i=0;i<n;i++) for(int j=num;j>=b[i];j--) { if(a[i]==1) dp[j]=max(dp[j],dp[j-b[i]]+b[i]); } int aa=sum-dp[num]; aa=max(aa,dp[num]); ans+=aa; cout<<ans<<endl; }} return 0;}
阅读全文
2 2
- dp --- CSU 1547: Rectangle
- CSU 1547: Rectangle
- csu 1547: Rectangle (01背包)
- CSU 1547 Rectangle(01背包)
- csu 1547: Rectangle (01背包)
- CSU 1547-Rectangle(0-1背包)
- CSU 1547 背包
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- java网页正则表达式爬虫(用纯java代码在相应的网页中查看自己想要的内容)
- 调整字符串中文格式
- 2011 浙大机试 A+B for Matrices
- 解决“Spring 使用MapperScannerConfigurer无法读取*.properties文件"的问题
- 学以致用-使用分类汇总分析股价与时间维度(年、月、日、星期)的关系(Excel+Spotfire)
- CSU 1547: Rectangle
- Levenberg–Marquardt algorithm
- uva-699
- hdu1495--bfs
- matlab离线处理c++输出到txt的数据
- 单反相机的PASM
- 8.2 暑期集训—— 二分法
- 读书:特里尔的《毛泽东传》
- hdu 6059 Kanade's trio