DP-based data Tower Model poj1157

Source: Internet
Author: User

Question: poj1157Little Shop of flowers


Assume that the window of the flower shop is arranged in the most beautiful way, and there are f flowers. The variety of each flower is different. At the same time, there are at least the same number of vases which are placed in a line in order, the vase is fixed and numbered from left to right, from 1 to V, and V is the number of vases. The vase numbered 1 is on the leftmost, the vase numbered V is on the rightmost side. The bouquet can be moved and each bouquet is uniquely identified by an integer ranging from 1 to F, the integer that identifies the bouquet determines the order of the bouquet in the vase. That is, if I <j, then the bouquet I must be placed in the vase on the left of the bouquet J. For example, assume that the number of logo is 1, the number of logo is 2, and the number of logo is 3. All the flowers must be in the order of the number of Logo when they are placed in the vase. That is: the azalea must be placed in the vase on the left of the begonia, and The Begonia must be placed in the vase on the left of the carnation. If the vase number is greater than the number of the bouquet, the remaining vase must be empty, that is, each vase can only put a bunch of flowers. The shape and color of each vase are also different. Therefore, different aesthetic effects are produced when different flowers are placed in each vase and expressed as an aesthetic value (an integer, the Aesthetic Value of the empty vase is 0.

In the above example, the aesthetic values of the vase and the bouquet can be expressed in the following table. According to the table, the azalea flower in vase 2 looks very nice, but it looks ugly if it is in vase 4.
To achieve the best aesthetic effect, you must keep the order of the bouquet to maximize the aesthetic value of the flowers. If there are more than one way to place the maximum aesthetic value, then, output any solution. The data in the question meets the following conditions: 1 ≤ F ≤ 100, F ≤ v ≤ 100,-50 ≤ AIJ ≤ 50, aII is the aesthetic value of the bouquet I placed in the vase J. Enter the integer F, V, and matrix (AIJ) to output the maximum aesthetic value and the vase number in each vase.

── ┬ ── ─ ┬ ── ┐
│ Vase 1 │ vase 2 │ vase 3 │ vase 4 │ vase 5 │
── ┼ ── ─ ┼ ── ┤
│ Azalea │ 7 │ 23 │-5 │-24 │ 16 │
── ┼ ── ─ ┼ ── ┤
│ │ 5 │ 21 │-4 │ 10 │ 23 │
── ┼ ── ─ ┼ ── ┤
│ Carnation │-21 │ 5 │-4 │-20 │ 20 │

── ┴ ── ─ ┴ ── ┘


Analysis: it is obvious that we can move to the data tower model to achieve the greatest aesthetic value, so I want the current one to achieve the greatest aesthetic value, and then next according to the current transfer, A simple DP question.

We can define the State DP [I] [J] to indicate the maximum aesthetic value obtained from putting the first I flower and the last put J, then the transfer equation

DP [I] [J] = DP [[I] [J] + dp [I-1] [J-1]

And each line of DP [I] [J] = max (DP [I] [J], DP [I] [J-1]

Finally, ANS is at the maximum value in the DP [v] [I] Line.


Simple questions:

1: if there is only one vase for a flower

2: The aesthetic values may all be negative. How can this problem be solved?

3: The vase must be inserted for each flower to be cut.


Point Data:

Input:

3 5
-5-5-5-5-5
-5-5-5-5-5
-5-5-5-5-5


1 5
7 23-5-24 16


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


2 2
1 15
2 10


1 1
-10


1 1
200

Output:

-15

23

64

11

-10

200

Code:

# Include <iostream> # include <cstring> # include <cstdio> # define max (A, B) A> B? A: busing namespace STD; const int INF = 0x3f3f3f3f; const int n = 110; int map [N] [N]; int dis [N] [N]; int V, f; int main () {// freopen ("input.txt", "r", stdin); While (~ Scanf ("% d", & V, & F) {for (INT I = 1; I <= V; I ++) {for (Int J = 1; j <= f; j ++) scanf ("% d", & map [I] [J]);} for (INT I = 2; I <= f; I ++) map [1] [I] = max (Map [1] [I], map [1] [I-1]); For (INT I = 2; I <= V; I ++) {for (Int J = I; j <= F; j ++) {map [I] [J] = map [I] [J] + map [I-1] [J-1]; if (Map [I] [J] <0 & map [I] [J-1] <0) // process negative values map [I] [J] = min (Map [I] [J], map [I] [J-1]); else map [I] [J] = max (Map [I] [J], map [I] [J-1]);} // printf ("\ n ");} int MA =-INF, MI = inf; For (INT I = 2; I <= f; I ++) {If (Map [v] [I]> Ma) MA = map [v] [I]; If (Map [v] [I] <mi) MI = map [v] [I];} if (MA =-INF) Ma = map [1] [1]; printf ("% d \ n", Ma);} return 0 ;}


DP-based data Tower Model poj1157

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.