CSU 1547: Rectangle
2017-08-04 01:01
459 查看
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; }
相关文章推荐
- 51 nod 1007 正整数分组 (简单01背包) && csu 1547: Rectangle
- CSU 1547 Rectangle(01背包)
- csu 1547: Rectangle (01背包)
- dp --- CSU 1547: Rectangle
- CSU - 1547 Rectangle —— DP(01背包)
- csu 1547: Rectangle (01背包)
- CSU 1547-Rectangle(0-1背包)
- csu 1547: Rectangle (01背包)
- dp --- CSU 1547: Rectangle
- CSU 1547 背包
- 湖南多校对抗赛(2015.03.28)CSU1547~1556 题解
- csu 1547(01背包)
- Rectangle(csu)
- C++第十三周【任务3】定义抽象基类Shape,由它派生出3个派生类,Circle(圆形)、Rectangle(矩形)、Triangle(三角形)
- csu 1085: 挖宝藏
- CSUOJ 1093 Caps Lock (CSU Monthly 2012 Aug. C)
- 直方图最大矩形问题(Largest Rectangle in Histogram)
- [LeetCode] Largest Rectangle in Histogram 解题报告
- HDU 1221 Rectangle and Circle
- Largest Rectangle in a Histogram