题目链接: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背包)的更多相关文章

  1. USACO Money Systems Dp 01背包

    一道经典的Dp..01背包 定义dp[i] 为需要构造的数字为i 的所有方法数 一开始的时候是这么想的 for(i = 1; i <= N; ++i){ for(j = 1; j <= V ...

  2. HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)

    HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...

  3. POJ.3624 Charm Bracelet(DP 01背包)

    POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...

  4. HDOJ(HDU).2546 饭卡(DP 01背包)

    HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...

  5. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

  6. UVA.10130 SuperSale (DP 01背包)

    UVA.10130 SuperSale (DP 01背包) 题意分析 现在有一家人去超市购物.每个人都有所能携带的重量上限.超市中的每个商品有其相应的价值和重量,并且有规定,每人每种商品最多购买一个. ...

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

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

  8. CSU 1547: Rectangle (思维题加一点01背包)

    1547: Rectangle Submit Page    Summary    Time Limit: 1 Sec     Memory Limit: 256 Mb     Submitted: ...

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

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

随机推荐

  1. redis容量预估

    2.存储的数据内容:前端系统登录用到的Token,类型:key:string(32),value:string(32)3.业务场景存数据:用户登录验证成功后,ICORE-PAP后台产生Token(st ...

  2. iOS Mobile Development: Using Xcode Targets to Reuse the Code 使用xcode targets来实现代码复用

    In the context of iOS mobile app development, a clone is simply an app that is based off another mob ...

  3. excel怎么把文本格式的数字转换为数字,且把前面的撇号去掉

    excel把文本格式的数字转换为数字,且把前面的撇号去掉方法:1.选中要处理的列,在“数据”菜单下,选择“分列”.2.在“分列”向导对话框里,选择“分隔符号”,并点击下一步.3.在“分列”向导对话框第 ...

  4. Direct2D教程(九)渲染位图

    概述 这篇的标题更确切的说应该叫位图画刷,这样才好和前几篇对应起来.在Direct2D中,位图的渲染也是通过画刷来实现的. Direct2D中并没有直接操作位图的接口,而是借助WIC(Windows ...

  5. c# Dictionary拓展2个key得到1个value

    using System.Collections.Generic; using System.Collections; Dictionary<Tuple<int, int>, int ...

  6. G 全然背包

    <span style="color:#3333ff;">/* /* _________________________________________________ ...

  7. mt-datetime-picker type="date" 时间格式 bug

    1.mint-ui 中 mt-datetime-picker 组件,存在日期格式 bug <!-- 日期选择器 --> <template> <div class=&qu ...

  8. soapUI学习笔记---断言的小使用

    转自:http://www.cnblogs.com/duimianyoushan/p/4274791.html 以下示例在soapUI 4.5中进行.刚开始学soapUI的使用,记录下以免忘记 1.创 ...

  9. C语言函数的递归和调用

    函数记住两点: (1)每个函数运行完才会返回调用它的函数:每个函数运行完才会返回调用它的函数,因此,你可以先看看这个函数不自我调用的条件,也就是fun()中if条件不成立的时候,对吧,不成立的时候就是 ...

  10. Java泛型 类型变量的限定

    有时候,类和方法须要对类型变量加以约束.比方你有一个方法,你仅仅希望它接收某个特定类型及其子类型作为參数. 以下就举一个方法限定接收參数的类型的样例来说明怎样限定类型变量. 首先有几个简单的辅助类: ...