CSU - 1547 Rectangle —— DP(01背包)
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1547
题解:
关键是怎么处理长度为1的长方形。当长度为1的长方形的个数cnt>=2时,怎么把这些长方形分成两组,使得这两组的高度差最小,即最接近H/2。一开始用贪心做,结果wa了。因为贪心出来的不一定是最优,于是就想到了用dp。dp出H/2最大能够容纳的高度。然后再取 H-dp[H/2]。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
//#define LOCAL
#define eps 0.0000001
#define LNF (1<<60)
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = 100000+10;
const int mod = 1e9+7; int main()
{
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // LOCAL int t,n;
int a[105],dp[10005];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int val,ty;
int ans = 0, cnt = 0, H = 0;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&ty,&val);
if(ty==1) H += val, a[++cnt] = val;
else ans += val;
}
memset(dp,0,sizeof(dp)); if(cnt==1)
ans += a[1]; else if(cnt>1)
{
for(int i = 1; i<=cnt; i++)
for(int j = H/2; j>0; j--)
if(j>=a[i])
dp[j] = max(dp[j],dp[j-a[i]]+a[i]); ans += H-dp[H/2];
}
printf("%d\n", ans);
}
return 0;
}
CSU - 1547 Rectangle —— DP(01背包)的更多相关文章
- USACO Money Systems Dp 01背包
一道经典的Dp..01背包 定义dp[i] 为需要构造的数字为i 的所有方法数 一开始的时候是这么想的 for(i = 1; i <= N; ++i){ for(j = 1; j <= V ...
- HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)
HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...
- POJ.3624 Charm Bracelet(DP 01背包)
POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...
- HDOJ(HDU).2546 饭卡(DP 01背包)
HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...
- HDOJ(HDU).2602 Bone Collector (DP 01背包)
HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...
- UVA.10130 SuperSale (DP 01背包)
UVA.10130 SuperSale (DP 01背包) 题意分析 现在有一家人去超市购物.每个人都有所能携带的重量上限.超市中的每个商品有其相应的价值和重量,并且有规定,每人每种商品最多购买一个. ...
- CSU 1547 Rectangle(dp、01背包)
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1547 Description Now ,there are some rectang ...
- CSU 1547: Rectangle (思维题加一点01背包)
1547: Rectangle Submit Page Summary Time Limit: 1 Sec Memory Limit: 256 Mb Submitted: ...
- 51 nod 1007 正整数分组 (简单01背包) && csu 1547: Rectangle
http://www.51nod.com/onlineJudge/questionCode.html#problemId=1007¬iceId=15020 求出n个数的和sum,然后用s ...
随机推荐
- nfs详解及实现全网备份
1.统一hosts cat /etc/hosts 172.16.1.5 lb01 172.16.1.6 lb02 172.16.1.7 web02 172.16.1.8 web01 172.16.1. ...
- RabbitMQ 核心概念
目录 RabbitMQ 特点 AMQP 协议 RabbitMQ 消息传递机制 Message Exchange 1. 简介 2. 类型 3. 属性 RabbitMQ 特点 RabbitMQ 相较于其他 ...
- SpringMVC整合MongoDB
首先,在pom文件中新增spring-data-mongodb的依赖: <dependency> <groupId>org.springframework.data</g ...
- [转] 32位 PL/SQL Develope r如何连接64位的Oracle 图解
原文地址:LINK 由于硬件技术的不断更新,Win7系统逐渐成为主流,而且计算机内存逐渐增大,为了充 分的利用内存资源(因为32为系统最多只能用到3G左右的内存),提高系统性能,很多人开始使用Win7 ...
- 3.【nuxt起步】-下面以一个SPA单页程序为例子
spa:single page applcation 1.components目录新建header.vue,footer.vue Header.vue Footer.vue Pages/index.v ...
- Git学习0基础篇(下)
server上的 Git - 协议 Git能够使用四种基本的协议传输资料:本地协议(Local).HTTP 协议.SSH(Secure Shell) 协议以及 Git 协议.眼下使用最普及的是 SSH ...
- react 通过 classnames 处理 多个class 的问题
react原生动态添加多个className会报错: import style from './style.css' <div className={style.class1 style.cla ...
- Java中HashMap遍历的两种方法(转)
第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Ma ...
- python(24)- 面向对象进阶
面向对象基础知识: 1.面向对象是一种编程方式,此编程方式的实现是基于对类和对象的使用: 2.类是一个模板,模板中包装了多个‘函数’供使用(可以将多函数中公用的变量封装到对象中): 3.对象,根据模板 ...
- python(32)- 模块练习Ⅱ:使用正则表达式实现计算器的功能
开发一个简单的python计算器 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568 ...