ZOJ 3211 Dream City DP 01背包 经典问题

题目大意: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-7      ;
const int N = 210            ;
const int M = 1100011*2      ;
const ll P = 10000000097ll   ;
const int MAXN = 10900000    ;
const int INF = 0x3f3f3f3f   ;
const int offset = 100       ;

int n, m;
int dp[300][300];   //dp[i][j] 意思是在前j天,前i颗树 能达到的最大效益

struct sc {
    int a, b;
} a[300];

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 = 0;

    cin >> t;
    while (t--) {
        cin >> n >> m;
        for (i = 1; i <= n; ++i) cin >> a[i].a;
        for (i = 1; i <= n; ++i) cin >> a[i].b;

        sort (a + 1, a + 1 + n, cmp);
        memset (dp, 0, sizeof (dp));
        for (i = 1; i <= n; ++i) {
            for (j = 1; j <= m; ++j) {
                dp[i][j] = Max (dp[i - 1][j], dp[i - 1][j - 1] + a[i].a + (j - 1) * a[i].b);    //Choose or not choose
            }
        }
        cout << dp[n][m] << endl;
    }

    return 0;
}
时间: 2024-10-12 17:20:27

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

ZOJ 3211 Dream City (J) DP

Dream City Time Limit: 1 Second      Memory Limit: 32768 KB JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are n trees in the yard. Let's call them tree 1, tree 2 ...and tree n. At the first day, each tree i has ai coins

ZOJ 3211 Dream City(线性DP)

Dream City Time Limit: 1 Second      Memory Limit: 32768 KB JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are n trees in the yard. Let's call them tree 1, tree 2 ...and tree n. At the first day, each tree i has aicoins o

zoj 3211 - Dream City

题目:javaman来到了一个城市,这里有很多长着金币的树,每棵树每晚还会结出新的金币, 现在他每天白天只能砍一棵树,最多在这里呆m天,求能得到的最大金币数. 分析:贪心+dp,二维01背包.如果砍树的集合确定,那一定按照b递增的顺序砍,因此排序后背包. 说明:(2011-11-02 05:49). #include <stdio.h> #include <stdlib.h> #include <string.h> #define max(a,b) ((a)>(b

USACO Money Systems Dp 01背包

一道经典的Dp..01背包 定义dp[i] 为需要构造的数字为i 的所有方法数 一开始的时候是这么想的 for(i = 1; i <= N; ++i){ for(j = 1; j <= V; ++j){ if(i - a[j] > 0){ dp[i] += dp[i - a[j]]; } } } 状态存在冗余, 输出的时候答案肯定不对 但只需要改一下两个for循环的顺序即可. Source Code: /* ID: wushuai2 PROG: money LANG: C++ */ //

hdu 1561The more, The Better(树形dp&amp;01背包)

The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4949    Accepted Submission(s): 2918 Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝

poj 3345 Bribing FIPA 【树形dp + 01背包】

Bribing FIPA Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4274   Accepted: 1337 Description There is going to be a voting at FIPA (Fédération Internationale de Programmation Association) to determine the host of the next IPWC (Interna

poj 1837 Balance (dp,01背包)

链接:poj 1837 题意:有一个天平,天平左右两边各有若干个钩子,总共有C个钩子,有G个钩码, 求将钩码挂到钩子上使天平平衡的方法的总数.其中可以把天枰看做一个以x轴0点作为平衡点的横轴 分析:力臂=重量 *臂长 = g[i]*c[j] 当平衡度k=0时,说明天枰达到平衡,k>0,说明天枰倾向右边(x轴右半轴),k<0则左倾 因此可以定义一个 状态数组dp[i][k],意为在挂满前i个钩码时,平衡度为k的挂法的数量. 由于距离c[i]的范围是-15~15,钩码重量的范围是1~25,钩码数量

hdu 4044 GeoDefense (树形dp+01背包)

GeoDefense Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 663    Accepted Submission(s): 267 Problem Description Tower defense is a kind of real-time strategy computer games. The goal of towe

Bone Collector(dp 01背包)

Problem Description Many years ago , in Teddy's hometown there was a man who was called "Bone Collector". This man like to collect varies of bones , such as dog's , cow's , also he went to the grave - The bone collector had a big bag with a volu