题目来源:码蹄集
https://matiji.net/exam/brushquestion/158/778/B3FCFEC101BD05189BB74D522E019504
时间限制:1000ms
内存限制:65535kb
题目描述:数组中有N个元素,找出乘积最大的连续子序列,输出他们的乘积。
输入格式:第一行输入数组长度N,第二行输入数组元素,整型,空 格分隔。
输出格式:输出整型
输入样例:5
2 5 -1 2 -1
输出样例:20
参考程序:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
#define ll long long
ll a[10000] ;
ll ma[10000] , mi[10000] ;
ll max1 (ll a, ll b, ll c){
ll m = max (a, b) ;
m = max (m , c) ;
return m;
}
ll min1 (ll a, ll b, ll c){
ll m = min (a , b);
return min (m, c) ;
}
ll ans = -1e16;
void dp1(ll n){
for (int i = 1; i <= n; i++){
ma[i] = max1 (a[i] , a[i] * ma[i - 1] , a[i] * mi[i - 1]) ;
mi[i] = min1 (a[i] , a[i] * ma[i - 1] , a[i] * mi[i - 1]) ;
ans = max1 (mi [i] , ma [i], ans) ;
}
}
int main(){
int n;
cin >> n;
for (int i = 1; i <= n; i++){
scanf("%lld",&a[i]);
}
dp1(n);
cout << ans ;
return 0;
}