题目大意:JAVAMAN 到梦幻城市旅游见到了黄金树,黄金树上每天回结出金子。已经有n棵树,JAVAMAN要停留m天,每天只能砍掉一棵树,砍掉树后就能得到树上的黄金。给定n棵树上原有的黄金a[i]和每天可以新增加的黄金b[i],求他最多可以得到多少黄金。中途如果有1天不砍树的话,之后的日子久不能砍树,所有最好每天都砍树,或者直到树被砍完。

这个其实是个背包问题,把日期看成背包的容量。然后n棵树当成n个物品。

假设我们取得由某 m 棵树组成的最优解。如果先砍的树的b值比后砍的树的b值大,

那么我们总能交换这两树砍的顺序而得到更多的钱。所以我们按增加钱币量b值升序将n棵树排序。

然后就是个dp 的过程,dp[i][j] 表示 在前 j 天,前 i 颗树 能达到的最大效益

Source Code:

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int N = ;
const int M = * ;
const ll P = 10000000097ll ;
const int MAXN = ;
const int INF = 0x3f3f3f3f ;
const int offset = ; int n, m;
int dp[][]; //dp[i][j] 意思是在前j天,前i颗树 能达到的最大效益 struct sc {
int a, b;
} a[]; bool cmp (struct sc a, struct sc b) {
return a.b < b.b;
} int main() {
std::ios::sync_with_stdio(false);
int i, j, t, k, u, c, v, p, numCase = ; cin >> t;
while (t--) {
cin >> n >> m;
for (i = ; i <= n; ++i) cin >> a[i].a;
for (i = ; i <= n; ++i) cin >> a[i].b; sort (a + , a + + n, cmp);
memset (dp, , sizeof (dp));
for (i = ; i <= n; ++i) {
for (j = ; j <= m; ++j) {
dp[i][j] = Max (dp[i - ][j], dp[i - ][j - ] + a[i].a + (j - ) * a[i].b); //Choose or not choose
}
}
cout << dp[n][m] << endl;
} return ;
}

ZOJ 3211 Dream City DP 01背包 经典问题的更多相关文章

  1. ZOJ 3211 Dream City(线性DP)

    Dream City Time Limit: 1 Second      Memory Limit: 32768 KB JAVAMAN is visiting Dream City and he se ...

  2. ZOJ 3211 Dream City(DP)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3374 题目大意:JAVAMAN 到梦幻城市旅游见到了黄金树,黄金树上 ...

  3. ZOJ 3211 Dream City

    贪心,$dp$. 假设我们知道要选择哪些物品,那么这些物品应该按什么顺序选择呢? 物品$A(a1,b1)$,物品$B(a2,b3)$. 假设物品$A$在第$x$天被选择,物品$B$在第$y$天被选择. ...

  4. USACO Money Systems Dp 01背包

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 高性能javascript 学习笔记(1)

    加载和运行 管理浏览器中的javascript代码是个棘手的问题,因为代码运行阻塞了其他浏览器处理过程,诸如用户绘制,每次遇到<script>标签,页面必须停下来等待代码下载(如果是外部的 ...

  2. ural1057 Amount of degrees 位数统计

    #include <iostream> #include <string> using namespace std; ][]; void init(){ f[][] =; ;i ...

  3. SubLime2 乱码解决

    参考 http://www.fuzhaopeng.com/2012/sublime-text-2-with-gb2312-gbk-support/ 使用其中提到的方法二安装 首先下载http://su ...

  4. 帝国cms修改[!--show.listpage--]分页页码所生成的html标签

    在使用帝国cms系统时,我们用[!--show.page--]和[!--show.listpage--]来生成页码 其中[!--show.listpage--]所生成的html页码代码为: <a ...

  5. CSS找到 (div+css请讲)

    CSS 定位和浮动 CSS 定位和浮动提供了一些特性,使用这些属性,你可以建立栏布局,的重叠布局的一部分,并有一些.也可多年来完成通常需要使用的多个表格能力完成的任务. 定位的基本思路是非常easy. ...

  6. 11个实用但你可能不知道的Python程序库

    目前,网上已有成千上万个Python包,但几乎没有人能够全部知道它们.单单PyPi上就有超过47000个包列表. 现在,越来越多的数据科学家开始使用Python,虽然他们从pandas,scikit- ...

  7. ThinkPHP - 登录模块,核心代码

    /** * 登录成功 * @return [type] [description] */ public function checkLogin($data) { $user = M($this-> ...

  8. CAN总线与RS485的比较

    CAN总线与RS485的比较 http://blog.csdn.net/reille/article/details/6135546 can总线与485总线有什么区别?  http://blog.16 ...

  9. resolve "Undefined attribute name" warning for Angular "ng-" attributes in HTML files

    由于这些attributes引起的warning数量较多, 影响直观查找其他warning. 因此选择将这类warning忽视掉: Project Property -> Validation ...

  10. php的迭代器

    接口Iterator 主要需要实现的方法: abstract public mixed current ( void ) abstract public scalar key ( void ) abs ...