poj1157

来源:互联网 发布:手机怎样看淘宝的积分 编辑:程序博客网 时间:2024/05/01 19:11

http://poj.org/problem?id=1157

LITTLE SHOP OF FLOWERS
Time Limit: 1000MSMemory Limit: 10000KTotal Submissions: 14050Accepted: 6467

Description

You want to arrange the windowof your flower shop in a most pleasant way. You have F bunches offlowers, each being of a different kind, and at least as many vasesordered in a row. The vases are glued onto the shelf and arenumbered 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 vaseV is the rightmost vase. The bunches are moveable and are uniquelyidentified by integers between 1 and F. These id-numbers have asignificance: They determine the required order of appearance ofthe flower bunches in the row of vases so that the bunch i must bein 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 ofcarnations (id-number=3). Now, all the bunches must be put into thevases keeping their id-numbers in order. The bunch of azaleas mustbe in a vase to the left of begonias, and the bunch of begoniasmust be in a vase to the left of carnations. If there are morevases than bunches of flowers then the excess will be left empty. Avase 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 certainaesthetic value, expressed by an integer. The aesthetic values arepresented in a table as shown below. Leaving a vase empty has anaesthetic value of 0.

V A S E S

1

2

3

4

5

Bunches

1 (azaleas)

723-5-2416

2 (begonias)

521-41023

3 (carnations)

-21

5-4-2020
According to the table, azaleas, for example, would look great invase 2, but they would look awful in vase 4.

To achieve the most pleasant effect you have to maximize the sum ofaesthetic values for the arrangement while keeping the requiredordering of the flowers. If more than one arrangement has themaximal sum value, any one of them will be acceptable. You have toproduce exactly one arrangement.

Input

  • The first line contains two numbers: F, V.
  • The following F lines: Each of these lines containsV integers, so that Aij is given as thejth number on the(i+1)st line of the input file.


  • 1 <= F <= 100 where F is thenumber of the bunches of flowers. The bunches are numbered 1through F.
  • F <= V <= 100 where V is thenumber of vases.
  • -50 <= Aij <= 50 where Aij is theaesthetic value obtained by putting the flower bunch i into thevase j.

Output

The first line will contain thesum of aesthetic values for your arrangement.

Sample Input

3 57 23 -5 -24 165 21 -4 10 23-21 5 -4 -20 20

Sample Output

53

Source

IOI 1999

动态方程很关键,,方程的初始化也很必要(鹏哥讲解的)。。

#include<stdio.h>#include<string.h>#define maxn 210int f[maxn][maxn],v[maxn][maxn];int MAX(int a,int b){        return a>b?a:b;}int main(){        int F,V;        while(scanf("%d %d",&F,&V)!=EOF)        {                int i,j;                memset(f,0,sizeof(f));                memset(v,0,sizeof(v));                for(i=0;i<F;i++)                {                        for(j=0;j<V;j++)                        {                                scanf("%d",&v[i][j]);                        }                }                for(j=0;j<=V;j++)                        f[0][j]=0;                for(i=0;i<F;i++)                        f[i][i]=f[i-1][i-1]+v[i][i];                for(i=0;i<F;i++)                {                        for(j=i+1;j<V;j++)                        {                                f[i][j]=MAX(f[i][j-1],f[i-1][j-1]+v[i][j]);                        }                }                printf("%d\n",f[F-1][V-1]);        }        return 0;}
 
另一种初始化(我更喜欢)
#include<stdio.h>#include<string.h>#define maxn 210int f[maxn][maxn],v[maxn][maxn];int MAX(int a,int b){        return a>b?a:b;}int main(){        int F,V;        while(scanf("%d %d",&F,&V)!=EOF)        {                int i,j;                memset(f,0,sizeof(f));                memset(v,0,sizeof(v));                for(i=0;i<F;i++)                {                        for(j=0;j<V;j++)                        {                                scanf("%d",&v[i][j]);                        }                }
                for(i=0;i<F;i++)                {                    for(j=0;j<V;j++)                    f[i][j]=-100000000;                }                for(i=0;i<F;i++)                {                    for(j=0;j<V;j++)                    f[i][j]=f[i-1][j-1]+v[i][j];                }                for(i=0;i<F;i++)                {                        for(j=i+1;j<V;j++)//从对角线开始                        {                                f[i][j]=MAX(f[i][j-1],f[i-1][j-1]+v[i][j]);                        }                }                printf("%d\n",f[F-1][V-1]);        }        return 0;}