Florist shop window dynamic programming solution to a problem

what! A full hour to bring up this road card dead dynamic programming problem ~

Very emotional

 

Title reads as follows

1090: B10- dynamic programming - florist shop window

Time limit: 1 Sec Memory Limit: 128 MB
submit: 13 Solution: 7
[ submit ] [ state ] [ Discussion Board ] [proposition man: External Import]

Title Description

    xq and xz his wife recently opened a flower shop, they are ready to shop the best looking flowers placed in the window. But they have a lot of vases, each vase has its own characteristics, therefore, when each vase into a different bouquet, will produce different aesthetic effects. In order to put flowers in the window of the most appropriate, they must find a way to arrange the placement of each flower.
    But because xq and xz are busy every day, no time to design windows pendulum method to spend, so they want you to help them find the maximum extent beautiful and each flower placed flowers put in the position.
    Note: The label must be placed flowers in front of a big label.
 
 
    Each flowers on different bottle will produce different aesthetic level, aesthetic appearance may be a positive number may also be negative.
 
    In the above example, the vase has a bouquet with a different aesthetic level, as shown in the table:
 
                             Flower Bottle
 
                      1     2    3    4    5
       1 (Azalea) 723-5-2416
       2 (Begonia) 521-41023
       3 (Carnation) -215-4-2020
 
 
    According to the table, placed in a vase azalea 2, it will look very nice; but if placed in a vase in four is very ugly.
 
 

 

Entry

Line 1: Two integers F and V, xq and xz represent a total of flowers F, V vase. (1 <= F <= V <= 100)
Row 2 to row F + 1: the number per line V, indicates that the flower vase placed in a different degree of aesthetic value value. (Beauty degree and not more than maxint, aesthetic appearance positive or negative.)
 

 

Export

There are two output lines: first line of output to maximize the appearance and value, the second line has the number of F indicates the number of each flower vases should be placed.

 

Sample input

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

 

Sample Output

53
2 4 5

 

prompt



 
 

 

Source / classification

 

 

 

My idea is this problem by dp [i] [j] to represent the i-th flowers on the j-th position to the maximum cumulative value of this flower obtained

 

 


//f[i][j]=max(f[i][j],f[i-1][k]+v[i][j])
#include<bits/stdc++.h> using namespace std; int v[105][105],dp[105][105]; int f,V; void Printout(int maxn,int i,int j){ if(i==0) return; for(int k=1;k<=V;k++) if(dp[i-1][k]+v[i][j]==maxn){ Printout(dp[i-1][k],i-1,k); break; } cout<<j<<" "; } int main(){ cin>>f>>V; for(int i=1;i<=f;i++) for(int j=1;j<=V;j++) scanf("%d",&v[i][j]),dp[i][j]=-10000; for(int i=1;i<=f;i++){ for(int j=i;j<=V-(f-i);j++){ for(int k=i-1;k<j;k++){ dp[i][j]=max(dp[i][j],dp[i-1][k]+v[i][j]); } } } int maxn=-1; for(int i=f;i<=V;i++) maxn=max(maxn,dp[f][i]); int ii,jj; for(int i=f;i<=V;i++) if(dp[f][i]==maxn){ ii=f; jj=i; break; } cout<<maxn<<endl; Printout(maxn,ii,jj); return 0; }

这一道题死活不过整理了如下几个错误原因

1.没有处理好每层循环的边界

2.没有把dp数组初始化!?我也不知道是什么bug 反正不初始化就会出各种神奇的错误

3.我那个神奇的递归打印路径函数 一开始在主函数里传的参数错了  我一开始写的Printout(maxn,f,V) 但是最后一种花不一定放在最后一个位置鸭

 

加油 继续努力 梦想一定就在不远的前方~

Guess you like

Origin www.cnblogs.com/Tidoblogs/p/11257360.html