CSU 1547: Rectangle

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
#include
#include
#include
#include
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;icin>>a[i]>>b[i];
            if(a[i]==1)
            sum+=b[i];
        }
        for(int i=0;iif(a[i]==2)
            {
                ans+=b[i];
            }
        }
        int num = (sum>>1);
        memset(dp,0,sizeof(dp));
        for(int i=0;ifor(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<return 0;
} 

你可能感兴趣的:(背包问题)