SGU 104. Little shop of flowers (DP)

时间:2014-02-09 15:25:37   收藏:0   阅读:351

104. Little shop of flowers

time limit per test: 0.25 sec. 
memory limit per test: 4096 KB

 

 

PROBLEM

You want to arrange the window of your flower shop in a most pleasant way. You have 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

 

 

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.

ASSUMPTIONS

 

 

 

 

 

 

 

Input

 

 

 

 

 

 

 

Output

 

 

 

 

 

 

 

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

 

 

 

 

题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=104

 

 

直接进行DP,记录下路径。

dp[i][j] 表示前i个花,放在前j个花瓶得到的最大值。

dp[i][j] 可以由dp[i][j-1] 和  dp[i-1][j-1]转移过来,记录路径就可以了

 

bubuko.com,布布扣
 1 /* ***********************************************
 2 Author        :kuangbin
 3 Created Time  :2014-2-7 23:42:44
 4 File Name     :E:\2014ACM\SGU\SGU104.cpp
 5 ************************************************ */
 6 
 7 #include <stdio.h>
 8 #include <string.h>
 9 #include <iostream>
10 #include <algorithm>
11 #include <vector>
12 #include <queue>
13 #include <set>
14 #include <map>
15 #include <string>
16 #include <math.h>
17 #include <stdlib.h>
18 #include <time.h>
19 using namespace std;
20 const int INF = 0x3f3f3f3f;
21 int a[110][110];
22 int dp[110][110];
23 int pre[110][110];
24 vector<int>ans;
25 void solve(int n,int m)
26 {
27     if(n == 0)return;
28     if(pre[n][m] == 0)
29     {
30         solve(n-1,m-1);
31         ans.push_back(m);
32     }
33     else if(pre[n][m] == 1) solve(n,m-1);
34 }
35 
36 int main()
37 {
38     //freopen("in.txt","r",stdin);
39     //freopen("out.txt","w",stdout);
40     int n,m;
41     while(scanf("%d%d",&n,&m) == 2)
42     {
43         for(int i = 1;i <= n;i++)
44             for(int j = 1; j <= m;j++)
45                 scanf("%d",&a[i][j]);
46         for(int i = 0;i <= n;i++)
47             for(int j = 0;j <= m;j++)
48                 dp[i][j] = -INF;
49         for(int i = 0;i <= m;i++)
50             dp[0][i] = 0;
51         for(int i = 1;i <= n;i++)
52             for(int j = 1;j <= m;j++)
53             {
54                 int t1 = dp[i-1][j-1] + a[i][j];
55                 int t2 = dp[i][j-1];
56                 if(t1 >= t2)
57                 {
58                     dp[i][j] = t1;
59                     pre[i][j] = 0;
60                 }
61                 else
62                 {
63                     dp[i][j] = t2;
64                     pre[i][j] = 1 ;
65                 }
66             }
67         printf("%d\n",dp[n][m]);
68         ans.clear();
69         solve(n,m);
70         for(int i = 0;i < n;i++)
71         {
72             printf("%d",ans[i]);
73             if(i < n-1)printf(" ");
74             else printf("\n");
75         }
76     }
77     return 0;
78 }
bubuko.com,布布扣

原文:http://www.cnblogs.com/kuangbin/p/3540460.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!