POJ 1179 Polygon (DP)

来源:互联网 发布:mac连接wifi经常断线 编辑:程序博客网 时间:2024/06/05 19:59
Polygon
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 4264 Accepted: 1765

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

4t -7 t 4 x 2 x 5

Sample Output

331 2

Source

IOI 1998
思路:对于每次合并的两条子链,如果是*进行合并,那么产生的最大值可能有两条子链的最小值相乘得到,所以要记录每次合并的最大值和最小值。dp[i][j][0]和dp[i][j][1]分别记录从第i个点开始,合并掉j个点的最小值和最大值,那么dp[i][j][f]可由dp[i][k][f]推得,0<k<j,k即为j的子结构枚举。注意是条链,所以需要对n取模。
#include <iostream>#include <cstdio>#include <cstring>#include <climits>using namespace std;int dp[51][51][2];char str[51];int main(){    int n,b,i,j,k;    char op;    while(cin>>n)    {        for(i=1; i<=n; i++)            for(j=0; j<n; j++)                for(k=0; k<2; k++)                    dp[i][j][k]=(k==0?INT_MAX:INT_MIN);        for(i=1; i<=n; i++)        {            cin>>op>>b;            str[i]=op;            dp[i][0][0]=dp[i][0][1]=b;        }        for(j=1; j<n; j++)            for(i=1; i<=n; i++)                for(k=0; k<j; k++)                {                    if(str[(i+k)%n+1]=='t')                    {                        dp[i][j][0]=min(dp[i][j][0],dp[i][k][0]+dp[(i+k)%n+1][j-k-1][0]);                        dp[i][j][1]=max(dp[i][j][1],dp[i][k][1]+dp[(i+k)%n+1][j-k-1][1]);                    }                    else                    {                        dp[i][j][0]=min(dp[i][j][0],dp[i][k][0]*dp[(i+k)%n+1][j-k-1][0]);                        dp[i][j][0]=min(dp[i][j][0],dp[i][k][0]*dp[(i+k)%n+1][j-k-1][1]);                        dp[i][j][0]=min(dp[i][j][0],dp[i][k][1]*dp[(i+k)%n+1][j-k-1][1]);                        dp[i][j][0]=min(dp[i][j][0],dp[i][k][1]*dp[(i+k)%n+1][j-k-1][0]);                        dp[i][j][1]=max(dp[i][j][1],dp[i][k][1]*dp[(i+k)%n+1][j-k-1][1]);                        dp[i][j][1]=max(dp[i][j][1],dp[i][k][0]*dp[(i+k)%n+1][j-k-1][0]);                    }                }        int ans=INT_MIN;        for(i=1; i<=n; i++)            ans=max(dp[i][n-1][1],ans);        cout<<ans<<endl;        for(i=1; i<=n; i++)            if(dp[i][n-1][1]==ans)                cout<<i<<" ";        cout<<endl;    }    return 0;}



原创粉丝点击