考虑负负得正情况
比如 -6 * -4 = 24 -2 * -4 = 8 所以也要取min
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 2 * 100010;
int f[N], g[N];
int main() {
int n;
cin >> n;
vector<int> a(n);
for(int i = 0; i < n; i ++){
cin >> a[i];
f[i] = a[i];
g[i] = a[i];
}
int res = a[0];
for(int i = 1; i < n; i ++){
f[i] = max(g[i - 1] * a[i], max(f[i - 1] * a[i], f[i]));
g[i] = min(f[i - 1] * a[i], min(g[i - 1] * a[i], g[i]));
res = max(res, f[i]);
}
cout << res;
return 0;
}

京公网安备 11010502036488号