POJ - 1179 Polygon 区间dp

本文介绍了一种在Polygon游戏中计算最高可能得分及实现该得分的初始边移除策略的方法。通过区间动态规划处理包含加法和乘法操作的顶点,以确定最优解。
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

首先每一步都是对相邻的元素做某种运算,我们用区间dp,

这题有加有乘,当然不能只求区间的最大值,因为区间的最大值还可能由二个最小值相乘得到,

当oprater为乘时最大值可能为max{minm*minm,maxm*maxm};

最小值为min(minm*minm,minm*maxm,maxm*minm)

相加时 maxm=max(maxm+maxm)

minm=min(minm+minm)

还有就是找第一次断的那条边,我们不必要用一个for来枚举,只要将前面的复制到末尾就可以了

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
using namespace std;
#define ll long long
typedef pair<int,int>P;
const int len=1e2+5;
int n,dp1[len][len],dp2[len][len],a[len];
char c[len];
int main()
{
    while(cin>>n)
    {
        for(int i=1;i<=n;++i)
        {
            cin>>c[i]>>a[i];
            c[i+n]=c[i];
            a[i+n]=a[i];
        }
        memset(dp1,-0x3f,sizeof(dp1));
        memset(dp2,0x3f,sizeof(dp2));
        for(int i=1;i<=2*n;++i)dp1[i][i]=dp2[i][i]=a[i];
        for(int le=2;le<=n;++le)//n=枚举区间长度
        {
            for(int l=1;l<=2*n-le+1;++l)//左端点
            {
                int r=l+le-1;//右端点
                for(int k=l;k<r;++k)//中短点
                {
                    if(c[k+1]=='x')
                    {
                        dp1[l][r]=max(dp1[l][r],max(dp1[l][k]*dp1[k+1][r],dp2[l][k]*dp2[k+1][r]));
                        dp2[l][r]=min(dp2[l][r],min(dp2[l][k]*dp2[k+1][r],min(dp2[l][k]*dp1[k+1][r],dp1[l][k]*dp2[k+1][r])));
                    }
                    else
                    if(c[k+1]=='t')
                    {
                        dp1[l][r]=max(dp1[l][r],dp1[l][k]+dp1[k+1][r]);
                        dp2[l][r]=min(dp2[l][r],dp2[l][k]+dp2[k+1][r]);
                    }
                }
            }
        }
        ll maxm=-1e18,u;
        for(int i=1;i<=n;++i)
        {
            if(dp1[i][i+n-1]>maxm)
                maxm=dp1[i][i+n-1];
        }
        cout<<maxm<<endl;
        for(int i=1;i<=n;++i)
            if(dp1[i][i+n-1]==maxm)cout<<i<<' ';
        puts("");
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值