poj1157Little Shop of Flowers(动态规划题)
2016-04-18 19:25
281 查看
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 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.
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
Sample Output
大意:按顺序给一些花瓶和鲜花,瓶子多于或等于鲜花。花置于不同的花瓶有不同的审美价值,且每个瓶子最多放一朵鲜花。放置时应注意花瓶的序号依次增加,花的序号依次增加。问如何放使审美价值最高?
dp[ i ][ j ]表示前 i 个花瓶与前 j 朵鲜花产生的价值。
它等于max( dp[ i-1 ][ j-1 ]+record[ i ][ j ] , dp[ i ][ j-1 ] ) ; 前者表示选中第 i 个花瓶,其存放第 j 朵花,后者表示未选中第 i 个花瓶,无法放置第 j 朵花。
二维的数组用来做动态规划是最常见的,先考虑二维,同时题中两个变量与这两个维数要扯上关系。
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 |
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
大意:按顺序给一些花瓶和鲜花,瓶子多于或等于鲜花。花置于不同的花瓶有不同的审美价值,且每个瓶子最多放一朵鲜花。放置时应注意花瓶的序号依次增加,花的序号依次增加。问如何放使审美价值最高?
dp[ i ][ j ]表示前 i 个花瓶与前 j 朵鲜花产生的价值。
它等于max( dp[ i-1 ][ j-1 ]+record[ i ][ j ] , dp[ i ][ j-1 ] ) ; 前者表示选中第 i 个花瓶,其存放第 j 朵花,后者表示未选中第 i 个花瓶,无法放置第 j 朵花。
二维的数组用来做动态规划是最常见的,先考虑二维,同时题中两个变量与这两个维数要扯上关系。
#include<iostream> #include<cstring> #include<stdio.h> using namespace std; int record[120][120]; int dp[120][120]; const int inf=0x3f3f3f3f; int main() { // freopen("in.txt","r",stdin); int F,V; while(cin>>F>>V) { memset(record,0,sizeof(record)); memset(dp,-inf,sizeof(dp)); for(int i=1;i<=F;++i) for(int j=1;j<=V;++j) cin>>record[i][j]; for(int i = 0;i <= 100;i++) dp[0][i] = 0; for(int i=1;i<=F;++i) { for(int j=1;j<=V;++j) { int t1=dp[i][j-1]; int t2=dp[i-1][j-1]+record[i][j]; if(t1>t2) { dp[i][j]=t1; } else { dp[i][j]=t2; } } } cout<<dp[F][V]<<endl; } }
相关文章推荐
- Linux System Calls Hooking Method Summary LD_PRELOAD
- 在CentOS 7上快速安装配置WebSphere MQ 7.5快速指南
- Linux Expect 脚本学习资源
- shell并发处理脚本
- Linux C++编译运行过程
- shell脚本之sed使用详解
- 向正在运行的Linux应用程序注入代码
- Nginx重新编译添加模块
- 配置ganglia 监控 hadoop 节点
- Linux下Nodejs安装(完整详细)
- shell脚本之grep使用详解
- CentOS 6.3 NFS的安装配置、启动及mount挂载方法
- 一个Go推送服务器集群:gopush-cluster
- 工作邮件loop的用法
- centos7 nginx permission denied while connecting to upstream
- mac上配置tomcat9.0
- Linux iptables
- 在MyEclipse上部署Tomcat服务器
- 消失的Java进程-Linux OOM Killer
- Win7 telnet VMware Linux