IOI1998/POJ1179 Polygon

                                                                                       Polygon

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 6260   Accepted: 2679

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。

       令f[ l ][ r ][ 0 ]为把第 l 个 到第 r 个顶点合成一个顶点后,顶点上的数值最大是多少,令f[ l ][ r ][ 1 ]为把第 l 个 到第 r 个顶点合成一个顶点后,顶点上的数值最小是多少。

       最大值的来源可能是两个最大值相加、相乘,或两个最小值相乘(负负得正)。

       最小值的来源可能是两个最想之相加、相乘,或一个最大值乘一个最小值。于是就有以下状态转移方程:

if(way[k+1] == 1)   //加号
{
  f[j][i+j][0] = max(f[j][k][0] + f[k+1][i+j][0] , f[j][i+j][0]);
  f[j][i+j][1] = min(f[j][k][1] + f[k+1][i+j][1] , f[j][i+j][1]);
}
else        //乘号
{
  f[j][i+j][0] = max(max(f[j][k][0] * f[k+1][i+j][0] , f[j][k][1] * f[k+1][i+j][1]) , f[j][i+j][0]);
  f[j][i+j][1] = min(min(f[j][k][0] * f[k+1][i+j][1] , f[j][k][1] * f[k+1][i+j][0]) , f[j][i+j][1]);
  f[j][i+j][1] = min(f[j][k][1] * f[k+1][i+j][1] , f[j][i+j][1]);
}
//l <= k < r

       初始化: ∀ l ∈ [1 ,  N],f[l , l] =f[ l ][ l ][ 1 ] =num[ i ],其余为正无穷或负无穷。

       答案 : f[ 1 ][ N ][ 0 ]。

       这时的时间复杂度是O(N ^ 4),我们还能继续优化掉选择删除哪条边的循环。在游戏最初,我们任意选择一条边删除,然后把剩下的链复制一份接在末尾(以被删除的边逆时针方向的第一个顶点为开头)。这样以后对于把长度为 ∀ i ∈ [1 ,  N] ,把长度为 N 的区间合并成一个顶点。因为区间长度是DP的阶段,所以我们只用对前 N 个阶段进行DP,总时间复杂度降到O(N ^3),最后的答案 max{f[ i ][i + N - 1]} (1 <=i <= N)。

代码:
 

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <vector>
#include <bitset>
#include <queue>
#include <ctime>
#include <deque>
using namespace std;

const int INF=1e9;
const int Max=205;
int n,m,pos;
int num[Max],way[Max];
int ans=-1e9,f[Max][Max][2];

inline int get_int()
{
   int x=0,f=1;
   char c;
   for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
   if(c=='-') {f=-1;c=getchar();}
   for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
   return x*f;
}

inline void pre()
{
   for(int i=1;i<=2*n;i++)
     for(int j=1;j<=2*n;j++)
     {
       f[i][j][0]=-INF;
       f[i][j][1]=INF;
     }
}

inline void solve()
{
   for(int i=1;i<=n-1;i++)
     for(int j=1;j+i <= 2 * n;j++)
     {
       for(int k=j;k<j+i;k++)
       {
       	 if(way[k+1] == 1)
       	 {
       	   f[j][i+j][0] = max(f[j][k][0] + f[k+1][i+j][0] , f[j][i+j][0]);
       	   f[j][i+j][1] = min(f[j][k][1] + f[k+1][i+j][1] , f[j][i+j][1]);
       	 }
       	 else
       	 {
       	   f[j][i+j][0] = max(max(f[j][k][0] * f[k+1][i+j][0] , f[j][k][1] * f[k+1][i+j][1]) , f[j][i+j][0]);
       	   f[j][i+j][1] = min(min(f[j][k][0] * f[k+1][i+j][1] , f[j][k][1] * f[k+1][i+j][0]) , f[j][i+j][1]);
       	   f[j][i+j][1] = min(f[j][k][1] * f[k+1][i+j][1] , f[j][i+j][1]);
       	 }
       }
     }
   for(int i=1;i<=n;i++) ans=max(ans , f[i][i+n-1][0]);
}

int main()
{
   n=get_int();
   pre();
   for(int i=1;i<=n;i++)
   {
   	 char c=getchar();
   	 c == 't' ? way[i+n] = way[i] = 1 : way[i+n] = way[i] = 2;
   	 f[i][i][0] = f[i][i][1] = f[i+n][i+n][1] = f[i+n][i+n][0] = num[i] = num[n+i] = get_int();
   }

   solve();
   printf("%d\n",ans);
   for(int i=1;i<=n;i++) if(f[i][i+n-1][0] == ans) printf("%d ",i);
   return 0;
}

       这种“任意选择一个位置断开,复制形成2倍长度的链”的方法,是解决环形DP中环形结构的乘用手段之一。

                                                                                                                                      ——《算法竞赛 进阶指南》

猜你喜欢

转载自blog.csdn.net/m0_38083668/article/details/81088318