思路:
dp。
实现:
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <vector> 5 using namespace std; 6 7 const int INF = 0x3f3f3f3f; 8 9 int n, a[55]; 10 char c[55]; 11 struct node 12 { 13 int minn, maxn; 14 }; 15 node dp[55][55]; 16 struct ans 17 { 18 int res, index; 19 }; 20 int cal(int a, int b, char c) 21 { 22 return c == 't' ? a + b : a * b; 23 } 24 int myMax(int a, int b, int c, int d) 25 { 26 return max(a, max(b, max(c, d))); 27 } 28 int myMin(int a, int b, int c, int d) 29 { 30 return min(a, min(b, min(c, d))); 31 } 32 int main() 33 { 34 cin >> n; 35 for (int i = 0; i < n; i++) 36 { 37 cin >> c[i] >> a[i]; 38 } 39 vector<ans> Ans; 40 for (int t = 0; t < n; t++) 41 { 42 for (int i = 0; i < n; i++) 43 { 44 for (int j = i; j < n; j++) 45 { 46 dp[i][j].minn = INF; 47 dp[i][j].maxn = -INF; 48 } 49 } 50 vector<int> x; 51 vector<char> y; 52 for (int j = t; j < n; j++) 53 { 54 x.push_back(a[j]); 55 if (j != t) 56 y.push_back(c[j]); 57 } 58 for (int j = 0; j < t; j++) 59 { 60 x.push_back(a[j]); 61 y.push_back(c[j]); 62 } 63 for (int i = n - 1; i >= 0; i--) 64 { 65 for (int j = i; j < n; j++) 66 { 67 if (j == i) 68 dp[i][j].minn = dp[i][j].maxn = x[i]; 69 if (j == i + 1) 70 { 71 dp[i][j].maxn = dp[i][j].minn = cal(x[i], x[j], y[i]); 72 } 73 else 74 { 75 for (int k = i; k < j; k++) 76 { 77 node l = dp[i][k], r = dp[k + 1][j]; 78 if (y[k] == 't') 79 { 80 dp[i][j].minn = min(dp[i][j].minn, cal(l.minn, r.minn, 't')); 81 dp[i][j].maxn = max(dp[i][j].maxn, cal(l.maxn, r.maxn, 't')); 82 } 83 else 84 { 85 dp[i][j].minn = min(dp[i][j].minn, 86 myMin(cal(l.minn, r.minn, 'x'), 87 cal(l.minn, r.maxn, 'x'), 88 cal(l.maxn, r.minn, 'x'), 89 cal(l.maxn, r.maxn, 'x'))); 90 dp[i][j].maxn = max(dp[i][j].maxn, 91 myMax(cal(l.minn, r.minn, 'x'), 92 cal(l.minn, r.maxn, 'x'), 93 cal(l.maxn, r.minn, 'x'), 94 cal(l.maxn, r.maxn, 'x'))); 95 } 96 } 97 } 98 } 99 } 100 ans as; 101 as.res = dp[0][n - 1].maxn; 102 as.index = t + 1; 103 Ans.push_back(as); 104 } 105 int maxn = -INF, maxIndex = -1; 106 for (int i = 0; i < n; i++) 107 { 108 if (Ans[i].res > maxn) 109 { 110 maxn = Ans[i].res; 111 maxIndex = Ans[i].index; 112 } 113 } 114 vector<int> p; 115 for (int i = 0; i < n; i++) 116 { 117 if (Ans[i].res == maxn) 118 { 119 p.push_back(Ans[i].index); 120 } 121 } 122 cout << maxn << endl; 123 for (int i = 0; i < p.size(); i++) 124 { 125 cout << p[i] << " "; 126 } 127 cout << endl; 128 return 0; 129 }