Rectangle 0-1背包


A - Rectangle
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%lld & %llu
Submit  Status  Practice  CSU 1547

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

Hint


0-1背包问题。。背包大小 sum/2

//HDU1171:Big Event in HDU
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;


int dp[100050];
int f[105];

int main() 
{
	int i,j,k,t,n,a,p,b;
	scanf("%d",&t);
	for (p=1;p<=t;p++)
	{
		memset(dp,0,sizeof(dp));
		scanf("%d",&n);
		
		int ok=0;
		int sum=0;
		int len=0;
		for (j=1;j<=n;j++)
		{
			scanf("%d%d",&a,&b);
			if (a==2)
			{
				len+=b;
			}
			
			if (a==1)
			{
				f[++ok]=b;
				sum+=b;
			}
			
		}
		for (i=1;i<=ok;i++)
		{
			for (j=sum/2;j>=f[i];j--)
			{
				dp[j]=max(dp[j],dp[j-f[i]]+f[i]);
			}
		}
		
		
		len+=sum-dp[sum/2];
		
		printf("%d\n",len);
		
		
		
		
		
		
	}
	
	
	return 0;
	
}

你可能感兴趣的:(Rectangle 0-1背包)