题目描述

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.

Polygon(区间DP)_#include

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.

Polygon(区间DP)_i++_02

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.
输入描述:
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 \leq N \leq 503≤N≤50
For any sequence of moves, vertex labels are in the range [-32768,32767].
输出描述:
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.
示例1
输入
复制
4
t -7 t 4 x 2 x 5
输出
复制
33
1 2
题目大意: 有两种运算符Polygon(区间DP)_i++_03分别用Polygon(区间DP)_i++_04表示,就是用对应的运算符连接两个数,可以看出是个闭合的,你可以选的删除一条边(也就是删除一个运算符)然后你就可以通过运算将两个点和成一个点,使得最后合成一个点,求这个点值最大值为多少?
思路: 
因为运算中有Polygon(区间DP)_#include_05那么我们就可以通过大*大或者小*大或者大*小或者小*小得到这个Polygon(区间DP)_#include_06运算得到的最大值
同样的最小的也就是就是大*大或者小*大或者大*小或者小*小
Polygon(区间DP)_运算符_07: 这个就

  1. 最大=最大+最大
  2. 最小= 最小+最小

还有一点就是删边操作,我们删掉那条边就从边的下个点开会计算,那么我们就需要构造二倍的长度来使得都能使得计算的长度是Polygon(区间DP)_#include_08
这种“任意选的一个位置断开,复制形成2倍长度的链”的方法是解决DP中环形结构的常用手段的之一。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include <algorithm>
#include <map>
using namespace std;
#define
#define
const int maxn=200;
const int mod=1e8+7;
const int inf = 0x3f3f3f3f;
ll n,m,l;
ll a[maxn],d[210][210],p[210][210],dis[maxn];
char str[maxn];
int main() {
while (~scanf("%lld", &n)) {
memset (d, -inf, sizeof(d));
memset (p, inf, sizeof(p));
for(int i=1; i<=n; i++) {
getchar();
scanf("%c %lld",&str[i],&a[i]);
}
for(int i=1; i<n; i++) {
a[i+n]=a[i];
str[i+n]=str[i];
}
for(int i=1; i<2*n; i++) {
d[i][i]=a[i];
p[i][i]=a[i];
}
for(int i=2*n-1; i>=1; i--) {
for(int j=i+1; j<2*n; j++) {
for(int k=i; k<j; k++) {
if(str[k+1]=='t') {
d[i][j]=max(d[i][j],d[i][k]+d[k+1][j]);
p[i][j]=min(p[i][j],p[i][k]+p[k+1][j]);
} else {
d[i][j]=max(d[i][j],d[i][k]*d[k+1][j]);
d[i][j]=max(d[i][j],d[i][k]*p[k+1][j]);
d[i][j]=max(d[i][j],p[i][k]*d[k+1][j]);
d[i][j]=max(d[i][j],p[i][k]*p[k+1][j]);

p[i][j]=min(p[i][j],d[i][k]*d[k+1][j]);
p[i][j]=min(p[i][j],d[i][k]*p[k+1][j]);
p[i][j]=min(p[i][j],p[i][k]*d[k+1][j]);
p[i][j]=min(p[i][j],p[i][k]*p[k+1][j]);
}
}
}
}
ll sum=-maxn,cnt=0;
for(int i=1; i<=n; i++) {
sum=max(sum,d[i][i+n-1]);
}
for(int i=1; i<=n; i++) {
if(d[i][i+n-1]==sum) {
dis[++cnt]=i;
}
}
cout<<sum<<" "<<endl;
for(int i=1; i<cnt; i++) cout<<dis[i]<<" ";
cout<<dis[cnt]<<endl;
}
return 0;
}