POJ1179 Polygon(区间DP)

传送门:http://poj.org/problem?id=1179

Polygon
Time Limit: 1000MS   Memory Limit: 10000K
     

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

 
  很典型的区间dp ,很水。。不过一次AC了还是很高兴的^_^...
  需要考虑的就是负数*负数的问题QAQ。。
  总结下区间dp。。基本上都可以写成
  dp[i][j] = max|min (d[i][j] , dp[i][k] + dp[k+1|k][j] + cost[k|k+1]) 的类型。。
  思想就是寻找分割点,但是要注意k的循环顺序
 
  Codes:
 1 #include<set>
 2 #include<queue>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<cstring>
 6 #include<iostream>
 7 #include<algorithm>
 8 using namespace std;
 9 const int N = 60;
10 const int inf = 2147483647;
11 #define For(i,n) for(int i=1;i<=n;i++)
12 #define Rep(i,l,r) for(int i=l;i<=r;i++)
13 char ch;
14 int A[N],num[N],OP[N],op[N],n;
15 int fmax[N][N],fmin[N][N],Ans[N];
16 void read(int &v){
17     int num = 0 , q = 1;ch = getchar();
18     while(ch>'9'||ch<'0'){
19         if(ch=='-') q = -1;
20         ch = getchar();
21     }
22     while(ch>='0'&&ch<='9'){
23         num = num * 10 + ch - '0';
24         ch = getchar();
25     }
26     v = num * q;
27 }
28 //4
29 //t -7 t 4 x 2 x 5
30 
31 void Move(int Loc){
32     Rep(i,Loc,Loc+n-1) {
33         num[i-Loc+1] = A[(i%n==0)?(n):(i%n)]; 
34         op[i-Loc+1] = OP[((i+1)%n==0)?(n):((i+1)%n)];
35     }
36 }
37 
38 void DP(){
39     For(del,n){
40         Move(del);
41         For(i,n) fmax[i][i] = fmin[i][i] = num[i];
42         For(i,n)
43             for(int j=i-1;j>=1;j--){
44                 fmax[j][i] = -inf;fmin[j][i] = inf;
45                 Rep(k,j,i-1)
46                     if(op[k]==0){
47                         fmax[j][i] = max(fmax[j][i],fmax[j][k]+fmax[k+1][i]);
48                         fmin[j][i] = min(fmin[j][i],fmin[j][k]+fmin[k+1][i]);
49                     }
50                     else{
51                         fmax[j][i] = max(fmax[j][i],fmax[j][k]*fmax[k+1][i]);
52                         fmax[j][i] = max(fmax[j][i],fmin[j][k]*fmax[k+1][i]);
53                         fmax[j][i] = max(fmax[j][i],fmax[j][k]*fmin[k+1][i]);
54                         fmax[j][i] = max(fmax[j][i],fmin[j][k]*fmin[k+1][i]);
55                         fmin[j][i] = min(fmin[j][i],fmin[j][k]*fmin[k+1][i]);
56                         fmin[j][i] = min(fmin[j][i],fmax[j][k]*fmin[k+1][i]);
57                         fmin[j][i] = min(fmin[j][i],fmin[j][k]*fmax[k+1][i]);
58                         fmin[j][i] = min(fmin[j][i],fmax[j][k]*fmax[k+1][i]);         
59                     }
60             }
61         Ans[del] = fmax[1][n];
62     }
63 }
64 int ans = 0;
65 int main(){
66     read(n);
67     For(i,n){
68         scanf("%c",&ch);
69         if(ch=='t') OP[i] = 0;
70         else        OP[i] = 1;
71         read(A[i]);
72     }
73     DP();
74     vector<int> list;
75     For(i,n) ans = max(ans,Ans[i]);
76     printf("%d
",ans);
77     For(i,n) if(Ans[i]==ans) list.push_back(i);
78     sort(list.begin(),list.end());
79     for(int i=0;i<list.size()-1;++i) printf("%d ",list[i]);printf("%d
",list[list.size()-1]);
80     return 0; 
81 }
原文地址:https://www.cnblogs.com/zjdx1998/p/3900271.html