Problem Solution - Floral Shop Window Arrangement (Luogu: P1854)

flower shop window layout

Topic description:

A flower shop has F bunches of flowers, each of which has different varieties, and at least the same number of vases, which are arranged in a row in order. The position of the vases is fixed, from left to right in the order of 1 to V number, V is the number of the vase. Bouquets can be moved, and each bouquet is identified by an integer from 1 to F. If I < J, bouquet I must be placed in the vase to the left of bouquet J. For example, assuming the ID number of azaleas is 1, the ID number of begonias is 2, and the ID number of carnations is 3, all bouquets must be placed in the vase in the order of their ID numbers, that is, the azaleas must be placed in the vase to the left of the begonias , the begonias must be placed in the vase to the left of the carnations. If the number of vases is greater than the number of bouquets, the excess vases must be empty, that is, only one bouquet of flowers can be placed in each vase.

The shape and color of each vase are also different, so when different bouquets are placed in each vase, different aesthetic effects will be produced, which are expressed in aesthetic value (an integer), and the aesthetic value of empty vase is 0. In the above example, the aesthetic values ​​of different combinations of vases and bouquets can be represented by the following table:

vase 1 vase 2 vase 3 vase 4 vase 5

Rhododendron 7 23 -5 -24 16

Begonias 5 21 -4 10 23

Carnation - 21 5 -4 -20 20

According to the table, the azaleas in vase 2 will look very nice, but in vase 4, they will look ugly.

In order to achieve the best aesthetic effect, the arrangement of the flowers must achieve the maximum aesthetic value under the premise of maintaining the order of the bouquets. If there is more than one arrangement with the maximum aesthetic value, any scheme can be output.

Input and output format

Input format:

The first line of the input file is two integers F and V, the number of bouquets and vases, respectively (1≤F≤100, F≤V≤100). Next is the matrix Aij, which has I rows, each row of J integers, Aij represents the aesthetic value of the bouquet I placed in the vase J.

Output format:

The first line of the output file is an integer, which is the largest aesthetic value; next there are F lines, each with two numbers, for the number of the flower in which vase.

Problem solving ideas:

I won't say much about the meaning of the title, let's get to the point:

The first thing we need to consider is how to describe the state, so there are two ways of describing it:

1. The maximum value of the i-th bunch of flowers placed in the j-th vase

2. Keep the i-th bunch of flowers(take a maximum value between putting and not putting)Maximum value in jth vase

Obviously, it can be seen from the description of the state that the time complexity of the second scheme is lower than that of the first scheme (After all, the second solution only needs to consider two cases)Next, we come to the sub-problems;

We use a two-dimensional array f[i][j] to represent the maximum value of whether the i-th bunch of flowers can be placed in the j-th vase. At this time, it has two sub-problems:

1. Don't put:

The jth vase does not put flowers, then this state can be described as: f[i][j]=f[i][j-1];

2. put

To put flowers in the jth vase, then add the aesthetic value of the ith flower in the jth vase: f[i][j]=f[i-1][j-1] (The title must be placed in the vase in front of it) +a[i][j] (a[i][j] is the aesthetic value)

Here is the code:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=105;
 4 int f[maxn][maxn];
 5 int n,m;
 6 int cost[maxn][maxn];
 7 struct node
 8 {
 9     int a[maxn];
10     int tail;
11 }way[maxn][maxn];//路径
12 int main()
13 {
14     memset(f,-127,sizeof(f));// Note the initialization (the array is infinitely small) 
15      cin>>n>> m;
 16      for ( int i= 1 ;i<=n;i++ )
 17         for ( int j= 1 ;j<=m;j++ )
 18            cin>> cost[i][j];
 19      for ( int i= 0 ;i<=m;i++) // Note initialization*2 
20         f[ 0 ][i]= 0 ;
 21      for ( int i= 1 ;i<=n;i++ )
 22         for (int j=i;j<=m;j++ )
 23         {
 24             if (f[i- 1 ][j- 1 ]+cost[i][j]>f[i][j- 1 ]) // state transfer 
25             {
 26                way[i][j]=way[i- 1 ][j- 1 ];
 27                way[i][j].a[++way[i][j].tail]=j; // Record the path 
28                f[i][j]=f[i- 1 ][j- 1 ]+cost[i][j]; // In the case of the jth vase 
29             }
 30             else // Status Transfer*2 
31            {
 32                way[i][j]=way[i][j- 1 ]; // record path 
33                f[i][j]=f[i][j- 1 ]; // not in jth 
34             }
 35         }
 36 cout      <<f[n][m]<< endl;
 37      for ( int i= 1 ;i<=way[n][m].tail;i++ )
 38          cout<< way[n][m].a[i]<< "  " ; // print path 
39      return  0 ;
 40 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325853415&siteId=291194637