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[ ...
随机推荐
- 在JS中如何把毫秒转换成规定的日期时间格式
最近做的一个项目,从后台查询到日期数据传到前台JS中遍历之后显示的是以"14"开头的毫秒数,刚开始想用"simpleDateFormat"类处理,结果显示都不显 ...
- asp.net web api 2 host in a windows service推荐阅读
最简单的例子(官方)在控制台app里面运行: http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-h ...
- Dlink DIR-823G 漏洞挖掘过程
前言 本文由 本人 首发于 先知安全技术社区: https://xz.aliyun.com/u/5274 初步分析 首先下载固件 https://gitee.com/hac425/blog_data/ ...
- XSS学习(未完..)
前言 XSS 漏洞原理非常简单,实际应用中非常灵活,于是通过 prompt(1) to win 来学习学习 正文 工具 分析正则表达式 https://regex101.com/ http://xss ...
- needPrint 不显示打印按钮
客户问题: 客户用的是needPrint 来显示打印按钮,现在访问不能显示后台提示有错误 打开控制台显示: java.security.AccessControlException: ...
- Storm监控文件夹变化 统计文件单词数量
监控指定文件夹,读取文件(新文件动态读取)里的内容,统计单词的数量. FileSpout.java,监控文件夹,读取新文件内容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- 10 tensorflow在循环体中用tf.print输出节点内容
代码 i=tf.constant(0,dtype=tf.int32) batch_len=tf.constant(10,dtype=tf.int32) loop_cond = lambda a,b: ...
- WEB API 支持多种端的后台
一套代码,支持多种平台 1. 支持web 可以js获取webapi的数据源.利用mvvm组织展现在html上. 2.支持安卓. post方法,安卓获取webapi的数据.
- [翻译] FeSpinner
FeSpinner The loader collection for iOS app. 收集的iOS加载动画. REQUIREMENT FeSpinner work on any version i ...
- 《编程题》穷举法求N年后有多少头牛
若一头小母牛,从出生起第四个年头开始每年生一头母牛,按这个规律,第N年时有多少头母牛? #include <iostream> int main(int argc, const char ...