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

題意:求出怎樣斷開使得結果最大,並且求出在哪斷開最大(有幾個斷開方式就輸出幾個)。

 

#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
const int inf = 0x3f3f3f3f;
char op[100];
int v[100],n;
int ans[100];
int m[100][100][2];
//m[a][b][c]表示從a開始長度爲b的鏈
int minf,maxf;
//假設最後一次運算是在s處進行的
//則將p(i,j)分爲p(i,s)和p(i+s,j-s)
void minMax(int i,int s,int j)
{
    int e[5]= {0};
    int a=m[i][s][0];
    int b=m[i][s][1];
    int r=(i+s-1)%n+1;
    int c=m[r][j-s][0];
    int d=m[r][j-s][1];
    if(op[r]=='t')//加法直接求出最優解
    {
        minf=a+c;
        maxf=b+d;
    }
    else//乘法的最優解可能是最大最小值交錯相乘
    {
        e[1]=a*c;
        e[2]=a*d;
        e[3]=b*c;
        e[4]=b*d;
        minf=e[1];
        maxf=e[1];
        for(int k=2; k<5; k++)
        {
            if(minf>e[k])
                minf=e[k];
            if(maxf<e[k])
                maxf=e[k];
        }
    }
    return ;
}
int polyMax()
{
    for(int j=2; j<=n; j++)
    {
        for(int i=1; i<=n; i++)
        {
            for(int k=1; k<j; k++)
            {
                minMax(i,k,j);
                if(m[i][j][0]>minf)
                    m[i][j][0]=minf;
                if(m[i][j][1]<maxf)
                    m[i][j][1]=maxf;
            }
        }
    }
    int t=m[1][n][1];
    return t;
}
int main()
{
    cin>>n;
    for(int i=1; i<=n; i++)
        cin>>op[i]>>v[i];
    for(int k=1; k<=n; k++)
    {
        for (int i = 1; i <= n;i++){
            for (int j = 1; j <= n;j++){
                m[i][j][1] = -inf;
                m[i][j][0] = inf;
            }
        }
        for (int i = 1; i <= n; i++)
        {
            m[i][1][0] = v[i];
            m[i][1][1] = v[i];
        }
        ans[k]=polyMax();
        //多次調換在求其最優解
        int p=v[1];
        char c=op[1];
        for(int i=1; i<=n-1; i++)
        {
            op[i]=op[i+1];
            v[i]=v[i+1];
        }
        v[n]=p;
        op[n]=c;
    }
    //找出一個最大
    int maxx = -inf;
    for (int i = 1; i <= n;i++){
        maxx = max(maxx, ans[i]);
    }
    int e = 0, z[100] = {0};
    for(int i=1; i<=n; i++)
    {
        if(ans[i]==maxx)//只要跟最大值相等就放到數組中
            z[e++]=i;
    }
    sort(z, z + e);
    cout << maxx << endl;
    for (int i = 0; i < e; i++)
    {
        if(i==e-1)
            cout<<z[i]<<endl;
        else
            cout<<z[i]<<" ";
    }
 
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章