1. 程式人生 > >POJ 1179 Polygon(區間DP)

POJ 1179 Polygon(區間DP)

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.
Figure 1

On the first move, one of the edges is removed. Subsequent moves involve the following steps:
1. pick an edge E and the two vertices V1 and V2 that are linked by E; and
2. 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.
Figure 2

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

IOI 1998

解題思路

這道題的題意大致是對於n個點的環先刪除一個點後,根據連線的運算子逐漸合併各個點,最後求最大的結果可能是多少。一道經典的區間DP題,有幾個點需要注意一下:

  1. 需要列舉最開始刪除的是哪條邊
  2. 由於一開始是個環,刪邊後可能從中間斷開,導致區間不連續,無法使用區間DP。解決方法是將原始陣列複製一倍,這樣就能保證能夠使用區間DP。
  3. 由於元素可能存在負值,負負相乘得正後可能得到的結果更大。所以在維護結果的時候,既要維護最大值,也要維護最小值。

AC程式碼

#include<bits/stdc++.h>
//#define TEST
using namespace std;
#define INF 400000
int n,ans;
char op[105];
int num[105],ma[105][105],mi[105][105];
vector<int> lt;

int mmax(int s,int k,int t,char z)
{
    if(z=='t')
        return ma[s][k-1]+ma[k][t];
    else
        return max(ma[s][k-1]*ma[k][t],mi[s][k-1]*mi[k][t]);
}

int mmin(int s,int k,int t,char z)
{
    if(z=='t')
        return mi[s][k-1]+mi[k][t];
    else
        return min(ma[s][k-1]*ma[k][t],mi[s][k-1]*mi[k][t]);
}

int main()
{
    scanf("%d\n",&n);
    for(int i=0;i<n;i++){
        scanf("%c %d%*c",&op[i],&num[i]);
        op[i+n]=op[i];
        num[i+n]=num[i];    // 增加一倍長度
    }
    for(int i=0;i<2*n;i++){
        for(int j=0;j<2*n;j++){
            ma[i][j]=-INF;
            mi[i][j]=INF;
        }
    }
    for(int i=0;i<(n<<1);i++){
        ma[i][i]=mi[i][i]=num[i];
    }
        #ifdef TEST
    for(int i=0;i<2*n;i++){
        for(int j=0;j<2*n;j++){
            cout<<ma[i][j]<<' ';
        }
        cout<<endl;
    }
    #endif // TEST
    for(int d=1;d<=n;d++){
        for(int s=0;s<2*n-d+1;s++){
            int t=s+d-1;
            for(int k=s+1;k<=t;k++){
                ma[s][t]=max(ma[s][t],mmax(s,k,t,op[k]));
                mi[s][t]=min(mi[s][t],mmin(s,k,t,op[k]));
            }
        }
    }
    #ifdef TEST
    for(int i=0;i<2*n;i++){
        for(int j=0;j<2*n;j++){
            cout<<ma[i][j]<<' ';
        }
        cout<<endl;
    }
    #endif // TEST
    ans=-INF;
    lt.clear();
    for(int i=0;i<n;i++){
        if(ma[i][i+n-1]>ans){
            ans=ma[i][i+n-1];
            lt.clear();
            lt.push_back(i+1);
        }
        else if(ma[i][i+n-1]==ans){
            lt.push_back(i+1);
        }
    }
    printf("%d\n",ans);
    printf("%d",lt[0]);
    for(int i=1;i<lt.size();i++){
        printf(" %d",lt[i]);
    }
    printf("\n");
    return 0;
}