Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 10554 | Accepted: 4890 |
Description
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.
Input
- The first line contains two numbers: F, V.
- The following F lines: Each of these lines contains V integers, so that Aij is given as the jth number on the (i+1)st line of the input file.
- 1 <= F <= 100 where F is the number of the bunches of flowers. The bunches are numbered 1 through F.
- F <= V <= 100 where V is the number of vases.
- -50 <= Aij <= 50 where Aij is the aesthetic value obtained by putting the flower bunch i into the vase j.
Output
Sample Input
3 5 7 23 -5 -24 16 5 21 -4 10 23 -21 5 -4 -20 20
Sample Output
53
Source
/*
http://acm.pku.edu.cn/JudgeOnline/problem?id=1157
DP
设f(c1, len1)表示1-c1列中,长度为len1的满足题目要求的最大值
则f(c1, len1) = maxv(f(c1 - 1, len1), f(c1 - 1, len1 -1) + input[len1][c1])
所求即为f(col, row)
虽然看上去这个DP比较简单,但却不是很容易一下子想到的
*/
#include <iostream>
#define MAX_N 100
#define maxv(a, b) ((a) >= (b) ? (a) : (b))
using namespace std;
int input[MAX_N + 1][MAX_N + 1];
int maxVal[MAX_N + 1][MAX_N + 1];
int maxCol1[MAX_N + 1];
int row, col;
void getRes()
{
int len, c1;
for(c1 = 0; c1 <= col; c1++)
maxVal[0][c1] = maxVal[c1][0] = 0;
for(len = 1; len <= row; len++)
{
for(c1 = len; c1 <= col; c1++)
{
if(c1 - 1 >= len)
maxVal[c1][len] = maxVal[c1 - 1][len];
maxVal[c1][len] = maxv(maxVal[c1 - 1][len - 1] + input[len][c1], maxVal[c1][len]);
}
}
}
int main()
{
int i, j;
scanf("%d%d", &row, &col);
for(i = 1; i <= row; i++)
{
for(j = 1; j <= col; j++)
{
maxVal[i][j] = INT_MIN;
scanf("%d", &input[i][j]);
}
}
getRes();
printf("%d/n", maxVal[col][row]);
return 0;
}