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. 使用Ajax上传图片到服务器(不刷新页面)

    有时候我们需要上传图片时不刷新页面,那么Ajax就是很好的东西哦.之前在网上找了很多的资料都不对,不是这里就是那里错,这是本人亲自测试了的哈,是没有问题的,若有不足之处希望指正.我用的.net,对了这 ...

  2. storyboard pushViewController 的时候,新的界面黑屏

    storyboard 创建的一级界面需要通过代码跳转到另一 storyboard 创建的界面的时候,通常我们会这样 其实 alloc init 相当于重新创建一个界面,所以我们 push 进入之后会发 ...

  3. c语言if语句

    #include<stdio.h>#include<windows.h>#include <limits.h>#include <math.h>int ...

  4. wireshark_Couldn’t run /usr/sbin/dumpcap in child process: Permission denied

    关于Wireshark出现:Couldn't run /usr/sbin/dumpcap in child process: Permission denied Are you a member of ...

  5. iOS - HTTPS接口加密和身份认证

    为什么要使用HTTPS代替HTTP HTTPS和HTTP的区别 https协议需要到CA申请证书,一般免费证书很少,需要交费. http是超文本传输协议,信息是明文传输,https则是具有安全性的SS ...

  6. jquery学习笔记---Dom操作

    一.DOM操作的分类 DOM(document object model)是一种与浏览器.平台.语言无关的接口,使用该接口可以访问页面中的·所有组件.DOM的操作可以分为DOM Core.HTML-D ...

  7. Hybrid App是如何实现网页语言与程序语言的混合?谁占主体?

    [编者按]本文作者@徐珂铭,一位看好Html5的移动互联网的从业人士.喜爱玩技术,会点JAVA.HTML及CSS,有自己的想法及姑且能表达想法的文字,因此有了自己的文章. 基于HTML5的Web Ap ...

  8. jQuery Mobile学习之grid、等待显示的ajax效果、页面跳转、页面跳转传递参数等(二)

    Index.cshtml <!-- Start of second page --> <section data-role="page" id="bar ...

  9. Python 打包程序判断是否已经运行

    代码如下: # -*- coding: UTF8 -*- from win32com.client import Dispatch import win32com import sys, os fro ...

  10. iphone手机不同版本兼容、横竖屏

    /* 兼容问题*/ @media screen and (device-width: 320px) and (device-height: 480px) and (-webkit-device-pix ...