POJ 1179 Polygon 区间DP

版权声明:https://blog.csdn.net/huashuimu2003 https://blog.csdn.net/huashuimu2003/article/details/88750581

title

POJ 1179
CH POJ1179
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

analysis

关键就是要想到最大值可能是负负得正得来的,所以我们还需要维护区间的最小值,别的就不难了,很单纯的区间dp。——摘自humveea6

code

第一种写法:主要思想为:这是一个循环的区间dp,只要在右边界取余即可。

#include<bits/stdc++.h>
using namespace std;
const int maxn=110;
const int inf=0x3f3f3f3f;
template<typename T>inline void read(T &x)
{
	x=0;
	T f=1, ch=getchar();
	while (!isdigit(ch) && ch^'-') ch=getchar();
	if (ch=='-') f=-1, ch=getchar();
	while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), ch=getchar();
	x*=f;
}
inline int cal(char x,int a,int b)
{
	if (x=='t') return a+b;
	else return a*b;
}
char c[maxn];
int val[maxn];
int f[maxn][maxn],g[maxn][maxn];
int main()
{
	int n;read(n);
	for (int i=0; i<n; ++i)
		cin>>c[i],read(val[i]);
	for (int i=0; i<n; ++i)
		f[i][i]=g[i][i]=val[i];
	for (int len=1; len<n; ++len)
		for (int i=0; i<n; ++i)
		{
			int MAX=-inf,MIN=inf;
			int j=(i+len)%n;
			for (int k=0; k<len; ++k)
			{
				int p1=(i+k)%n;
				int p2=(i+k+1)%n;
				MAX=max(MAX,max( cal(c[p2],f[i][p1],f[p2][j]), cal(c[p2],g[i][p1],g[p2][j]) ) );
				MIN=min(MIN,min( cal(c[p2],f[i][p1],f[p2][j]), cal(c[p2],g[i][p1],g[p2][j]) ) );
			}
			f[i][j]=MAX;
			g[i][j]=MIN;
		}
	int ans=-inf;
	for (int i=0; i<n; ++i)
	{
		int j=(i+n-1)%n;
		ans=max(ans,f[i][j]);
	}
	printf("%d\n",ans);
	for (int i=0; i<n; ++i)
	{
		int j=(i+n-1)%n;
		if (f[i][j]==ans)
			printf("%d ",i+1);
	}
	return 0;
}

正常环形处理。

#include<bits/stdc++.h>
using namespace std;
const int maxn=110;
const int inf=0x3f3f3f3f;
template<typename T>inline void read(T &x)
{
	x=0;
	T f=1, ch=getchar();
	while (!isdigit(ch) && ch^'-') ch=getchar();
	if (ch=='-') f=-1, ch=getchar();
	while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), ch=getchar();
	x*=f;
}
char c[maxn];
int val[maxn];
int f[maxn][maxn],g[maxn][maxn];
int main()
{
    int n;read(n);
    for (int i=1; i<=(n<<1); ++i)
        for (int j=1; j<=(n<<1); ++j)
			f[i][j]=-inf,g[i][j]=inf;
    for (int i=1; i<=n; ++i)
	{
        cin>>c[i],read(val[i]);
        c[i+n]=c[i];
		val[i+n]=val[i];
        f[i][i]=f[i+n][i+n]=g[i][i]=g[i+n][i+n]=val[i];
    }
    for (int len=1; len<n; ++len)
		for (int i=1; i+len<=(n<<1)-1; ++i)
			for (int j=i+1; j<=i+len; ++j)
    		{
        		if(c[j]=='t')
				{
            		f[i][i+len]=max(f[i][i+len],f[i][j-1]+f[j][i+len]);
            		g[i][i+len]=min(g[i][i+len],g[i][j-1]+g[j][i+len]);
        		}
        		else
				{
            		f[i][i+len]=max(f[i][i+len],f[i][j-1]*f[j][i+len]);
            		f[i][i+len]=max(f[i][i+len],g[i][j-1]*g[j][i+len]);
            		g[i][i+len]=min(g[i][i+len],g[i][j-1]*g[j][i+len]);
            		g[i][i+len]=min(g[i][i+len],f[i][j-1]*g[j][i+len]);
            		g[i][i+len]=min(g[i][i+len],g[i][j-1]*f[j][i+len]);
        		}
    		}
    int ans=0;
    for (int i=1; i<=n; ++i)
		ans=max(ans,f[i][i+n-1]);
    printf("%d\n",ans);
    for (int i=1; i<=n; ++i)
        if (f[i][i+n-1]==ans)
			printf("%d ",i);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/huashuimu2003/article/details/88750581