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:
1)pick an edge E and the two vertices V1 and V2 that are linked by E; and
2)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
I think
题目大意 多角形是一个单人玩的游戏,开始时有一个N个顶点的多边形。如图,这里N=4。每个顶点有一个整数标记,每条边上有一个“+”号或“*”号。边从1编号到N。
第一步,一条边被拿走;随后各步包括如下:
选择一条边E和连接着E的两个顶点V1和 V2;
得到一个新的顶点,标记为V1与V2通过边E上的运算符运算的结果。
最后,游戏中没有边,游戏的得分为仅剩余的一个顶点的值。
求如何使得最终结果最大
这道题不难想到先枚举第一条删除的边,再用记忆化搜索,取每一个运算符号两边两部分的最大值。但是贪心需要斟酌,考虑到题中*号两边两部分会有负数,而两个最小负数相乘能够得到最大整数。因此
对于乘法
求最大值:
正数×正数,如果两个都是最大值,则结果最大
正数×负数,正数最小,负数最大,则结果最大
负数×负数,如果两个都是最小值,则结果最大
求最小值:
正数×正数,如果两个都是最小值,则结果最小
正数×负数,正数最大,负数最小,则结果最小
负数×负数,如果两个都是最大值,则结果最小
用fmax[i][j] 表示从第i个数到j个数运算后所得的最大结果,fmin[i][j] 表示从第i个数到j个数运算后所得的最小结果,即可递推。
记忆化搜索实现。
Code
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int sm = 2*50+5;
const int inf = 0x3f3f3f3f;
char ch;
char g[sm][sm];
int ans;
int n,u[sm],e[sm];
int fmax[sm][sm],fmin[sm][sm];
int dfsmax(int,int);
int dfsmin(int,int);
int main() {
scanf("%d\n",&n);
for(int i=1;i<=n;++i){
ch=getchar();
g[i-1+n][i+n]=g[i-1][i]=ch;
scanf("%d",&u[i]);u[i+n]=u[i];
ch=getchar();
}
ans=-inf;
for(int i=1;i<=2*n;++i)
for(int j=1;j<=2*n;++j) {
fmax[i][j]=(i==j)?u[i]:-inf;
fmin[i][j]=(i==j)?u[i]:inf;
}
for(int i=1;i<=n;++i) {
int t=dfsmax(i,i+n-1);
if(t>ans) {
ans=t;e[0]=1;e[e[0]]=i;
}
else if(t==ans) e[++e[0]]=i;
}
printf("%d\n",ans);
for(int i=1;i<=e[0];++i)
printf("%d ",e[i]);
printf("\n");
return 0;
}
int dfsmax(int x,int y) {
if(fmax[x][y]!=-inf)return fmax[x][y];
int tmp=-inf;
for(int i=x;i<y;++i) {
if(g[i][i+1]=='t')//'+'
tmp=max(tmp,dfsmax(x,i)+dfsmax(i+1,y));
else {//'*'
int a,b,c,d;
a=dfsmin(x,i);b=dfsmin(i+1,y);
c=dfsmax(x,i);d=dfsmax(i+1,y);
tmp=max(tmp,a*b);tmp=max(tmp,a*d);
tmp=max(tmp,c*b);tmp=max(tmp,c*d);
}
}
return fmax[x][y]=tmp;
}
int dfsmin(int x,int y) {
if(fmin[x][y]!=inf)return fmin[x][y];
int tmp=inf;
for(int i=x;i<y;++i) {
if(g[i][i+1]=='t')//'+'
tmp=min(tmp,dfsmin(x,i)+dfsmin(i+1,y));
else {
int a,b,c,d;
a=dfsmin(x,i);b=dfsmin(i+1,y);
c=dfsmax(x,i);d=dfsmax(i+1,y);
tmp=min(tmp,a*b);tmp=min(tmp,a*d);
tmp=min(tmp,c*b);tmp=min(tmp,c*d);
}
}
return fmin[x][y]=tmp;
}