POJ 1179 Polygon

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
思路:典型的石子合并问题。
但因为有乘法,所以要同时记录最大值和最小值。
石子合并问题查看:石子合并

两种思路:
1 假设 dp[i][j]表示顶点i到顶点j得到的最大最小值,
代码如下:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<limits.h>
#include<vector>
#include<set>
using namespace std;

const int N = 55;
char op[2*N];
int arr[2*N];
int dpmax[2*N][2*N];
int dpmin[2*N][2*N];
int main()
{
    freopen("input.txt","r",stdin);
    int n;
    while(cin>>n)
    {
        for(int i = 0; i < n; i++)
        {
            cin>>op[i];
            op[i+n] = op[i];
            cin>>arr[i];
            arr[i+n] = arr[i];
        }

        for(int i = 0; i < 2 * n; i++)
        {
            dpmin[i][i] = arr[i];
            dpmax[i][i] = arr[i];
        }

        for(int len = 1; len < n; len++)
        {
            for(int i = 0; i + len < 2 * n; i++)
            {
                dpmin[i][i+len] = INT_MAX;
                dpmax[i][i+len] = INT_MIN;
                for(int k = i; k < i + len; k++)
                {
                    if(op[k+1] == 't')
                    {
                        dpmin[i][i+len] = min(dpmin[i][i+len], dpmin[i][k] + dpmin[k+1][i+len]);
                        dpmax[i][i+len] = max(dpmax[i][i+len], dpmax[i][k] + dpmax[k+1][i+len]);
                    }
                    else if(op[k+1] == 'x')
                    {
                        int num1 = dpmin[i][k] * dpmin[k+1][i+len];
                        int num2 = dpmin[i][k] * dpmax[k+1][i+len];
                        int num3 = dpmax[i][k] * dpmax[k+1][i+len];
                        int num4 = dpmax[i][k] * dpmin[k+1][i+len];

                        dpmin[i][i+len] = min(dpmin[i][i+len],
                                              min(min(num1,num2), min(num3,num4)) );
                        dpmax[i][i+len] = max(dpmax[i][i+len],
                                              max(max(num1,num2), max(num3,num4)) );
                    }
                }
            }
        }

        int maxv = dpmax[0][n-1];
        for(int i = 0; i < n; i++)//以每一项为开头
        {
            maxv = max(maxv, dpmax[i][i+n-1]);//长度都为n
        }
        cout<<maxv<<endl;
        vector<int> vec;
        for(int i = 0; i < n; i++)
        {
            if(dpmax[i][i+n-1] == maxv)
            {
                vec.push_back(i+1);
            }
        }
        for(int i = 0; i < vec.size(); i++)
        {
            cout<<vec[i];
            if( i< vec.size() - 1) cout<<" ";
            else cout<<endl;
        }
    }

    return 0;

}

2 dp[i][j]表示从顶点i开始的j个顶点得到的最优值
代码如下:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<limits.h>
#include<vector>
#include<set>
using namespace std;

const int N = 55;
char op[N];
int arr[N];
int dpmax[N][N];//从i开始j个顶点的最大值
int dpmin[N][N];//从i开始j个顶点的最小值
int main()
{
    freopen("input.txt","r",stdin);
    int n;
    while(cin>>n)
    {
        for(int i = 0; i < n; i++)
        {
            cin>>op[i];
            cin>>arr[i];
        }
        //初始化
        for(int i = 0; i < n; i++)
        {
            dpmin[i][0] = arr[i];
            dpmax[i][0] = arr[i];
        }


        for(int len = 1; len < n; len++)
        {
            for(int i = 0; i < n; i++)
            {
                int maxv = INT_MIN;
                int minv = INT_MAX;
                for(int k = 0; k < len; k++)
                {
                    if(op[(i+k+1) % n] == 't')
                    {
                        if(dpmin[i][k] + dpmin[(i+k+1) % n][len - k - 1] < minv)
                        {
                            minv = dpmin[i][k] + dpmin[(i+k+1) % n][len - k - 1];
                        }
                        if(dpmax[i][k] + dpmax[(i+k+1) % n][len - k - 1] > maxv)
                        {
                            maxv = dpmax[i][k] + dpmax[(i+k+1) % n][len - k - 1];
                        }
                    }
                    else if(op[(i+k+1) % n] == 'x')
                    {
                        int n1 = dpmin[i][k] * dpmin[(i+k+1) % n][len - k - 1];
                        int n2 = dpmin[i][k] * dpmax[(i+k+1) % n][len - k - 1];
                        int n3 = dpmax[i][k] * dpmin[(i+k+1) % n][len - k - 1];
                        int n4 = dpmax[i][k] * dpmax[(i+k+1) % n][len - k - 1];

                        minv = min(minv, min(min(n1,n2), min(n3,n4)));
                        maxv = max(maxv, max(max(n1,n2), max(n3,n4)));
                    }
                    dpmin[i][len] = minv;
                    dpmax[i][len] = maxv;
                }
            }
        }
        int res = dpmax[0][n-1];
        for(int i = 0; i < n; i++)//以每一项为开头
        {
            res = max(res, dpmax[i][n-1]);//长度都为n
        }
        cout<<res<<endl;
        vector<int> vec;
        for(int i = 0; i < n; i++)
        {
            if(dpmax[i][n-1] == res)
            {
                vec.push_back(i+1);
            }
        }
        for(int i = 0; i < vec.size(); i++)
        {
            cout<<vec[i];
            if( i< vec.size() - 1) cout<<" ";
            else cout<<endl;
        }

    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhengjihao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值