HEU 5019 Max

/**************************************
Problem: HEU 5019 Max
Time: 0.0030 s
Memory: 552 k 
Accepted Time: 2009-04-28 11:46:04
Tips: 因为有负数,所以要考虑最小值 
*************************************
*/

#include 
< stdio.h >
char  b[ 110 ];
long  max[ 110 ][ 110 ],min[ 110 ][ 110 ];
long  fun( int  a1, int  a2, int  a3, int  a4, int  flag)
{
    
long sum;
    
switch(flag)
    
{
        
case 1:
            
if(b[a2]=='+')sum=max[a1][a2]+max[a3][a4];
            
else sum=max[a1][a2]*max[a3][a4];
            
break;
        
case 2:
            
if(b[a2]=='+')sum=min[a1][a2]+min[a3][a4];
            
else sum=min[a1][a2]*min[a3][a4];
            
break;
        
case 3:
            
if(b[a2]=='+')sum=min[a1][a2]+max[a3][a4];
            
else sum=min[a1][a2]*max[a3][a4];
            
break;
        
case 4:
            
if(b[a2]=='+')sum=max[a1][a2]+min[a3][a4];
            
else sum=max[a1][a2]*min[a3][a4];
            
break;        
    }

    
return sum;
}

int  main()
{
    
int n;
    
while(scanf("%d",&n)!=EOF)
    
{
        
int i,j,k,t;
        
for(i=1;i<=n;i++)
        
{
            scanf(
"%ld",&max[i][i]);
            min[i][i]
=max[i][i];
            
if(i!=n)scanf("%s",&b[i]);
        }

        
long temp;
        
for(i=n-1;i>=1;i--)
        
{
            
for(j=1;j<=i;j++)
            
{
                
long max1=-100000000;
                
long min1=100000000;
                
for(k=j;k<j+n-i;k++)
                
{
                    
for(t=1;t<5;t++)
                    
{
                        temp
=fun(j,k,k+1,j+n-i,t);
                        
if(temp>max1)max1=temp;
                        
if(temp<min1)min1=temp;
                    }

                }

                max[j][j
+n-i]=max1;
                min[j][j
+n-i]=min1;    
            }

        }

        printf(
"%ld\n",max[1][n]);
    }

    
return 0;
}

你可能感兴趣的:(max)