pij 1179 Polygon

Polygon

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 1
Problem 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

给你一些边和顶点,边为x表示*,为t便是+,加入删除一条边,那么他们的顶点就会合一。
求先删除哪一条边,能使得到的值最大

区间dp,枚举一圈,然后求出顶点之间的值的和,由于有*,所以就会出现负负得正的情况,因此在枚举中需要求出dp_MIN(a,i),dp_MIN(i+1,b),dp_MAX(a,i),dp_MAX(i+1,b)
,从中选出乘积最大的值输出,并记录最大值时的顶点是哪个,输出边。

状态方程:

加法  max(i,j) = max(i,k)+max(k,j);
   乘法  max(i,j) = MAX(max(i,k)*max(k,j),max(i,k)*min(k,j),max(k,j)*min(i,k),min(i,k)*min(k,j));(i=<k<=j)
计算最小值:
   加法  min(i,j) = min(i,k)+min(k,j);
   乘法  min(i,j) = MIN(max(i,k)*max(k,j),min(i,k)*min(k,j),max(k,j)*min(i,k),min(i,k)*min(k,j));(i=<k<=j)
#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define N 110
int vist_max[N*N],vist_min[N*N],dp_max[N*N],dp_min[N*N];
int num[N];
int op[N];
int judge_max(int x,int y,int z,int h)
{
    return max(x,max(y,max(z,h)));
}
int judge_min(int x,int y,int z,int h)
{
    return min(x,min(y,min(z,h)));
}
int dp_MAX(int a,int b);
int dp_MIN(int a,int b);

int dp_MAX(int a,int b)
{
    int n=a*100+b;
    if(vist_max[n]) return dp_max[n];
    vist_max[n]=1;
    if(b<=a+1)
    {
        if(a==b) return dp_max[n]=num[a-1];
        if(!op[a]) return dp_max[n]=num[a]+num[a-1];
        else return dp_max[n]=num[a]*num[a-1];
    }
    dp_max[n]=-INF;
    for(int i=a;i<b;i++)
    {
        int l=dp_MIN(a,i);
        int r=dp_MIN(i+1,b);
        int ll=dp_MAX(a,i);
        int rr=dp_MAX(i+1,b);
        if(!op[i]) dp_max[n]=max(dp_max[n],ll+rr);
        else dp_max[n]=max(dp_max[n],judge_max(l*r,l*rr,r*ll,ll*rr));
    }
    return dp_max[n];
}


int dp_MIN(int a,int b)
{
    int n=a*100+b;
    if(vist_min[n]) return dp_min[n];
    vist_min[n]=1;
    if(b<=a+1)
    {
        if(a==b) return dp_min[n]=num[a-1];
        if(!op[a]) return dp_min[n]=num[a]+num[a-1];
        else return dp_min[n]=num[a]*num[a-1];
    }
    dp_min[n]=INF;
    for(int i=a;i<b;i++)
    {
        int l=dp_MIN(a,i);
        int r=dp_MIN(i+1,b);
        int ll=dp_MAX(a,i);
        int rr=dp_MAX(i+1,b);
        if(!op[i]) dp_min[n]=min(dp_min[n],l+r);
        else dp_min[n]=min(dp_min[n],judge_min(l*r,l*rr,r*ll,ll*rr));
    }
    return dp_min[n];
}

int main()
{
    int n;
    char c;
    while(~scanf("%d%*c",&n))
    {
        memset(op,0,sizeof(op));
        memset(dp_max,0,sizeof(dp_max));
        memset(dp_min,0,sizeof(dp_min));
        memset(vist_max,0,sizeof(vist_max));
        memset(vist_min,0,sizeof(vist_min));
        for(int i=0; i<n; i++)
        {
            scanf("%c %d%*c",&c,&num[i]);
            if(c=='x')
                op[i]=op[i+n]=1;
            num[i+n]=num[i];
        }
        int sum_max=-INF;
        //cout<<111<<endl;
        for(int i=0; i<n; i++)
        {
            sum_max=max(sum_max,dp_MAX(i+1,i+n));
        }
        //cout<<111<<endl;
        cout<<sum_max<<endl;
        int flag=1;
        for(int i=0; i<n; i++)
        {
            if(dp_MAX(i+1,i+n)==sum_max)
            {
                if(flag)
                {
                    printf("%d",i+1);
                    flag=0;
                }
                else printf(" %d",i+1);
            }
        }
        cout<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值