题目大意: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. Python每日一练(1):计算文件夹内各个文章中出现次数最多的单词

    #coding:utf-8 import os,re path = 'test' files = os.listdir(path) def count_word(words): dic = {} ma ...

  2. ssh远程登录linux服务器

    ssh远程登录linux服务器 用法: ssh -l user -p port server_ip 或者 ssh -p port user@server_ip 参数: -l 后接要登录的远程系统用户名 ...

  3. Myeclipse利用maven构建sping web项目

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQQAAADvCAIAAACbnj2oAAAfq0lEQVR4nO2d+1MUV9rH+294U9mqpG ...

  4. UIPickView之自定义生日键盘和城市键盘

    ////  ViewController.m//  04-键盘处理// // #import "ViewController.h"#import "XMGProvince ...

  5. AndroidStudio 0.2.x 引入多模块Eclipse项目

    !!!!太他妈的累人了!整整折腾了两天!!!!!!! 不知从那个版本开始ImportModule... 从AndroidStudio的File菜单中消失了,在0.2之前的版本作为library的模块可 ...

  6. Razor强类型视图下的文件上传

    域模型Users.cs using System;using System.Collections.Generic;using System.Linq;using System.Web; namesp ...

  7. ASP.NET常用内置对象

    ASP.NET 常用内置对象:Response对象.Request对象.Session对象.Server对象.Application对象 1.Response对象: (1) 用于向浏览器输出信息 常用 ...

  8. 一个简单链表的C++实现

    /* LList.cpp * Author: Qiang Xiao * Time: 2015-07-12 */ #include<iostream> using namespace std ...

  9. Xcode之外的文档浏览工具--Dash (在iOS代码库中浏览本帖)

    链接地址:http://www.cocoachina.com/bbs/read.php?tid=273479 Xcode之外的文档浏览工具--Dash    (在iOS代码库中浏览本帖)       ...

  10. Myeclipse代码提示及如何设置自动提示

    Myeclipse代码提示及如何设置自动提示 (2011-11-18 00:38:41) 转载▼ 标签: 杂谈 分类: Java 1. 设置代码自动提示window --> preference ...