Little shop of flowers - SGU 104 (DP)

题目大意:把 M 朵花插入 N 个花瓶中,每个花插入不同的花瓶都有一个价值A[Mi][Nj],要使所有的花都插入花瓶,求出来最大的总价值(花瓶为空时价值是0)。

分析:dp[i][j]表示前i朵花插入前j个花瓶的最大价值,那么比较容易看出 dp[i][j] = max(dp[i][j-1], dp[i][j-1]+A[i][j]),也就是这个花瓶要还是不要,别忘记输出路径。

代码如下:

========================================================================================================================================

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

const int MAXN = 107;
const int oo = 1e9+7;

int dp[MAXN][MAXN];
int A[MAXN][MAXN];

void Path(int x, int y, int M)
{
    if(x == 0)
        return ;

    if(dp[x][y] == dp[x][y-1])
        Path(x, y-1, M);
    else
    {
        Path(x-1, y-1, M);
        printf("%d%c", y, x==M ? ‘\n‘:‘ ‘);
    }
}

int main()
{
    int M, N;///M朵花,N个花瓶

    scanf("%d%d", &M, &N);

    for(int i=1; i<=M; i++)
    for(int j=1; j<=N; j++)
        scanf("%d", &A[i][j]);

    for(int i=1; i<=M; i++)
        dp[i][0] = -oo;

    for(int i=1; i<=M; i++)
    for(int j=1; j<=N; j++)
    {
        dp[i][j] = max(dp[i][j-1], dp[i-1][j-1]+A[i][j]);
    }

    printf("%d\n", dp[M][N]);

    Path(M, N, M);

    return 0;
}
时间: 2024-11-07 05:15:33

Little shop of flowers - SGU 104 (DP)的相关文章

sgu 104 Little Shop of Flowers

经典dp问题,花店橱窗布置,不再多说,上代码 #include <cstdio> #include <cstring> #include <iostream> #include <cstdlib> #include <algorithm> #define N 150 #define inf 0x7f7f7f7f using namespace std; int n, m; int val[N][N], f[N][N]; int fa[N][N];

POJ 题目1157 LITTLE SHOP OF FLOWERS(DP)

LITTLE SHOP OF FLOWERS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19457   Accepted: 8966 Description You want to arrange the window of your flower shop in a most pleasant way. You have F bunches of flowers, each being of a different

【DP】POJ-1157 LITTLE SHOP OF FLOWERS

LITTLE SHOP OF FLOWERS Time Limit: 1000MS Memory Limit: 10000K Description You want to arrange the window of your flower shop in a most pleasant way. You have F bunches of flowers, each being of a different kind, and at least as many vases ordered in

POJ 1157 LITTLE SHOP OF FLOWERS (线性dp)

OJ题目:click here~~ 题目分析:f个束花,编号为1-- f.v个花瓶,编号为1 -- v.编号小的花束,所选花瓶的编号也必须比编号大的花束所选花瓶的编号小,即花i 选k, 花j选t ,如果i < j ,则定有k < t . 如果 i > j , 则定有 k > t . 每束花放在每个花瓶里有一个值.求f束花,能得到的最大值. 设dp[ i ][ j ] 为第 i 束花选择了第 j 个花瓶 , 则转移方程为 dp[ i ][ j ] =  max(dp[ i  - 1]

SGU 104

简单DP. 递推关系式:f[i,j]=max{f[i-1,k]}+value[i,j]. 其中,i>=2,i<=j<=V-F+i,i-1<=k<j(原程序中貌似写成了i<=k<j TUT 不知道怎么AC的- - V是空间数,F是花的总数. #include "stdio.h" #include "string.h" const int MAXF=100,MAXV=100; int main(){ int f[MAXF+2][

POJ1157——LITTLE SHOP OF FLOWERS

LITTLE SHOP OF FLOWERS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18481   Accepted: 8512 Description You want to arrange the window of your flower shop in a most pleasant way. You have F bunches of flowers, each being of a different

【SGU】SGU每日练1&#183;Little shop of flowers【DP】

题目大意: 给你n*m的矩形(m >= n) 每个节点mp[i][j]有一个权值,从第一行走到最后一行,每一行只准选择一个数且对于i行,所选数的列数要严格大于i-1行选择的列数 问你最大权值是多少,并输出选择的n个列数 思路: DP方程非常好想:DP[i][j] = max(DP[i][j - 1], DP[i - 1][j - 1] + mp[i][j]); 找路径的话,可以每行可以从从i+1到m,也可以直接从i - 1开始找 也可以在DP里面做标记,状态转移的时候将此点记录! 但是不能想的太

poj - 1157 - LITTLE SHOP OF FLOWERS(dp)

题意:F朵花(从左到右标号为1到F,1 <= F <= 100)放入V个花瓶(从左到右标号为1到V,F <= V <= 100),花 i 要放在花 j 的左边,如果i < j,每朵花放入每个花瓶有一个好看度(-50 <= Aij <= 50),求所有花放入花瓶后的最大好看度和. -->>设dp[i][j]表示将前j种花放入前i个花瓶的最大好看度和,则状态转移方程为: dp[i][j] = max(dp[i - 1][j], dp[i - 1][j -

POJ1157 LITTLE SHOP OF FLOWERS DP

题目 http://poj.org/problem?id=1157 题目大意 有f个花,k个瓶子,每一个花放每一个瓶子都有一个特定的美学值,问美学值最大是多少.注意,i号花不能出如今某大于i号花后面.问最大美学值是多少 解题思路 dp[i][j]表示将第i个花插入第k个瓶子的最大美学值. 状态转移方程为dp[i][j] = max(dp[i-1][(i-1)~(k-f+i-1)]) + value[i][j] 代码 #include <cstdio> const int maxn = 110;