(01背包)Rectangle

Rectangle
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB
Total submit users: 23, Accepted users: 13
Problem 13266 : No special judgement
Problem 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
Judge Tips

——Shuai Long

Problem Source
HNU Contest 
题意:就是说有1*x或者2*x的长方形,你有2*m的总空间,问你m的最小值;
思路:如果是2*x那么x必定是要加的,但是1*x则可以叠加,so把所有的1的宽x加起来,用0-1背包一下就行了;


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 105;
typedef long long LL;
const int INF = 0xfffffff;
int c[maxn];
int dp[10005];
int main()
{
	//freopen("f:\\input.txt", "r", stdin);
	int T;
	scanf("%d", &T);
	while (T--)
	{
		int n;
		scanf("%d", &n);
		int a, b;
		int sum = 0;
		int ans = 0, top = 0;
		for (int i = 0; i < n; i++)
		{
			scanf("%d%d", &a, &b);
			if (a == 2)
			{
				ans += b;
			}
			else
			{
				c[top++] = b;
				sum += b;
			}
		}
		memset(dp, 0, sizeof(dp));
		for (int i = 0; i < top;i++)
		for (int j = sum / 2; j >= c[i]; j--)
			dp[j] = max(dp[j], dp[j - c[i]] + c[i]);
		ans += sum - dp[sum / 2];
		printf("%d\n", ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值