Window arrangement (flower) (dp)

Description] [Title
assumed window arranged in the most beautiful flower shop, the bunch of flowers F has, for each species is different bouquet, at the same time, at least the same number of vases, are sequentially put in a row, the position of the vase fixed, and left to right, sequentially numbered from 1 to V, V is the number of the vase, vase number 1 in the left, the number V rightmost vase, bouquet may move, and each with a bunch of flowers F is an integer that uniquely identifies the integer bouquet identity determines the order of the columns in the vase bouquet that is, if i <j, i must be placed in the vase bouquet bouquet left of j.

For example, suppose the number is 1 logo azaleas, begonias identification number is 2, the identification number is three carnations, bouquets all must remain the order of the number of identity at the time of release vase, namely: Azalea must be placed on the left of begonias vase, begonias must be placed in a vase of carnations left. If the number is greater than the vase bouquet, the excess must be empty vases, i.e. only each discharge a bunch of flowers in the vase.

Each vase shape and color are not the same, therefore, when the respective different bouquets put the vase will produce different aesthetic effects, and the aesthetic value (an integer) is represented, the aesthetic value of 0 empty vases. In the above example, the vase has a bouquet with different aesthetic value may be represented by the following table.

According to the table, azaleas put in vases 2, will look very nice, but if placed in a vase in four look ugly.

For best aesthetic effect, must be the premise of maintaining order in the bouquet, the flowers placed to achieve maximum aesthetic value placed way if you have more than one of the greatest aesthetic value, any program can be output. Data in question following condition is satisfied: 1≤F≤100, F≤V≤100, -50≤Aij≤50, where Aij is placed in a vase bouquet i j is the aesthetic value. Enter an integer F, V, and the matrix (Aij), and outputs the maximum value for each aesthetic flowers placed in each vase vase number.

Vase 1 Vase 2 Vase vase 3 4 5 vases
azalea 723-5-2416
begonias 521-41023
carnation -215-4-2020
assumptions:

1≤F≤100, where F is the number of Bouquet, numbered from 1 to F bouquet.

F≤V≤100, where V is the number of the vase.

-50≤Aij≤50, where Aij is the aesthetic value i bouquet in the vase j.

[Input]
The first line contains two numbers: F, V.

F subsequent lines, each line contains V integers, Aij of the input file is the first (i + 1) number of the j-th row.

[Output]
The first row is placed way aesthetic value generated by the program.

The second line must be expressed by way of placing the number of F, ie the number K of the line indicates the number of vase bouquet K is located.

[Sample] input
. 3. 5
. 7 -24 23 is 16 -5
. 5 10 -4 21 is 23 is
-21. 5 -20 -4 20 is
[output] Sample
53 is
2. 4. 5
Title Analysis: for dynamic programming class title done so much I found a feature that has done the same type of problem can be quickly done, did not make the problem even write the state transition equation will be stuck in a lot of details, like the title, because of the existence of a negative number , you need to initialize a negative value, at the beginning I used memset function assignment always a point was stuck, because I changed an hour, I finally found a statement on memset function in the solution to a problem one, memset is byte copy, can only be 0 or 1, and some other value I do not know, in short, finally memset into a for loop assignment finally passed.
State transition equation:
provided DP [f] [v] denotes the f v flowers into a bottle landscaping maximum value. So this is equivalent to f-1 v-1 bouquet of flowers into the bottle, and then the first f bouquet of flowers into the bottle of this v-1 outside of the bottle, so here we must traverse all possible cases, specific the range of the own room, to consider the situation carefully point will be able to find more comprehensively. Therefore dp [f] [v] =
max (dp [f] [v], dp [f-1] [k-1] + a [f] [k]) where k <= v && k> = f
on output scheme , typically are used in the recursion dp outputs, in order to stack the output flashback.
Code:

#include<iostream>
using namespace std;
int dp[101][101];
int a[101][101];
int maxl;
void print(int);
int main()
{
    int f,v;
    cin>>f>>v;
    for(int i=1;i<=f;i++)
        for(int j=1;j<=v;j++)
        cin>>a[i][j];
    for(int i=1;i<=f;i++)
        for(int j=1;j<=v;j++)
        dp[i][j]=-99999;
    for(int i=1;i<=100;i++)
        dp[0][i]=0;
    for(int i=1;i<=f;i++)
        for(int j=i;j<=v-f+i;j++)
        for(int k=i;k<=j;k++)
        dp[i][j]=max(dp[i-1][k-1]+a[i][k],dp[i][j]);
    cout<<dp[f][v]<<endl;
    maxl=dp[f][v];
    print(f);
    return 0;
}
void print(int h)
{
    if(h==0) return;
    int i=h;
    while(dp[h][i]!=maxl)
    {
        i++;
    }
    maxl-=a[h][i];
    print(h-1);
    cout<<i<<" ";
}
Published 42 original articles · won praise 42 · views 9316

Guess you like

Origin blog.csdn.net/amazingee/article/details/104451197