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];

void print(int now, int place)
{
if (now == 1) printf("%d ", place);
else
{
print(now-1, fa[now][place]);
if (now != n) printf("%d ", place);
else printf("%d\n", place);
}
}

int main()
{
scanf("%d%d", &n, &m);
memset(val, 0, sizeof(val));
memset(f, -0x7f, sizeof(f));
for (int i = 0; i <= m; ++i) f[0][i] = 0;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
scanf("%d", &val[i][j]);
for (int i = 1; i <= n; ++i)
for (int j = i; j <= m-n+i; ++j)
for (int k = i-1; k < j; ++k)
if (f[i][j] < f[i-1][k] + val[i][j])
{
f[i][j] = f[i-1][k] + val[i][j];
fa[i][j] = k;
}
int ans, place;
ans = -inf;
for (int i = n; i <= m; ++i)
if (ans < f[n][i])
{
ans = f[n][i];
place = i;
}
printf("%d\n", ans);
print(n, place);
}

sgu 104 Little Shop of Flowers,布布扣,bubuko.com

时间: 2024-10-07 17:55:53

sgu 104 Little Shop of Flowers的相关文章

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][

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

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

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]

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]),也就是这个花瓶要还是不要,别忘记输出路径. 代码如下: =======================================================

【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里面做标记,状态转移的时候将此点记录! 但是不能想的太

[POJ1157]LITTLE SHOP OF FLOWERS

试题描述 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 a row. The vases are glued onto the shelf and are numbered consecutiv

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 -