1547: Rectangle

Submit Page    Summary    Time Limit: 1 Sec     Memory Limit: 256 Mb     Submitted: 1198     Solved: 347


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

Source

题目意思:
现在有这样一些方块,1 x n和2 x n的方块
要你放进2 x m的大方块内(方块不重叠)
问你最小的m的值是多少
ans代表最小的m
分析:
在给出的方块中,宽为2的方块肯定是直接放进2 x m的大方块中的
所以ans直接加上这些宽为2的方块的长
现在没有放进去的方块中,只有宽度为1的了
所以我们应该尽可能的把这些方块分成尽可能相等的两部分
这样m才会最小
所以我们现在是把这些宽度为1,长度已知的方块采取尽可能好的策略放置
其实这就是一个01背包问题
背包容量就是所有宽度为1的方块的长
物品价值和重量都是这些宽度为1的方块的长
然后就是需要加上这些宽1的方块叠成的长的最大值
 
code:
#include<stdio.h>
#include<iostream>
#include<vector>
#include <cstring>
#include <stack>
#include <cstdio>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include<string>
#include<string.h>
#include<math.h>
typedef long long LL;
using namespace std;
#define max_v 105
int b[max_v];//宽1的方块长
//01背包
int ZeroOnePack_improve(int v[],int w[],int n,int c)
{
int dp[c+];
memset(dp,,sizeof(dp));
for(int i=; i<=n; i++)
{
for(int j=c; j>=; j--)
{
if(j>=w[i])
dp[j]=max(dp[j],dp[j-w[i]]+v[i]); }
}
return dp[c];
}
void init()
{
memset(b,,sizeof(b));
}
int main()
{
int t;
scanf("%d",&t);
int n;
int x,y;
while(t--)
{
init();
scanf("%d",&n);
//结果
int ans=;
//宽为1的方块的总长
int c=; int m=;
for(int i=; i<n; i++)
{
scanf("%d %d",&x,&y);
if(x==)
ans+=y;
else if(x==)
c+=y,b[++m]=y;
}
int w=ZeroOnePack_improve(b,b,m,c/);
ans+=max(c-w,w);//加上大的那个!!! wa了几次....
printf("%d\n",ans);
}
}
/*
题目意思:
现在有这样一些方块,1 x n和2 x n的方块
要你放进2 x m的大方块内(方块不重叠)
问你最小的m的值是多少 ans代表最小的m 分析:
在给出的方块中,宽为2的方块肯定是直接放进2 x m的大方块中的
所以ans直接加上这些宽为2的方块的长 现在没有放进去的方块中,只有宽度为1的了
所以我们应该尽可能的把这些方块分成尽可能相等的两部分
这样m才会最小
所以我们现在是把这些宽度为1,长度已知的方块采取尽可能好的策略放置 其实这就是一个01背包问题
背包容量就是所有宽度为1的方块的长
物品价值和重量都是这些宽度为1的方块的长 然后就是需要加上这些宽1的方块叠成的长的最大值 */

CSU 1547: Rectangle (思维题加一点01背包)的更多相关文章

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

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

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

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

  3. QAU 18校赛 J题 天平(01背包 判断能否装满)

    问题 J: 天平 时间限制: 1 Sec  内存限制: 128 MB提交: 36  解决: 9[提交][状态][讨论版][命题人:admin] 题目描述 天平的右端放着一件重量为w的物品.现在有n个重 ...

  4. 洛谷P1926 小书童—刷题大军【01背包】

    题目链接:https://www.luogu.org/problemnew/show/P1926 题目背景 数学是火,点亮物理的灯:物理是灯,照亮化学的路:化学是路,通向生物的坑:生物是坑,埋葬学理的 ...

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

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

  6. dp --- CSU 1547: Rectangle

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

  7. [Usaco2008 Dec]Hay For Sale 购买干草[01背包水题]

    Description     约翰遭受了重大的损失:蟑螂吃掉了他所有的干草,留下一群饥饿的牛.他乘着容量为C(1≤C≤50000)个单位的马车,去顿因家买一些干草.  顿因有H(1≤H≤5000)包 ...

  8. Bookshelf 2(poj3628,01背包,dp递推)

    题目链接:Bookshelf 2(点击进入) 题目解读: 给n头牛,给出每个牛的高度h[i],给出一个书架的高度b(所有牛的高度相加>书架高度b),现在把一些牛叠起来(每头牛只能用一次,但不同的 ...

  9. 51nod1085(01背包)

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1085 题意: 中文题诶~ 思路: 01背包模板题. 用dp[ ...

随机推荐

  1. LOJ#6491. zrq 学反演(莫比乌斯反演 杜教筛)

    题意 题目链接 Sol 反演套路题? 不过最后一步还是挺妙的. 套路枚举\(d\),化简可以得到 \[\sum_{T = 1}^m (\frac{M}{T})^n \sum_{d \ | T} d \ ...

  2. JS判断客户端是否是iOS或者Android端

    通过判断浏览器的userAgent,用正则来判断手机是否是 IOS 和 Android 客户端. 代码如下: (function(){ var u = navigator.userAgent; var ...

  3. CSS实现各类分栏布局

    在CSS中,实现分栏布局有两种方法.第一种方法是使用四种CSS定位选项(absolute .static.relative和fixed)中的绝对定位(absolute positioning),它可以 ...

  4. 分享一个oracle 完整备份的批处理文件

    该批处理是基本可以无限针对使用window 服务器的oracle 备份,如下: set mydate=%DATE:~0,10% exp e_cards2016/e_cards2016@orcl ful ...

  5. linux 文件操作命令 touch、cat、more、less、head、tail

    touch /bin/touch 创建空文件 linux 创建文件可以使用特殊符号,/除外 touch test test1 创建了两个文件touch "test test1" 创 ...

  6. 记录一次mybatis查询返回为空数据库却能查询到数据的经历

    昨晚上测试人员给发了一条测试数据,说是根据这条数据接口返回信息为空.之后根据给的信息去数据库查询了下,明明是有数据的.但是用mybatis就是查询不出来.奇了怪了,自己测试的数据都能查询出来,为何这条 ...

  7. leetCode题解寻找最短字符路径

    1.题目描述 2.分析 最简单的方案,对每一个字符,向两边寻找. 3.代码 vector<int> shortestToChar(string S, char C) { vector< ...

  8. Jmeter入门--脚本录制

    一.Badboy脚本录制(推荐) 下载地址:http://www.badboy.com.au/download/add,下载完成后直接安装即可. Badboy是一个强大的工具,旨在帮助测试和开发复杂的 ...

  9. C# .Net动态调用webService

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Ne ...

  10. Linux学习---Linux用户审计简单版

    [root@localhost root]# vim /etc/profile # SHENJI history USER=`whoami` USER_IP=`who -u am i 2>/de ...