题目链接:https://www.nowcoder.com/questionTerminal/661c49118ca241909add3a11c96408c8
题目大意:
略
分析:
设 dpMax[i][j] 表示以 a[i] 结尾,一共选 j 个学生的情况下的乘积最大值。
设 dpMin[i][j] 表示以 a[i] 结尾,一共选 j 个学生的情况下的乘积最小值。
由于要从前 i 个中选出 j 个,所以 i >= j ,否则就是不合法的。
dpMax[i][j] 有3种情况:
设 1 <= h <= d。
- 就是它自己,即 dpMax[i][j]。
- dpMax[i - h][j - 1] * a[i],这是在 a[i] >= 0 的情况。
- dpMin[i - h][j - 1] * a[i],这是在 a[i] < 0 的情况。
三个取最大即可。
dpMin[i][j] 同理。
代码如下:
1 #pragma GCC optimize("Ofast") 2 #include <bits/stdc++.h> 3 using namespace std; 4 5 #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0); 6 #define Rep(i,n) for (int i = 0; i < (n); ++i) 7 #define For(i,s,t) for (int i = (s); i <= (t); ++i) 8 #define rFor(i,t,s) for (int i = (t); i >= (s); --i) 9 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i) 10 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i) 11 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i) 12 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) 13 14 #define pr(x) cout << #x << " = " << x << " " 15 #define prln(x) cout << #x << " = " << x << endl 16 17 #define LOWBIT(x) ((x)&(-x)) 18 19 #define ALL(x) x.begin(),x.end() 20 #define INS(x) inserter(x,x.begin()) 21 22 #define ms0(a) memset(a,0,sizeof(a)) 23 #define msI(a) memset(a,inf,sizeof(a)) 24 #define msM(a) memset(a,-1,sizeof(a)) 25 26 #define MP make_pair 27 #define PB push_back 28 #define ft first 29 #define sd second 30 31 template<typename T1, typename T2> 32 istream &operator>>(istream &in, pair<T1, T2> &p) { 33 in >> p.first >> p.second; 34 return in; 35 } 36 37 template<typename T> 38 istream &operator>>(istream &in, vector<T> &v) { 39 for (auto &x: v) 40 in >> x; 41 return in; 42 } 43 44 template<typename T1, typename T2> 45 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) { 46 out << "[" << p.first << ", " << p.second << "]" << " "; 47 return out; 48 } 49 50 typedef long long LL; 51 typedef unsigned long long uLL; 52 typedef pair< double, double > PDD; 53 typedef pair< int, int > PII; 54 typedef set< int > SI; 55 typedef vector< int > VI; 56 typedef map< int, int > MII; 57 typedef vector< LL > VL; 58 typedef vector< VL > VVL; 59 const double EPS = 1e-10; 60 const int inf = 1e9 + 9; 61 const LL mod = 1e9 + 7; 62 const int maxN = 1e5 + 7; 63 const LL ONE = 1; 64 const LL evenBits = 0xaaaaaaaaaaaaaaaa; 65 const LL oddBits = 0x5555555555555555; 66 67 LL n, k, d, a[57]; 68 LL dpMax[57][17], dpMin[57][17]; 69 LL ans = -1; 70 71 int main(){ 72 INIT(); 73 cin >> n; 74 msI(dpMin); // 初始化为 inf 75 For(i, 1, n) { 76 cin >> a[i]; 77 // 以 a[i] 结尾只取一个的最大最小值就是 a[i] 78 dpMax[i][1] = dpMin[i][1] = a[i]; 79 } 80 cin >> k >> d; 81 For(j, 2, k) { 82 For(i, j, n) { 83 For(h, 1, d) { 84 if(i - h >= 1 && i - h >= j - 1) { 85 dpMax[i][j] = max(dpMax[i][j], max(a[i] * dpMax[i - h][j - 1], a[i] * dpMin[i - h][j - 1])); 86 dpMin[i][j] = min(dpMin[i][j], min(a[i] * dpMax[i - h][j - 1], a[i] * dpMin[i - h][j - 1])); 87 } 88 } 89 } 90 } 91 92 For(i, k, n) ans = max(ans, dpMax[i][k]); 93 cout << ans << endl; 94 return 0; 95 }