Polygon Games - interval dp

Title Description

Polygons (Polygon) is a single game to play the game, the beginning of a given polygon composed of a N vertices (in the example shown in FIG. 1, N = 4), each vertex is assigned an integer value, and each edges were given a symbol: + (addition operation), or * (multiplication), successively all sides integer 1 to N identification.image

Figure 1: a graphic representation of the polygon first movement (first move), will be deleted to allow one edge; each subsequent shift sequence (subsequentmoves), comprising the following steps:

1, select an edge E, and vertex E V1 and V2 by the coupling;

2, with a new vertex, the edge E and substituted two vertices V1 and V2 are coupled. New vertices to be given a new value, this value is V1 and V2, designated E made by calculation, the results obtained.

After all the edges have been removed, leaving only a vertex, the game ends. Game score is the value of the vertex. The following is an example of the game: a polygon consider Figure 1.

The first step of players to delete Article 3 sides. The results shown in Fig. image FIG 2 sides deleting article 3

After that, players deletion of article 1 edge  image 3 1 side deletion of article

Then remove the article fourth side,  image FIG. 4 delete article 4 sides

Finally, the deletion of article 2 sides. Score is 0.  image FIG 5 the second side section to delete assignment: write a program, for any given polygon, calculating the highest possible score, and can include all of the results in the highest score is moved edge first.

Input Format

POLYGON.IN file is given, a polygon of N of vertices. File comprising 2 rows: a first row record is the value N (n <= 50); the second row contains all the symbols sides (1, ..., N) are given, and fitted into the vertex between the two sides value (the value corresponding to an integer number 1, 2 vertices simultaneously connected to the side; a second integer corresponding to the number 2, 3 vertices while connected to the side; ...;., etc. Finally, a value corresponding to the N number, while the number attached to one side of the apex), and the values ​​between the symbols separated by a space. There are two sides of the symbol: letter T (corresponding to +), letters x (corresponding to *).

Output Format

In the first line of the file POLYGON.OUT, your program must output the highest score in the input file specified conditions may get. If some edges are removed in the first move, it can lead to the highest score. In the second line of the output file, such claims include all sides, and the output in the ascending order, separated by a space therebetween.

Sample

Sample input

4
t -7 t 4 x 2 x 5

Sample Output

33
1 2

Data range and tips

Sample input corresponds to the polygon shown in FIG. The first character of the second line is the symbol number 1 side.

This problem Similarly stones were combined, converted into a polygon cyclic chain, an n into 2 * n + 1 characters.

The total number can not be changed, so a little goes a little backwards;

One cycle before, we decided to remove that edge, plus n, goes rearward;

f [i] [j], represents the optimal solution from i to j;

Each point must be composed of two points, so, in the i, j enumeration division point intervals, attention not equal division points J;

Because this problem, the numbers have both positive and negative, will have to consider the case of negative makes a positive

For example, -1,000 -1,000 * 100 * 100 = 1000000 and 1000000 = 10000> 10000, but has less than 100 -1000;

So every time the merger is bound to consider the minimum and maximum values;

If the next merge, a symbol '+', directly adding the maximum and minimum values;

If '*', it will be big * big * little big, small * large and small * Comparison of four little bit, looking for the maximum and minimum;

 

 

Code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int n,shu[150],f[150][150][3],sum[150][150],ff[150]={0},ma,mb,a,b,c,d;
char fu[150];
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>fu[i]>>shu[i];//先输入符号,在输入数字
    }
    for(int i=n+1;i<=2*n;i++)
    {
        shu[i]=shu[i-n];
        fu[i]=fu[i-n];
    }
    for(int i=1;i<=n;i++)
    {
        for(int o=1;o<=2*n;o++)
        {
            for(int p=1;p<=2n-*; P ++ ) 
            { 
                F [O] [P] [ . 1 ] = - 9999999 ; // find a large value, the minimum initialization of 
                F [O] [P] [ 2 ] = 9999999 ; // find a small value, initializing the maximum 
            } 
        } 
        for ( int Q = . 1 ; Q <= 2 * n-; Q ++) // initializing section DP 
        { 
            F [Q] [Q] [ . 1 ] = Shu [Q]; 
            F [Q] [Q] [ 2 ] = Shu [Q];
             IF (FU [Q + . 1 ] == ' T ')
            {
                f[q][q+1][1]=shu[q]+shu[q+1];
                f[q][q+1][2]=shu[q]+shu[q+1];
            }
            if(fu[q+1]=='x')
            {
                f[q][q+1][1]=shu[q]*shu[q+1];
                f[q][q+1][2]=shu[q]*shu[q+1];
            }
        }
        for( Int L = 2 ; L <= n-; L ++ ) 
        { 
            for ( int J = I; J <= I + NL; J ++ ) 
            { 
                int K = J + L- . 1 ;
                 for ( int X = J; X <K ; X ++) // must be less than k, because x + 1 rear; 
                {
                     IF (FU [X + . 1 ] == ' T ' && F [J] [K] [ . 1 ] <F [J] [X] [ . 1 ] + F [X + . 1 ] [K] [ . 1 ]) F [J] [K] [ . 1 ] = F [J] [X] [ . 1 ] + F [X + . 1 ] [K] [1];
                    if(fu[x+1]=='t'&&f[j][k][2]>f[j][x][2]+f[x+1][k][2])f[j][k][2]=f[j][x][2]+f[x+1][k][2];
                    if(fu[x+1]=='x')
                    {
                        a=f[j][x][1]*f[x+1][k][1];
                        b=f[j][x][2]*f[x+1][k][1];
                        c=f[j][x][1]*f[x+1][k][2];
                        d=f[j][x][2]*f[x+1][k][2];
                        f[j][k][1]=max(max(max(a,b),max(c,d)),f[j][k][1]);
                        f[j][k][2]=min(min(min(a,b),min(c,d)),f[j][k][2]);
                    }
                }
            }
        }
        ff[i]=f[i][i+n-1][1];
    }
    ma=-1000000;
    for(int i=1;i<=n;i++)//寻找最大值
    {
        if(ma<ff[i])
        {
            ma=ff[i];
            mb=i;
        }
    }
    cout<<ma<<endl;
    for(int i=1;i<=n;i++)
    {
        if(ff[i]==ma)
        {
            coutI << << "  " ; // output may be off the first few 
        } 
    } 
} 
/ * 
. 4 120 
X X 2 X. 4. 3. 1 X 2. 5. 4. 3 


. 6 10000 
X X -10 -10 -10 T 10 T 10 X 10. 6 T 


. 7 300000 
X X -10 -10 -10 X X 10 -10 T 10 T 10 T. 5 
above three sets of test data * /    

 

 

Guess you like

Origin www.cnblogs.com/fengwu2005/p/11289562.html