IOI1998·Poj·洛谷·Polygon

初见安~这里是传送门:洛谷 P4342 & Poj P1179

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. 

IOI1998·Poj·洛谷·Polygon_第1张图片


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. 

IOI1998·Poj·洛谷·Polygon_第2张图片


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

Sol

n个点,n条边,明显是一个环形。每次可以选择合并一条边的两个点,做相应的计算。很明显是区间dp啊!!!每次都取一个中点合并两边,取最大值不就完了吗!!!

然而并不。因为乘法有负负得正的特殊性质,也就是说可能你这两个区间的最小值相乘反而可以得到最大值。那我们再维护一个最小值不就行了?

然后就没有了。真的,没有了。

#include
#include
#define maxn 200//一定不要开小了,要开两倍n!!
using namespace std;
int n, num[maxn];
int minn[maxn][maxn], maxx[maxn][maxn];
char op[maxn];
int main() {
	ios::sync_with_stdio(false);
	memset(minn, 0x3f, sizeof minn);
	memset(maxx, 0x80, sizeof maxx);
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> op[i] >> num[i], op[i + n] = op[i], num[i + 1] = num[i];
		minn[i][i] = minn[i + n][i + n] = maxx[i][i] = maxx[i + n][i + n] = num[i];
        //num那个数组不重要。处理环形,都要复制一倍接在后面
	}
	
	for(int len = 1; len <= n; len++) {
		for(int l = 1; l + len <= (n << 1); l++) {
			int r = l + len;
			
			for(int k = l; k < r; k++) {
				if(op[k + 1] == 't') {//注意对应的符号的下标
					maxx[l][r] = max(maxx[l][r], maxx[l][k] + maxx[k + 1][r]);
					minn[l][r] = min(minn[l][r], minn[l][k] + minn[k + 1][r]);。//加法直接操作便是
				}
				
				else {
					maxx[l][r] = max(maxx[l][r], max(maxx[l][k] * maxx[k + 1][r], minn[l][k] * minn[k + 1][r]));//乘法冗长一点,当然分步写看起来会舒服些【我懒就写长点
					minn[l][r] = min(min(minn[l][k] * minn[k + 1][r], minn[l][r]), min(minn[l][k] * maxx[k + 1][r], maxx[l][k] * minn[k + 1][r]));
				}
			}
		}
	}
	
	int ans = -(0x3f3f3f3f);
	for(int i = 1; i <= n; i++) ans = max(ans, maxx[i][i + n - 1]);
	
	cout << ans << endl;//先得出ans
	int tmp;
	for(int i = 1; i <= n; i++) if(maxx[i][i + n - 1] == ans) {cout << i; tmp = i;break;}
	for(int i = tmp + 1; i <= n; i++) if(maxx[i][i + n - 1] == ans) cout << ' '<< i;//某OJ不让多输出空格……至少洛谷没有限制的
	return 0;
}

迎评:)
——End——

你可能感兴趣的:(动态规划)