简单背包DP

Problem A: Rectangle

Time Limit: 1 Sec   Memory Limit: 256 MB
Submit: 207   Solved: 46
[ Submit][ Status][ Web Board]

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

#include<algorithm>
#include<iostream>
#include<limits.h>
#include<stdlib.h>
#include<string.h>
#include<cstring>
#include<iomanip>
#include<stdio.h>
#include<bitset>
#include<cctype>
#include<math.h>
#include<string>
#include<time.h>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<list>
#include<map>
#include<set>

#define LL long long

using namespace std;
const LL mod = 1e9 + 7;
const double PI = acos(-1.0);
const double E = exp(1);
const int M = 110;

int aa[M];
int dp[M * M];
int main()
{
    int t;
    int n;
    cin >> t;
    while( t-- ){
        cin >> n;
        int a, b;
        int ans = 0;
        int flag = 0;
        int sum = 0;
        while( n-- ){
            cin >> a >> b;
            if(a == 2)
                ans += b;
            else{
                aa[flag++] = b;
                sum += b;
            }
        }
        memset(dp,0,sizeof(dp));
        for(int i = 0; i < flag; ++i)
            for(int j = sum / 2; j >= aa[i]; --j)
                dp[j] = max(dp[j],dp[j - aa[i]] + aa[i]);
        ans += sum - dp[sum / 2];
        cout << ans << endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值