POJ1179——Polygon

Polygon

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4975   Accepted: 2090

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

Source

IOI 1998

好题!!

区间dp,枚举删除的边, 然后一开始无脑想到了这个方程 dp[i][j] = max(dp[i][k] + or * dp[k + 1]j]);

但是,存在一些情况是由左右两边负负得正得到的最大值,那么此时所需要的值并没有在之前保存下来,所以无法得到正确值

我们设f1[i][j], f2[i][j]表示从i处理到j所能得到的最大值以及最小值

对于f1

如果是‘+’,那么肯定是最大的+最大的得到最大的

如果是‘*‘,那么可能是大*大,大*小,小*大,小*小

对于f2

如果是’+‘,那么肯定是最小的+最小的得到最小的

如果是’*‘,那么可能是 大*大, 小*小, 大*小,小*大

#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 55;
const int inf = 0x3f3f3f3f;

int num[N];
int edge[N];
char op[N];
int f1[N * 2][N * 2], f2[N * 2][N * 2];

int main()
{
	int n;
	while (~scanf("%d", &n))
	{
		memset (f1, -inf, sizeof(f1));
		memset (f2, inf, sizeof(f2));
		for (int i = 1; i <= n; ++i)
		{
			getchar();
			scanf("%c%*c%d", &op[i], &num[i]);
		}
		for (int i = 1; i < n; ++i)
		{
			op[i + n] = op[i];
			num[i + n] = num[i];
		}
		for (int i = 1; i <= 2 * n - 1; ++i)
		{
			f1[i][i] = f2[i][i] = num[i];
		}
		for (int i = 2 * n - 1; i >= 1; --i)
		{
			for (int j = i + 1; j <= 2 * n - 1; ++j)
			{
				for (int k = i; k < j; ++k)
				{
					if (op[k + 1] == 't')// +
					{
						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], f1[i][k] * f2[k + 1][j]);
						f1[i][j] = max(f1[i][j], f2[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], f1[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], f2[i][k] * f2[k + 1][j]);
						f2[i][j] = min(f2[i][j], f1[i][k] * f1[k + 1][j]);
					}
				}
			}
		}
		int ans = -inf, cnt = 0;
		for (int i = 1; i <= n; ++i)
		{
			ans = max(ans, f1[i][i + n - 1]);
		}
		for (int i = 1; i <= n; ++i)
		{
			if (ans == f1[i][i + n - 1])
			{
				edge[cnt++] = i;
			}
		}
		printf("%d\n", ans);
		printf("%d", edge[0]);
		for (int i = 1; i < cnt; ++i)
		{
			printf(" %d", edge[i]);
		}
		printf("\n");
	}
	return 0;
}
时间: 2024-08-11 01:20:20

POJ1179——Polygon的相关文章

poj1179 Polygon【区间DP】

Polygon Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:6633   Accepted: 2834 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 inte

[POJ1179]Polygon

http://poj.org/problem?id=1179 多边形是一个玩家在一个有n个顶点的多边形上的游戏,如图所示,其中n=4.每个顶点用整数标记,每个边用符号+(加)或符号*(乘积)标记. 第一步,删除其中一条边.随后每一步: 选择一条边连接的两个顶点V1和V2,用边上的运算符计算V1和V2得到的结果来替换这两个顶点. 游戏结束时,只有一个顶点,没有多余的边. 如图所示,玩家先移除编号为3的边.之后,玩家选择计算编号为1的边,然后计算编号为4的边,最后,计算编号为2的边.结果是0. (翻

使用GPC分解多边形样例(Generic Polygon Clipper)

使用GPC分解多边形 (Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu 转载请标明来源) GPC: Generic Polygon Clipper GPC支持分解多边形.多边形求差集.交集,异或.并集 GPC_DIFF,                         /* Difference                        */ GPC_INT,                          /* Intersection    

[LeetCode] Convex Polygon 凸多边形

Given a list of points that form a polygon when joined sequentially, find if this polygon is convex (Convex polygon definition). Note: There are at least 3 and at most 10,000 points. Coordinates are in the range -10,000 to 10,000. You may assume the

ACdream 1429 Rectangular Polygon

Rectangular Polygon Time Limit: 1000MS   Memory Limit: 256000KB   64bit IO Format: %lld & %llu Description A rectangular polygon is a polygon whose edges are all parallel to the coordinate axes. The polygon must have a single, non-intersecting bounda

libgdx 裁剪多边形(clip polygon、masking polygon)

直接放例子代码,代码中以任意四边形为例,如果需要做任意多边形,注意libgdx不能直接用ShapeRender填充多边形,需要先切割成三角形. public static void drawClip(Batch batch, Polygon polygon, TextureRegion region, float x, float y) { float[] vertices = polygon.getVertices(); if (shapes == null) { shapes = new S

Scrambled Polygon(差集排序)

Scrambled Polygon Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7799   Accepted: 3707 Description A closed polygon is a figure bounded by a finite number of line segments. The intersections of the bounding line segments are called the

polygon

polygon Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 38(28 users) Total Accepted: 29(27 users) Rating: Special Judge: No Description We have a special polygon that all points have the same distance to original point.As you know we can get 

hunnu11562:The Triangle Division of the Convex Polygon(第n个卡特兰数取模)

Problem description   A convex polygon with n edges can be divided into several triangles by some non-intersect diagonals. We denote d(n) is the number of the different ways to divide the convex polygon. For example,when n is 6,there are 14 different