Rectangle(csu)

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

记忆化搜索

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
using namespace std;
int num[120];
int n;
int maxhave[10000][120];
int getmax(int sum,int n)
{
    int res;
    if(maxhave[sum][n] != -1) res = maxhave[sum][n];
    else if(n == 1){
        if(sum >= num[n]) res = num[n];
        else res = 0;
    }
    else if(sum >= num[n]){
        res = max(getmax(sum - num[n],n - 1) + num[n],getmax(sum,n - 1));
    }
    else res = getmax(sum,n - 1);
    maxhave[sum][n] = res;
    return res;
}
int main()
{
    int t;
    cin>>t;
    while(t--){
        memset(maxhave,-1,sizeof maxhave );
        cin>>n;
        int ans,sum;
        int a,b,c=1;
        ans = sum = 0;
        for(int i = 1; i <= n; ++i)
        {
            cin>>a>>b;
            if(a == 2) ans += b;
            else { num[c++] = b;sum += b; }
        }
        --c;
        int tmp = getmax(sum/2,c);
        ans = ans + max(tmp,sum-tmp);
        cout<<ans<<endl;
    }
}

 

posted @   JL_Zhou  阅读(224)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示