您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  背包问题 csuoj acm