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 consecutively 1 through V, where V is the number of vases, from left to right so that the vase 1 is the leftmost, and the vase V is the rightmost vase. The bunches are moveable and are uniquely identified by integers between 1 and F. These id-numbers have a significance: They determine the required order of appearance of the flower bunches in the row of vases so that the bunch i must be in a vase to the left of the vase containing bunch j whenever i < j. Suppose, for example, you have bunch of azaleas (id-number=1), a bunch of begonias (id-number=2) and a bunch of carnations (id-number=3). Now, all the bunches must be put into the vases keeping their id-numbers in order. The bunch of azaleas must be in a vase to the left of begonias, and the bunch of begonias must be in a vase to the left of carnations. If there are more vases than bunches of flowers then the excess will be left empty. A vase can hold only one bunch of flowers.

Each vase has a distinct characteristic (just like flowers do). Hence, putting a bunch of flowers in a vase results in a certain aesthetic value, expressed by an integer. The aesthetic values are presented in a table as shown below. Leaving a vase empty has an aesthetic value of 0.

 

V A S E S

1

2

3

4

5

Bunches

1 (azaleas)

7 23 -5 -24 16

2 (begonias)

5 21 -4 10 23

3 (carnations)

-21

5 -4 -20 20

According to the table, azaleas, for example, would look great in vase 2, but they would look awful in vase 4.

To achieve the most pleasant effect you have to maximize the sum of aesthetic values for the arrangement while keeping the required ordering of the flowers. If more than one arrangement has the maximal sum value, any one of them will be acceptable. You have to produce exactly one arrangement.

Input

  • The first line contains two numbers: F, V.
  • The following F lines: Each of these lines contains V integers, so that Aij is given as the jth number on the (i+1)st line of the input file.
  • 1 <= F <= 100 where F is the number of the bunches of flowers. The bunches are numbered 1 through F.
  • F <= V <= 100 where V is the number of vases.
  • -50 <= Aij <= 50 where Aij is the aesthetic value obtained by putting the flower bunch i into the vase j.

Output

The first line will contain the sum of aesthetic values for your arrangement.

Sample Input

3 5
7 23 -5 -24 16
5 21 -4 10 23
-21 5 -4 -20 20

Sample Output

53

题目意思:

每种花有一个编号,有几个花瓶,花瓶也是有编号的,每种花放在不同的花瓶的艺术价值不一样,
要你把这些花按某种策略放在这些花瓶里,得到艺术价值总值最大,
并且,编号小的花所在的花瓶的编号要比编号大的花所在花瓶的编号小。   我的思想: n=花种类,m=花瓶种类
输入数据的范围告诉你:n<=m
保证了插花方案的多样性
a[i][j]:表示第i朵花放第j个花瓶的艺术值
所以我们想象一下
1.前i种花放入前j个花瓶
2.前i种花放入前j-1个花瓶(要求编号小的花所在的花瓶的编号要比编号大的花所在花瓶的编号小。) 设dp[i][j]:表示前面i朵花放前面j个瓶子里面的艺术总值
1.对于前i种花放入前j个花瓶这种方案:就是前面的i-1种花放入前面j-1种花瓶里面,然后第i种花放入第j种花瓶里面
所以:dp[i][j]=dp[i-1][j-1]+a[i][j] 2.前i中花放入前j-1个花瓶
所以:dp[i][j-1] 取两种方案里面最大的那个
所以:
dp[i][j]=max(dp[i-1][j-1]+a[i][j],dp[i][j-1]) 注意初始化的时候
dp[i][i]=dp[i-1][i-1]+a[i][i]
对角线初始化
因为第i朵花不能插编号i前面的花瓶
所以应该这样初始化!!! code:
#include <iostream>
#include <cstdio>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<memory>
#include<queue>
#include<vector>
using namespace std;
#define max_v 105
#define INF 99999999
int dp[max_v][max_v];//前面i朵花放前面j个瓶子里面的艺术总值
int a[max_v][max_v];//第i朵花放第j个瓶子的艺术值
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
scanf("%d",&a[i][j]);
}
dp[i][i]=dp[i-][i-]+a[i][i];
}
for(int i=;i<=n;i++)
{
for(int j=i+;j<=m;j++)
{
dp[i][j]=max(dp[i-][j-]+a[i][j],dp[i][j-]);
}
}
printf("%d\n",dp[n][m]);
}
return ; }

思路二:

现在我们从另外一个角度思考

dp[i][j]:现在放第i种花到第j种瓶子里面得到的当前艺术总值

我们从第一种花开始放,那么我们需要的结果就是看把最后一朵花放入到哪个瓶子里面得到的艺术值中最大的哪个艺术值

dp[i][j]=max(dp[i-1][k])+a[i][j]

i-1<=k<j

所以说就是找一个最好的k,要使得当前的状态是最好的,那么我们就要找一个最好的上一个状态加上当前的值,那么这样当前的状态就是最好的

所以就是找一个合适的k

比如现在将第i种花插入到j种花瓶里面,在上一个状态中(插第i-1朵花)找一个合适的花瓶k使得上一个状态是最优的!

那就当前状态就是上面的最优状态加上当前i花插j瓶值的状态也是最优的

所以先开始初始化,我们知道每一种状态都是继承与上一种状态,插第一种花的时候,前面没有状态了,是继承不了的,算不出来,所以我们应该初始化给它赋值 dp[1][j]=a[1][j] j属于1到m

从第二种花开始算,在前面的状态中(插第一朵花)找个最优的加上i花插j瓶的值,那么当前状态就是最优的

注意数据范围:

算的时候i是2到n

j是i到m(要求编号小的花所在的花瓶的编号要比编号大的花所在花瓶的编号小)

k是i-1到j-1,不能取j,因为j是当前状态的值,你只能在前面找一个最好状态的k,不能找当前的嘛

注意理解状态转移方程:

dp[i][j]=max(dp[i-1][k])+a[i][j]

i-1<=k<j

code:

#include <iostream>
#include <cstdio>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<memory>
#include<queue>
#include<vector>
using namespace std;
#define max_v 105
#define INF 99999999
int dp[max_v][max_v];//现在放第i种花到第j种花瓶的艺术值
int a[max_v][max_v];//第i朵花放第j个瓶子的艺术值
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
scanf("%d",&a[i][j]);
}
}
memset(dp,,sizeof(dp));
for(int j=;j<=m;j++)//初始化
dp[][j]=a[][j];//第1种花放第j种花瓶中的艺术值
for(int i=;i<=n;i++)//从第二种花开始放
{
for(int j=i;j<=m;j++)//第i种花放的花瓶编号不能小于i,所以j属于i到m
{
int x=-INF;
for(int k=i-;k<j;k++)//找k
{
x=max(x,dp[i-][k]);
}
dp[i][j]=x+a[i][j];
}
}
int ans=-INF;
for(int j=n;j<=m;j++)//找max
{
ans=max(ans,dp[n][j]);
}
printf("%d\n",ans);
}
return ; }

POJ 1157 LITTLE SHOP OF FLOWERS (超级经典dp,两种解法)的更多相关文章

  1. Little shop of flowers - SGU 104 (DP)

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

  2. POJ 1080:Human Gene Functions LCS经典DP

    Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18007   Accepted:  ...

  3. POJ 1182食物链(分集合以及加权两种解法) 种类并查集的经典

    题目链接:http://icpc.njust.edu.cn/Problem/Pku/1182/ 题意:给出动物之间的关系,有几种询问方式,问是真话还是假话. 定义三种偏移关系: x->y 偏移量 ...

  4. POJ 1979 dfs和bfs两种解法

      fengyun@fengyun-server:~/learn/acm/poj$ cat 1979.cpp #include<cstdio> #include<iostream&g ...

  5. 【LA3487】最小割-经典模型 两种方法

    题目链接 题意:A.B两个公司要买一些资源(他们自己买的资源不会重复),一个资源只能卖给一个公司.问最大收益. simple input 部分: 54 1 //买到1就给54元 15 2 33 3 2 ...

  6. poj 3311 floyd+dfs或状态压缩dp 两种方法

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6436   Accepted: 3470 ...

  7. POJ 1515 Street Directions --一道连通题的双连通和强连通两种解法

    题意:将一个无向图中的双向边改成单向边使图强连通,问最多能改多少条边,输出改造后的图. 分析: 1.双连通做法: 双连通图转强连通图的算法:对双连通图进行dfs,在搜索的过程中就能按照搜索的方向给所有 ...

  8. 一道JAVA经典面试题目的两种解法

    题目要求:String s="-1 2 5 78 129 -65 -23";将字符串进行升序排序后输出. 方法一:使用数组进行排序 思路: 1.获取字符串中的数值:   2.将数组 ...

  9. sgu 104 Little shop of flowers 解题报告及测试数据

    104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB 问题: 你想要将你的 ...

随机推荐

  1. 自定义scoll样式

    使用伪类自定义scroll样式 效果: 代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8 ...

  2. Bzoj2395: [Balkan 2011]Timeismoney(最小乘积生成树)

    问题描述 每条边两个权值 \(x,y\),求一棵 \((\sum x) \times (\sum y)\) 最小的生成树 Sol 把每一棵生成树的权值 \(\sum x\) 和 \(\sum y\) ...

  3. Django请求生命周期之响应内容

    Django请求生命周期: 1.发送http请求2.服务器接受,根据请求头中的url在路由关系表中进行匹配(从上到下)3.匹配成功后,执行指定的views函数 URL -> 函数 ==>F ...

  4. LintCode2016年算法比赛----二叉树的所有路径

    二叉树的所有路径 题目描述 给定一棵二叉树,找从根节点到叶子节点的所有路径 样例 给出下面这课二叉树: 1 / \ 2 3 \ 5 所有根到叶子的路径为: [ "1->2->5& ...

  5. androidandroid中的通过网页链接打开本地app

    http://blog.csdn.net/zjlovety/article/details/54847980 <html> <head> <meta http-equiv ...

  6. Protocol Buffer学习笔记

    Protocol Buffer Protobuf基础概念 Protobuf是google开发的数据结构描述语言,能够将结构化数据序列化与反序列化,取代json和xml,常用于服务器通信协议.RPC系统 ...

  7. AspNetCore发布到Centos7

    1.Centos安装netcore2 sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc sudo sh -c 'e ...

  8. 学习笔记:IIS搭建PHP网站出现404错误的解决办法

    关于404错误提示相信大家都遇到过吧,记得我遇到这个问题的时候,弄得我焦头烂额的,今天给大家分享下,使用IIS大家PHP网站时出现404错误提示的处理方法,希望对各位朋友有所帮助.IIS搭建PHP出现 ...

  9. Oracle案例09——ORA-12154: TNS:could not resolve the connect identifier specified

    DG处理的问题还是蛮多的,但这次遇到一个比较奇葩的事情,表面配置.网络都没啥问题,但主备的同步始终有问题,经过多次调整参数.重新部署问题依旧,最终还是求助mos问题得以解决,现将处理过程记录如下: 一 ...

  10. 软件磁盘阵列 (Software RAID)

    什么是 RAID 磁盘阵列全名是『 Redundant Arrays of Inexpensive Disks, RAID 』,容错式廉价磁盘阵列. RAID 可以通过一些技术(软件或硬件),将多个较 ...