Polygon

xiaoxiao2022-05-13  28

                                       Polygon      POJ - 1179 

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

思路:典型的石子合并问题。  但因为有乘法和负数(eg(-100)*(-100)),所以要同时记录最大值和最小值。 

 

// C // 区间 dp , 本质是石子合并 #include <iostream> #include <algorithm> #include <queue> #include <math.h> #include <stdio.h> #include <string.h> using namespace std; #define MOD 1000000007 int dp_min[110][110],dp_max[110][110]; // dp[i][j]代表 第 i 和 j 个数运算到成一个数时的 最大/小值 int v[110]; char op[110]; int main() { int n; int i,j,k; while(scanf("%d",&n)!=EOF){ getchar(); for(i=1;i<=n;i++){ scanf("%c %d",&op[i],&v[i]); getchar(); // printf("%c %d",op[i],v[i]); op[i+n]=op[i]; v[i+n]=v[i]; } //printf("??"); for(i=1;i<=2*n;i++) { dp_max[i][i]=dp_min[i][i]=v[i]; if(i<2*n){ if(op[i+1]=='t') dp_max[i][i+1]=dp_min[i][i+1]=v[i]+v[i+1]; else dp_max[i][i+1]=dp_min[i][i+1]=v[i]*v[i+1]; } } for(k=3;k<=n;k++) // k 代表求解区间的长度 for(i=1;i+k-1<=2*n;i++){ dp_max[i][i+k-1]=-MOD; dp_min[i][i+k-1]=MOD; for(j=i;j<i+k-1;j++){ if(op[j+1]=='t'){ dp_max[i][i+k-1]=max(dp_max[i][i+k-1],dp_max[i][j]+dp_max[j+1][i+k-1]); dp_min[i][i+k-1]=min(dp_min[i][i+k-1],dp_min[i][j]+dp_min[j+1][i+k-1]); } else{ dp_max[i][i+k-1]=max(dp_max[i][i+k-1],dp_max[i][j]*dp_max[j+1][i+k-1]); dp_max[i][i+k-1]=max(dp_max[i][i+k-1],dp_min[i][j]*dp_min[j+1][i+k-1]); dp_max[i][i+k-1]=max(dp_max[i][i+k-1],dp_max[i][j]*dp_min[j+1][i+k-1]); dp_max[i][i+k-1]=max(dp_max[i][i+k-1],dp_min[i][j]*dp_max[j+1][i+k-1]); dp_min[i][i+k-1]=min(dp_min[i][i+k-1],dp_max[i][j]*dp_max[j+1][i+k-1]); dp_min[i][i+k-1]=min(dp_min[i][i+k-1],dp_max[i][j]*dp_min[j+1][i+k-1]); dp_min[i][i+k-1]=min(dp_min[i][i+k-1],dp_min[i][j]*dp_max[j+1][i+k-1]); dp_min[i][i+k-1]=min(dp_min[i][i+k-1],dp_min[i][j]*dp_min[j+1][i+k-1]); } } } int out[55],cnt=0; int ans=-MOD; for(i=1;i<=n;i++){ if(dp_max[i][i+n-1]>ans){ ans=dp_max[i][i+n-1]; cnt=0; out[cnt]=i; }else if(dp_max[i][i+n-1]==ans){ out[++cnt]=i; } } printf("%d\n",ans); for(i=0;i<cnt;i++) printf("%d ",out[i]); printf("%d\n",out[i]); } return 0; }

 

转载请注明原文地址: https://www.6miu.com/read-4884189.html

最新回复(0)