POJ 1179 Polygon

Polygon
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5178 Accepted: 2192

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

Source



今天竟然开始dp了
话说前两天的图论竟然都没跟上。。晚上回去还打游戏。。啊
简直就是sb

看到题感觉挺简单的
给一个环,n个点n条边,每个点一个值,每个边一个运算符,加法或者乘法
首先断开一条边 变成一条链 然后不断删去每条边,把边的两点值用边的运算符运算一下 成一个新的点
求最大值和能取到最大值得断开边方案
啪啪啪打上去代码
首先2m存圈变成链
枚举删除的边
f[i][j]表示从i到j的最大值
然后竟然写出了
f[i][j] = max( f[i+1][j] + calc(i,i+1), f[i][j-1] + calc(j-1.j));
这样的sb方程 竟然还交上去了一次
然后感觉不太对 改成枚举 [i,j]中的每一个k
f[i][j] = max( f[i][j], f[i][k]+f[k+1][j]+calc(k,k+1));
这样竟然还不对

最后看了题解才明白是因为 不但有加法还有乘法
加法这么写肯定没问题
但是乘法
3>-5
3>-5
3*3<(-5)*(-5)
所以还要搞一个数组vf[i][j]保存i到j的最小值!!!

所以完整代码如下
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int inf = 0x3f3f3f3f;
int f[111][111];
int vf[111][111];
int m,n,x,y;
int g[111];         // + 0  * 1
int s[111];
char chin;
int ans;
int calc(int u,int v,int p)
{
    if(p==0)
        return u+v;
    else
        return u*v;
}

int work(int l,int r)
{
    //cout<<"l="<<l<<" r="<<r<<endl;
    for(int i=1;i<=m-1;i++)             //changdu
        for(int j=l;j<=r-i;++j)        //kaishiweizhi
            for(int k=j;k<=j+i-1;++k)   //zhongjianweizhi
            {
                int temp1 = calc(f[j][k],f[k+1][j+i],g[k+1]);
                int temp2 = calc(vf[j][k],vf[k+1][j+i],g[k+1]);
                int mx=(max(temp1,temp2));
                int mi=(min(temp1,temp2));
                
                f[j][j+i]=max(f[j][j+i],mx);
                vf[j][j+i]=min(vf[j][j+i],mi);
                //cout<<"f["<<j<<"]["<<j+i<<"]="<<f[j][j+i]<<endl;;
                //cout<<"vf["<<j<<"]["<<j+i<<"]="<<vf[j][j+i]<<endl;;
            }
    return f[l][r];
}
int print[1111],num=0;
int main()
{
    while(scanf("%d",&m)==1)
    {
    num=0;
    ans=-inf;
    for(int i=1;i<=m;i++)
    {
        scanf(" %c %d",&chin,&n);
        if(chin=='t')
            g[i]=g[i+m]=0;
        else
            g[i]=g[i+m]=1;
        s[i]=s[i+m]=n;
    }
    for(int i=1;i<=m;++i)
    {
        memset(f,-inf,sizeof(f));
        memset(vf,inf,sizeof(vf));
        for(int j=1;j<=m+m;++j)
            vf[j][j]=f[j][j]=s[j];
            
        int ret = work(i,i+m-1);
        if(ret==ans)
        {
            num++;
            print[num]=i;
        }
        else
        if(ret > ans)
        {
            ans=ret;
            num=1;
            print[num]=i;
        }
    }
    cout<<ans<<endl;
    for(int i=1;i<num;++i)
        cout<<print[i]<<" ";
    cout<<print[num]<<endl;
    
    }
    return 0;
}

以后再犯sb错误扇脸!
想明白再写!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值