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)?(a):(b))

typedef struct nide
{
    int bas,add;
}dnode;
dnode D[ 255 ];
int F[ 255 ][ 255 ];

int cmp( constvoid* a, constvoid* b )
{
    dnode* p = (dnode*)a;
    dnode* q = (dnode*)b;
    return p->add - q->add;
}

int main()
{
    int T,n,m,t,i,j,v;
    while ( scanf("%d",&T) != EOF )
    for ( t = 1 ; t <= T ; ++ t ) {
        scanf("%d%d",&n,&m);
        for ( i = 1 ; i <= n ; ++ i )
            scanf("%d",&D[ i ].bas);
        for ( i = 1 ; i <= n ; ++ i )
            scanf("%d",&D[ i ].add);
        qsort( &D[ 1 ], n, sizeof( dnode ), cmp );

        memset( F, 0, sizeof( F ) );
        for ( i = 1 ; i <= n ; ++ i )
        for ( j = 1 ; j <= m ; ++ j ) {
            v = (j-1)*D[ i ].add+D[ i ].bas;
            if ( F[ i ][ j ] < F[ i-1 ][ j-1 ] + v )
                F[ i ][ j ] = max( F[ i-1 ][ j ], F[ i-1 ][ j-1 ] + v );
        }

        printf("%d\n",F[ n ][ m ]);
    }
    return 0;
}
时间: 2024-10-12 06:16:47

zoj 3211 - Dream City的相关文章

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 DP 01背包 经典问题

题目大意:JAVAMAN 到梦幻城市旅游见到了黄金树,黄金树上每天回结出金子.已经有n棵树,JAVAMAN要停留m天,每天只能砍掉一棵树,砍掉树后就能得到树上的黄金.给定n棵树上原有的黄金a[i]和每天可以新增加的黄金b[i],求他最多可以得到多少黄金.中途如果有1天不砍树的话,之后的日子久不能砍树,所有最好每天都砍树,或者直到树被砍完. 这个其实是个背包问题,把日期看成背包的容量.然后n棵树当成n个物品. 假设我们取得由某 m 棵树组成的最优解.如果先砍的树的b值比后砍的树的b值大, 那么我们

Dream City(线性DP)

描述 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 on it (i=1, 2, 3...n). Surprisingly, each tree i can grow

ZOJ Design the city LCA转RMQ

Design the city Time Limit: 1 Second      Memory Limit: 32768 KB Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terrible, that there are traffic jams everywhere. Now, Cerror finds out that the main reason

ZOJ 3195 Design the city

倍增法在线LCA..... ZOJ Problem Set - 3195 Design the city Time Limit: 1 Second      Memory Limit: 32768 KB Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terrible, that there are traffic jams everywhere. Now,

zoj 1100 - Mondriaan&amp;#39;s Dream

题目:在m*n的地板上铺上同样的1*2的地板砖,问有多少种铺法. 分析:dp,组合,计数.经典dp问题,状态压缩. 状态:设f(i,j)为前i-1行铺满,第i行铺的状态的位表示为j时的铺砖种类数: 转移:由于仅仅能横铺或者竖铺.那么一个砖块铺之前的状态仅仅有两种: 且假设当前竖放会对下一行产生影响,建立相邻两行状态相应关系. 这里利用dfs找到全部f(i.j)的上一行的全部前置状态f(i-1,k)加和就可以. f(i.j)= sum(f(i-1,k)){ 当中,f(i-1,k)能够产生f(i.j

ZOJ 3041 City Selection(好排序)

题目链接:ZOJ 3041 City Selection 题意:有N个城市坐标和M个工厂坐标,在以工厂为原点的第四象限的点都会受到污染.即图中画线区域 思路:把工厂和城市的坐标一起排序,再比较y坐标. AC代码: #include <stdio.h> #include <algorithm> #include <set> using namespace std; const int maxn=200010; struct node{ int x,y; int flag;

ZOJ Problem Set - 3195 Design the city 【Tarjan离线LCA】

题目:ZOJ Problem Set - 3195 Design the city 题意:给出一个图,求三点的连起来的距离. 分析:分别求出三点中任意两点的距离 / 2  = ans AC代码: #include <iostream> #include <cstdio> #include <cstring> #include <vector> using namespace std; #define N 50010 #define M 20010 struc