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

记忆化搜索

#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;
}
}

Rectangle(csu)的更多相关文章

  1. dp --- CSU 1547: Rectangle

    Rectangle Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1547 Mean: 给你一些宽为1或2 的木 ...

  2. CSU 1547 Rectangle(dp、01背包)

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1547 Description Now ,there are some rectang ...

  3. CSU 1547: Rectangle (思维题加一点01背包)

    1547: Rectangle Submit Page    Summary    Time Limit: 1 Sec     Memory Limit: 256 Mb     Submitted: ...

  4. CSU - 1547 Rectangle —— DP(01背包)

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1547 题解: 关键是怎么处理长度为1的长方形.当长度为1的长方形的个数cnt> ...

  5. 51 nod 1007 正整数分组 (简单01背包) && csu 1547: Rectangle

    http://www.51nod.com/onlineJudge/questionCode.html#problemId=1007&noticeId=15020 求出n个数的和sum,然后用s ...

  6. [LeetCode] Perfect Rectangle 完美矩形

    Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...

  7. [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

  8. [LeetCode] Smallest Rectangle Enclosing Black Pixels 包含黑像素的最小矩阵

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

  9. [LeetCode] Rectangle Area 矩形面积

    Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...

随机推荐

  1. Java容器题库

    一.    填空题 Java集合框架提供了一套性能优良.使用方便的接口和类,包括Collection和Map两大类,它们都位于  java.util  包中 队列和堆栈有些相似,不同之处在于栈是先进后 ...

  2. C#文件夹和文件操作

    File.Exist(string path)//文件读写FileStream fs=new FileStream(filename, FileMode.Create);BinaryWriter bw ...

  3. ERROR ITMS-90032 “Invalid image path”

    在用 Application Loader上传spa 文件的时候出现这样的错误:ERROR ITMS-90032: "Invalid image path No image found at ...

  4. tableView设置首尾

    [self.tableView setTableHeaderView:view]; [self.tableView setTableFooterView:view];

  5. java删除被占用的文件

    boolean result = f.delete();//判断是否删除完毕 if(!result) { System.gc();//系统进行资源强制回收 f.delete; }

  6. Ubuntu之MaxScale安装配置

    原文github:https://github.com/mariadb-corporation/MaxScale/blob/develop/Documentation/Documentation-Co ...

  7. DB2 Add hidden Identity columns

    An identity column contains a unique numeric value for each row in the table. DB2® can automatically ...

  8. Struts2拦截器之DefaultWorkflowInterceptor

    一.DefaultWorkflowInterceptor是什么 首先说这东西是干嘛来的,在action中可以对传进来的数据进行验证,方法是实现Validateable接口的validate():voi ...

  9. 与你相遇好幸运,Tippecanoe在Centos下の安装

    全新的CentOS 7 x86_64 安装编译工具 yum install -y gcc automake autoconf libtool make yum insyall -y gcc gcc-c ...

  10. vim: vs sp 调整窗口高度和宽度

    转自:http://www.cnblogs.com/xuechao/archive/2011/03/29/1999292.html vim多窗口有时候需要调整默认的窗口宽度和高度,可以用如下命令配合使 ...