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

最新文章

  1. 解决360、猎豹浏览器等极速模式下css3兼容问题
  2. JRE和JDK
  3. BT3入门之中文语言支持
  4. [ActionScript 3.0] AS3.0 水面波纹效果
  5. 评论一下现有几个开源IM框架(Msn/QQ/Fetion/Gtalk...)
  6. Android项目记录点滴
  7. zookeeper参考
  8. Visual Studio Team Services使用教程--Readers tfs组成员添加
  9. “最美天气”版本II
  10. JMeter接口返回数组键值对校验方法
  11. [LeetCode] Circular Array Loop 环形数组循环
  12. Theano.tensor.round函数学习,同时解决输出Elemwise{xxx,no_inplace}.0的问题
  13. Collections of Zujin Zhang&#39;s Published works
  14. css:清楚html所有标签自带属性
  15. Codeforces D - GCD of Polynomials
  16. UVA1252 【Twenty Questions】
  17. IntelliJ IDEA 2017版 spring-boot使用Spring Data JPA使用Repository&lt;T, T&gt;编程
  18. OpenGL学习--05--纹理立方体--BMP文件格式详解(转载)
  19. Nginx rewrite 中break与last指令的区别
  20. python学习笔记(十六)之文件

热门文章

  1. 【OpenCV】直方图
  2. WebFrom 的js日期控件
  3. AFNetworking request failed unacceptable content type text/html
  4. js中我的注释规范
  5. win7下python3.4 ImportError: No module named &#39;MySQLdb&#39;错误解决方法
  6. loadrunner支持https协议的操作方法-经验总结
  7. Java -- String、StringBuffer、StringBuilder
  8. EF学习 笔记-----EF映射
  9. JavaWeb学习之什么是Servlet、如何使用servlet、为什么这样使用、servlet的虚拟路径、关于缺省Servlet(2)
  10. SQL的一切常用函数展示