Polygon
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
算法分析:dp1[i][j]表示区间[i,j]之间的点合并后的最大值,初始化dp1[i][i] = a[i],状态转移方程为dp1[i][j] = max(dp1[i][j], dp1[i][q] op dp1[q+1][j] op dp2[i][q] op dp2[q+1][j]),由于最大值有可能是从两个最小值(负数)相乘得到,所以还需要维护区间的最小值dp2[i][j],转移方程和dp1类似,注意dp2有可能是最小值(负数)和最大值相乘得到,所以更新dp2时也要考虑到。计算过程为每次删除一条边然后计算一次最大值并记录,最后递增遍历一遍并输出最大值对应的下标即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
/************************************************** filename :b.cpp author :maksyuki created time :2017/3/2 20:16:22 last modified :2017/3/3 11:35:17 file location :C:\Users\abcd\Desktop\TheEternalPoet ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CPPFF ifstream cin ("in") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 100 + 6; int a[maxn], dp1[maxn][maxn], dp2[maxn][maxn], ans[maxn]; char op[maxn][maxn]; int Op(int a, int b, char val, int c, int d, int flag) { if(val == 't') return a + b; else { if(flag == 1) return max(a * b, c * d); else return min(a * b, c * d); } } int main() { //CFF; //CPPFF; int n; while(scanf("%d", &n) != EOF) { char tmp; for(int i = 1; i <= n; i++) { scanf(" %c %d", &tmp, &a[i]); a[i+n] = a[i]; if(i == 1) { op[i][n] = op[n][i] = tmp; op[n][n+1] = op[n+1][n] = tmp; } else { op[i-1][i] = op[i][i-1] = tmp; op[i-1+n][i+n] = op[i+n][i-1+n] = tmp; } } for(int i = 0; i < maxn; i++) ans[i] = -INF; for(int k = 0; k <= n - 1; k++) { for(int i = 0; i < maxn; i++) for(int j = 0; j < maxn; j++) { dp1[i][j] = -INF; dp2[i][j] = INF; } for(int i = 1 + k; i <= n + k; i++) dp1[i][i] = dp2[i][i] = a[i]; for(int len = 1; len <= n; len++) for(int i = 1 + k; i <= n + k; i++) { int j = i + len; if(j > n + k) break; for(int q = i; q <= j - 1; q++) { dp1[i][j] = max(dp1[i][j], Op(dp1[i][q], dp1[q+1][j], op[q][q+1], dp2[i][q], dp2[q+1][j], 1)); dp2[i][j] = min(dp2[i][j], Op(dp2[i][q], dp2[q+1][j], op[q][q+1], dp1[i][q], dp2[q+1][j], 2)); dp2[i][j] = min(dp2[i][j], Op(dp2[i][q], dp2[q+1][j], op[q][q+1], dp2[i][q], dp1[q+1][j], 2)); } } ans[k+1] = dp1[1+k][n+k]; } int maxval = -INF; for(int i = 1; i <= n; i++) maxval = max(maxval, ans[i]); printf("%d\n", maxval); bool is_first = true; for(int i = 1; i <= n; i++) { if(maxval == ans[i]) { if(is_first) { is_first = false; printf("%d", i); } else printf(" %d", i); } } puts(""); } return 0; } |
- « 上一篇:poj1018
- poj1414:下一篇 »