洛谷P4342 [IOI1998]Polygon【区间DP】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/niiick/article/details/81840548

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.


题目分析

因为题目给出的多边形是个环
所以首先想到断环为链,即将原序列复制一遍
再进行区间DP

由于负数和乘法的存在
如果dp状态只保存区间最大值的话,不能保证最优子结构
所以我们dp数组还要多开一维
d p [ 0 ] [ l l ] [ r r ] 表示合并区间 [ l l , r r ] 能得到的最小值
d p [ 1 ] [ l l ] [ r r ] 则表示最大值

按区间DP套路枚举断点k
那么对于区间最大值有

dp[1][ll][rr]=max(dp[1][ll][rr],dp[1][ll][k]+dp[1][k+1][rr]);
dp[1][ll][rr]=max(dp[1][ll][rr],dp[1][ll][k]*dp[1][k+1][rr]);
dp[1][ll][rr]=max(dp[1][ll][rr],dp[0][ll][k]*dp[0][k+1][rr]);

也就是区间 [ l l , r r ] 最大值可以由
两个子区间的最大值相加、相乘,或两个子区间最小值相乘转移得来

对于区间最小值有

dp[0][ll][rr]=min(dp[0][ll][rr],dp[0][ll][k]+dp[0][k+1][rr]);
dp[0][ll][rr]=min(dp[0][ll][rr],dp[0][ll][k]*dp[0][k+1][rr]);
dp[0][ll][rr]=min(dp[0][ll][rr],dp[1][ll][k]*dp[0][k+1][rr]);
dp[0][ll][rr]=min(dp[0][ll][rr],dp[0][ll][k]*dp[1][k+1][rr]);

也就是区间 [ l l , r r ] 最小值可以由
两个子区间的一个最大值与一个最小值相乘,或两个子区间最小值相加、相乘转移得来


#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
#include<map>
using namespace std;
typedef long long lt;

int read()
{
    int x=0,f=1;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return x*f;
}

const int maxn=200;
int n;
int op[maxn],a[maxn];
int dp[2][maxn][maxn];
int edge[maxn],cnt=0,ans=-1e9;

int main()
{
    n=read();
    for(int i=1;i<=n<<1;++i)
    {
        char ss[5];
        if(i%2){
            scanf("%s",&ss);
            op[(i>>1)+1]=op[(i>>1)+1+n]=(ss[0]=='t')?1:2;
        }
        else a[i>>1]=a[(i>>1)+n]=read();
    }
    //for(int i=1;i<=n<<1;++i)
    //dp[0][i][i]=dp[1][i][i]=a[i];

    for(int i=1;i<=n;++i)
    {
        memset(dp[0],67,sizeof(dp[0]));
        memset(dp[1],128,sizeof(dp[1]));
        for(int j=1;j<=n<<1;++j)
        dp[0][j][j]=dp[1][j][j]=a[j];

        for(int ll=i+n-1;ll>=i;--ll)
        for(int rr=ll+1;rr<=i+n-1;++rr)
        for(int k=ll;k<rr;++k)
        {
            if(op[k+1]==1)
            {
                dp[1][ll][rr]=max(dp[1][ll][rr],dp[1][ll][k]+dp[1][k+1][rr]);
                dp[0][ll][rr]=min(dp[0][ll][rr],dp[0][ll][k]+dp[0][k+1][rr]);
            }
            else if(op[k+1]==2)
            {
                dp[1][ll][rr]=max(dp[1][ll][rr],dp[1][ll][k]*dp[1][k+1][rr]);
                dp[1][ll][rr]=max(dp[1][ll][rr],dp[0][ll][k]*dp[0][k+1][rr]);

                dp[0][ll][rr]=min(dp[0][ll][rr],dp[0][ll][k]*dp[0][k+1][rr]);
                dp[0][ll][rr]=min(dp[0][ll][rr],dp[1][ll][k]*dp[0][k+1][rr]);
                dp[0][ll][rr]=min(dp[0][ll][rr],dp[0][ll][k]*dp[1][k+1][rr]);
            }
        }
        if(ans<dp[1][i][i+n-1])
        ans=dp[1][i][i+n-1],cnt=0,edge[++cnt]=i;
        else if(ans==dp[1][i][i+n-1])
        edge[++cnt]=i;
    }
    printf("%d\n",ans);
    for(int i=1;i<=cnt;++i)
    printf("%d ",edge[i]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/niiick/article/details/81840548