"Polygon Game" is a single-player puzzle game.
At the beginning of the game, a polygon with a N vertex N edge (number 1-N) is given, as shown in Figure 1, where N = 4.
Each vertex is written with an integer and each edge is marked with an operator + (plus sign) or operator* (multiplier sign).
1179_1.jpg
The first step is to choose an edge and delete it.
Next, in the N-1 step, in each step, the player chooses a side, and uses the new vertex instead of the two vertices connected to the edge and the edge. The integer value on the new vertex is equal to the number of the two vertices deleted, and the result is calculated according to the symbols marked on the edges.
The following is the whole process of the game using the quadrilateral shown in Figure 1.
1179_2.jpg
In the end, there is only one vertex in the game. The number on the vertex is the score of the player, and the score of the player above is 0.
Please calculate how many points the player can get for a given N edge, and what strategies the first step can bring to the player to get the highest score.
Input format
The input contains two lines, the first action integer N.
The second line is used to describe the symbols on all edges of a polygon and the integers on all vertices, starting with the edges numbered 1, edges, points, edges. Describe in order.
The description vertex is the integer on the input vertex, and the description edge is the symbol on the input edge. The plus sign is expressed by "t" and the multiplication sign is expressed by "x".
Output format
The output consists of two lines, the first line with the highest score.
In the second line, with the highest score, all the edges that can be deleted in the first step are numbered from small to large, separated by spaces.
Data range
3≤N≤50,
Data ensure that no matter how the player operates, the values on the vertex are all within [-3276832767].
Input sample:
4
t -7 t 4 x 2 x 5
Output sample:
33
1 2
Train of thought:
- Double expansion will change the chain.
- f1(i,j) represents the maximum value from I to j, and f2(i,j) represents the minimum value from I to J. Minimum values are recorded because negative numbers may occur. Multiplying negative numbers by negative numbers may lead to larger negative numbers.
- Pay attention to subscripts. If K is the decomposition point, then k+1 is the symbol. And i+len cannot be reached when enumerating. Because it's going to go beyond that.
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 105; int f1[maxn][maxn],f2[maxn][maxn]; char s[maxn]; int a[maxn]; int main() { int n;scanf("%d",&n); for(int i = 1;i <= n;i++) { getchar();scanf("%c%d",&s[i],&a[i]); s[i + n] = s[i]; a[i + n] = a[i]; } memset(f1,-0x3f,sizeof(f1));memset(f2,0x3f,sizeof(f2)); for(int i = 1;i <= 2 * n;i++) { f1[i][i] = f2[i][i] = a[i]; } for(int l = 1;l <= n;l++) { for(int i = 1;i + l <= 2 * n;i++) { int j = i + l; for(int k = i;k < j;k++)//It can't be equal to j, because if it's equal to j, the symbol goes to j+1, which is beyond this range. { if(s[k + 1] == 't')//At first, the boundary here is wrong. It should be the symbol of k+1 position. { f1[i][j] = max(f1[i][j],f1[i][k] + f1[k + 1][j]); f2[i][j] = min(f2[i][j],f2[i][k] + f2[k + 1][j]); } else { f1[i][j] = max(f1[i][j],f1[i][k] * f1[k + 1][j]); f1[i][j] = max(f1[i][j],f2[i][k] * f2[k + 1][j]); f2[i][j] = min(f2[i][j],f2[i][k] * f2[k + 1][j]); f2[i][j] = min(f2[i][j],f2[i][k] * f1[k + 1][j]); f2[i][j] = min(f2[i][j],f1[i][k] * f1[k + 1][j]); f2[i][j] = min(f2[i][j],f1[i][k] * f2[k + 1][j]); } } } } int maxx = -0x3f3f3f3f; for(int i = 1;i <= n;i++) { // printf("%d\n",f1[i][i + n - 1]); maxx = max(f1[i][i + n - 1],maxx); } printf("%d\n",maxx); for(int i = 1;i <= n;i++) { if(f1[i][i + n - 1] == maxx) { printf("%d ",i); } } return 0; }