CSU 1547: Rectangle (思维题加一点01背包)
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的值是多少
在给出的方块中,宽为2的方块肯定是直接放进2 x m的大方块中的
所以ans直接加上这些宽为2的方块的长
所以我们应该尽可能的把这些方块分成尽可能相等的两部分
这样m才会最小
所以我们现在是把这些宽度为1,长度已知的方块采取尽可能好的策略放置
背包容量就是所有宽度为1的方块的长
物品价值和重量都是这些宽度为1的方块的长
#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背包)的更多相关文章
- CSU 1547 Rectangle(dp、01背包)
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1547 Description Now ,there are some rectang ...
- 51 nod 1007 正整数分组 (简单01背包) && csu 1547: Rectangle
http://www.51nod.com/onlineJudge/questionCode.html#problemId=1007¬iceId=15020 求出n个数的和sum,然后用s ...
- QAU 18校赛 J题 天平(01背包 判断能否装满)
问题 J: 天平 时间限制: 1 Sec 内存限制: 128 MB提交: 36 解决: 9[提交][状态][讨论版][命题人:admin] 题目描述 天平的右端放着一件重量为w的物品.现在有n个重 ...
- 洛谷P1926 小书童—刷题大军【01背包】
题目链接:https://www.luogu.org/problemnew/show/P1926 题目背景 数学是火,点亮物理的灯:物理是灯,照亮化学的路:化学是路,通向生物的坑:生物是坑,埋葬学理的 ...
- CSU - 1547 Rectangle —— DP(01背包)
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1547 题解: 关键是怎么处理长度为1的长方形.当长度为1的长方形的个数cnt> ...
- dp --- CSU 1547: Rectangle
Rectangle Problem's Link: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1547 Mean: 给你一些宽为1或2 的木 ...
- [Usaco2008 Dec]Hay For Sale 购买干草[01背包水题]
Description 约翰遭受了重大的损失:蟑螂吃掉了他所有的干草,留下一群饥饿的牛.他乘着容量为C(1≤C≤50000)个单位的马车,去顿因家买一些干草. 顿因有H(1≤H≤5000)包 ...
- Bookshelf 2(poj3628,01背包,dp递推)
题目链接:Bookshelf 2(点击进入) 题目解读: 给n头牛,给出每个牛的高度h[i],给出一个书架的高度b(所有牛的高度相加>书架高度b),现在把一些牛叠起来(每头牛只能用一次,但不同的 ...
- 51nod1085(01背包)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1085 题意: 中文题诶~ 思路: 01背包模板题. 用dp[ ...
随机推荐
- bzoj2111ZJ2010排列计数_solution
-by bzoj http://www.lydsy.com/JudgeOnline/problem.php?id=2111 考虑第i个位置上的数字的可能性只取决于第i/2位置上的数,以及剩余数集的大小 ...
- Vue 框架-10-搭建脚手架 CLI
Vue 框架-10-搭建脚手架 CLI + 批处理快捷启动 脚手架是通过 webpack 搭建的开发环境 使用 ES6 语法 打包和压缩 JS 为一个文件 项目文件在环境中,而不是浏览器 实现页面自动 ...
- Android通过Intent传递对象
1.传递Serializable方式类对象 首先创建一个序列化类:User import java.io.Serializable; public class User implements Seri ...
- spring <context:annotation-config/> 注解作用
<context:component-scan>包含<context:annotation-config/>的作用 <context:annotation-config/ ...
- SpringBoot 之HelloController
1.搭建环境 1.1 新建一个maven项目,一路下一步 1.2 打开pom 文件,首先增加<parent></parent>标签: <parent> <gr ...
- Pig distinct用法举例
dst = distinct data: DISTINCT只能对整个记录(整行)去重,不能在字段级别去重. 触发reduce阶段 data = load 'data'; distinct ...
- 通过html导出PDF如何分页
每页一个DIV,加上样式page-break-inside:avoid; 即可分页了 .pdfpage{page-break-inside:avoid;} <div class="pd ...
- Angularjs controller之间的通信
刚刚看了网上的一些关于控制器之间的通信:然后结合自己项目做了一些,这里主要做的是二个同级之间的controller通信. Html: <html> <script src=" ...
- spring boot(12)-数据源配置原理
本篇讲的不仅是数据源配置,这也是spring boot实现自动配置的一部分.要理解数据源的配置原理,首先要理解第十篇tomcat连接池的配置 数据源配置源码 这里截取org.springframewo ...
- oracle 闪回功能详解
Oracle的闪回技术提供了一组功能,可以访问过去某一时间的数据并从人为错误中恢复.闪回技术是Oracle 数据库独有的,支持任何级别的恢复,包括行.事务.表和数据库范围.使用闪回特性,您可以查询以前 ...