Polygon

Description

Polygon is a game for one player that starts on a polygon with N vertices, like the one in Figure 1, where N=4. Each vertex is labelled with an integer and each edge is labelled with either the symbol + (addition) or the symbol * (product). The edges are numbered from 1 to N. 

On the first move, one of the edges is removed. Subsequent moves involve the following steps: 
�pick an edge E and the two vertices V1 and V2 that are linked by E; and 
�replace them by a new vertex, labelled with the result of performing the operation indicated in E on the labels of V1 and V2. 
The game ends when there are no more edges, and its score is the label of the single vertex remaining. 

Consider the polygon of Figure 1. The player started by removing edge 3. After that, the player picked edge 1, then edge 4, and, finally, edge 2. The score is 0. 

Write a program that, given a polygon, computes the highest possible score and lists all the edges that, if removed on the first move, can lead to a game with that score. 

Input

Your program is to read from standard input. The input describes a polygon with N vertices. It contains two lines. On the first line is the number N. The second line contains the labels of edges 1, ..., N, interleaved with the vertices' labels (first that of the vertex between edges 1 and 2, then that of the vertex between edges 2 and 3, and so on, until that of the vertex between edges N and 1), all separated by one space. An edge label is either the letter t (representing +) or the letter x (representing *). 

3 <= N <= 50 
For any sequence of moves, vertex labels are in the range [-32768,32767]. 

Output

Your program is to write to standard output. On the first line your program must write the highest score one can get for the input polygon. On the second line it must write the list of all edges that, if removed on the first move, can lead to a game with that score. Edges must be written in increasing order, separated by one space.

Sample Input

4
t -7 t 4 x 2 x 5

Sample Output

33
1 2

Source


题目大意是给你一个环,环的边上是运算符,节点是数字,求怎样分开数链进行相邻点的合并使得最后得到的值最大……(这题意描述得真不清楚,可是我的语文能力已经到此为止了……)

读了题后想了很久,果然弱爆了……
还记得当年noip的那到石子合并吗?如果记得真不该不会啊!

区间dp,i到j运算的最大值 ,可以由i到k的和k + 1 到j的转移来,注意由于有乘法,fmax 完全可能是两个子区间的最小值乘起来的,所以还要用相同的方法维护一个fmin

wa了一次,原因是枚举区间起点的时候只是从1到了n(化环为链以后应该一直到2 * n) 
ps:弱逼又一次在了输方案上纠结很久……什么毛病嘛!

#include <iostream>
#include <cstdio>
#include <cstring>
const long N = 200;
long n;
long a[N];
long fmax[N][N], fmin[N][N];
using namespace std;
long min(long a, long b)
{
    return a > b ? b : a;
}
long max(long a, long b)
{
    return a < b ? b : a;
}
void init()
{
    memset(fmax, 128, sizeof(fmax));
    memset(fmin, 127, sizeof(fmin));
    for (long i = 1; i <= n; i++)
    {
        char c;
        scanf("%c ", &c);
        if (c == 't') a[i] = 0;
            else a[i] = 1;
        a[i + n] = a[i];
        scanf("%d ", &fmin[i][i]);
        fmax[i][i] = fmin[i][i];
        fmax[i + n][i + n] = fmin[i + n][i + n] = fmin[i][i];
    }

}
void dp()
{
    for (long l = 2; l <= n ; l++)
    {
        for (long i = 1; i + l - 1 <= 2 * n; i++)
        {
            long j = i + l - 1;
            for (long k = i; k < j; k++)
            {
                 if (a[k + 1] == 0)
                 {
                     fmax[i][j] = max(fmax[i][j], fmax[i][k] + fmax[k + 1][j]);
                     fmin[i][j] = min(fmin[i][j], fmin[i][k] + fmin[k + 1][j]);
                 }
                 else{
                     fmax[i][j] = max(fmax[i][j], fmax[i][k] * fmax[k + 1][j]);
                     fmax[i][j] = max(fmax[i][j], fmin[i][k] * fmin[k + 1][j]);
                     fmin[i][j] = min(fmin[i][j], fmin[i][k] * fmin[k + 1][j]);
                     fmin[i][j] = min(fmin[i][j], fmax[i][k] * fmin[k + 1][j]);
                     fmin[i][j] = min(fmin[i][j], fmin[i][k] * fmax[k + 1][j]);
                     fmin[i][j] = min(fmin[i][j], fmax[i][k] * fmax[k + 1][j]);
                 }
            }
             // cout <<i <<' '<<j<<' '<< fmax[i][j] <<endl;
        }
    }
}

int main()
{
    freopen("poj1179.in", "r", stdin);
    while (scanf("%d\n", &n) != EOF)
    {
        init();
        dp();
        long re = -2147483647l;
        for (long i = 1; i <= n; i++)
        {
            //cout << i << ' '<< i + n - 1<<' '<< fmax[i][i + n - 1]<<endl;
            if (fmax[i][i + n - 1] > re)
                re = fmax[i][i + n - 1];
        }
        printf("%d\n", re);
        for (long i = 1; i <= n; i++)
        {
            if (fmax[i][i + n - 1] == re)
                printf("%d ", i);
        }
        printf("\n");
    }
    return 0;
}