POJ1179总结

Polygon

题目来源

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:
�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

我的代码

思路很简单:遍历每一条需要删去的边之后,构造成线性的式子以进行区间dp。但是这个题目有个天坑是我一开始没想到的——如果是乘法运算,最大值不一定是两个子段的最大值相乘!也有可能是两个都是负数的最小值相乘!

最大值的计算结果可能来自:两个子段的最大值相加、两个子段的最大值相乘、两个子段的最小值相乘;最小值的计算结果可能来自:两个子段的最小值相加、一个子段的最大值乘上一个子段的最小值。

所以dp方程是

dp_max[i][i+l-1]=max(dp_max[i][i+l-1],cal(dp_max[i][j],dp_max[j+1][i+l-1],j,nowtor));
dp_max[i][i+l-1]=max(dp_max[i][i+l-1],cal(dp_min[i][j],dp_min[j+1][i+l-1],j,nowtor));
dp_min[i][i+l-1]=min(dp_min[i][i+l-1],cal(dp_max[i][j],dp_min[j+1][i+l-1],j,nowtor));
dp_min[i][i+l-1]=min(dp_min[i][i+l-1],cal(dp_min[i][j],dp_max[j+1][i+l-1],j,nowtor));
dp_min[i][i+l-1]=min(dp_min[i][i+l-1],cal(dp_min[i][j],dp_min[j+1][i+l-1],j,nowtor));

完整代码如下:

#include<stdio.h>
#include<string.h>
const int inf=0x3f3f3f3f;
int n;
long long ans=0;
int operands[51];
char operators[51];
int ans_list[51];
int ans_num=0;
long long  max(long long x, long long y)
{
	return x>y?x:y;
}
long long  min(long long x, long long y)
{
	return x<y?x:y;
}
long long cal(long long a,long long b, int o,char op[])
{
	if (op[o]=='t')
	{
		return a+b;
	}
	else
	{
		return a*b;
	}
}
void make (int rands[],char tors[],int t)
{
	for (int i=0;i<n;i++)
	{
		rands[i]=operands[(i+t)%n];
	}
	for (int i=0;i<n-1;i++)
	{
		tors[i]=operators[(i+t+1)%n];
	}
}
int main()
{
	scanf("%d",&n);
	getchar();
	for (int i=0;i<n;i++)
	{
		scanf("%c",&operators[i]);
		scanf("%d",&operands[i]);
		getchar();
	}
	for(int m=0;m<n;m++)
	{
		int nowrand[51];
		char nowtor[51];
		long long dp_max[51][51];
		long long dp_min[51][51];
		memset(dp_max,-inf,sizeof(dp_max));
		memset(dp_min,inf,sizeof(dp_min));
		make(nowrand,nowtor,m);//构造当前的运算数序列和运算符序列 
		for (int i=0;i<n;i++)
		{
			dp_max[i][i]=nowrand[i];
			dp_min[i][i]=nowrand[i];
		}
		for (int l=2;l<=n;l++)
		{
			for (int i=0;i<n-l+1;i++)
			{
				for (int j=i;j<i+l-1;j++)
				{
					dp_max[i][i+l-1]=max(dp_max[i][i+l-1],cal(dp_max[i][j],dp_max[j+1][i+l-1],j,nowtor));
					dp_max[i][i+l-1]=max(dp_max[i][i+l-1],cal(dp_min[i][j],dp_min[j+1][i+l-1],j,nowtor));
					dp_min[i][i+l-1]=min(dp_min[i][i+l-1],cal(dp_max[i][j],dp_min[j+1][i+l-1],j,nowtor));
					dp_min[i][i+l-1]=min(dp_min[i][i+l-1],cal(dp_min[i][j],dp_max[j+1][i+l-1],j,nowtor));
					dp_min[i][i+l-1]=min(dp_min[i][i+l-1],cal(dp_min[i][j],dp_min[j+1][i+l-1],j,nowtor));
				}
			}
		}
		if (ans<dp_max[0][n-1])
		{
			ans=dp_max[0][n-1];
			ans_num=0;
			ans_list[ans_num++]=m+1;
		}
		else if (ans==dp_max[0][n-1])
		{
			ans_list[ans_num++]=m+1;
		}
	}
	printf("%lld\n",ans);
	for (int i=0;i<ans_num;i++)
	{
		printf("%d",ans_list[i]);
		if (i<ans_num-1)	printf(" ");
	}
	return 0;
}

反思

一开始我只是想做乘法会不会溢出,反而没有考虑负负得正也能产生最大值。以后遇到通过乘法来求运算最大值需要注意一下负数结果。

发表评论