DP7. 连续子数组的最大乘积
描述
输入描述
输出描述
输出子数组的乘积的最大值示例1
输入:
4 3 2 -2 4
输出:
6
说明:
子数组[3,2]的乘积为6,[3,2,-1,4]的乘积为-24,[4]的乘积为4,故返回6示例2
输入:
3 -3 0 -2
输出:
0
说明:
因为0在中间,所有包含0的子数组的乘积都为0,另外的数都是负数,所以最大乘积的子数组为[0],返回为0,因为子数组要求是连续的,所以[-3,-2]不是[-3,0,-2]的子数组,所以不能为6,示例3
输入:
3 -3 2 -2
输出:
12
C++ 解法, 执行用时: 7ms, 内存消耗: 420KB, 提交时间: 2022-08-05
#include<cstdio> #include<algorithm> using namespace std; template <typename T> inline void read(T& f) { f = 0; T fu = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') { fu = -1; } c = getchar(); } while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); } f *= fu; } template <typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x < 10) putchar(x + 48); else print(x / 10), putchar(x % 10 + 48); } int main(){ int maxs; //结果 int dpmax, dpmin; //以i位结尾的子数组最大的乘积,以i位结尾的子数组最小的乘积 int nmax, nmin; //新最大最小值 int num; int n; read(n); read(num); dpmax = dpmin = maxs = num; for(int i = 1;i < n;i++){ read(num); if(num < 0) nmax = max(dpmin * num, num); else nmax = max(dpmax * num, num); nmin = min(num, min(dpmin * num, dpmax * num)); dpmax = nmax, dpmin = nmin; maxs = max(dpmax, maxs); } print(maxs); return 0; }
C++ 解法, 执行用时: 10ms, 内存消耗: 2700KB, 提交时间: 2022-03-11
#include <bits/stdc++.h> using namespace std; template <typename T> inline void read(T &f) { f=0;T fu=1;char c=getchar(); while(c<'0'||c>'9') {if(c=='-'){fu=-1;}c=getchar();} while(c>='0'&&c<='9') {f=(f<<3)+(f<<1)+(c&15);c=getchar();} f*=fu; } template <typename T> void print(T x) { if(x<0) putchar('-'),x=-x; if(x<10) putchar(x+48); else print(x/10),putchar(x%10+48); } template <typename T> void print(T x, char t) { print(x);putchar(t); } const int maxn=2e5+50; int dpmax[maxn],dpmin[maxn],a[maxn]; int n; void solve() { read(n); for(int i=1;i<=n;++i) read(a[i]); int ans=a[1]; dpmax[1]=dpmin[1]=a[1]; for(int i=2;i<=n;++i) { dpmax[i]=max(max(dpmax[i-1]*a[i],dpmin[i-1]*a[i]),a[i]); dpmin[i]=min(min(dpmax[i-1]*a[i],dpmin[i-1]*a[i]),a[i]); ans=max(ans,dpmax[i]); } print(ans); } int main() { #ifdef AC freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif clock_t program_start_clock=clock(); solve(); fprintf(stderr,"\nTotal Time: %lf ms",double(clock()-program_start_clock)/(CLOCKS_PER_SEC/1000)); return 0; }
C++ 解法, 执行用时: 16ms, 内存消耗: 524KB, 提交时间: 2022-04-05
#include <iostream> using namespace std; int main() { int N,firstNeg=1,start = 1; int ans=-0x3f3f3f3f,allMul=1,secondMul=1; scanf("%d",&N); for(int i=1,t;i<=N;++i){ scanf("%d",&t); if(t==0 || i==N+1){ ans =max(ans,t); firstNeg = 1; start = 1; secondMul = 1; allMul = 1; } else{ if(t<0 && firstNeg==1){ firstNeg = 2; //secondMul=t; } else if(firstNeg==2){ secondMul*=t; ans = max(ans,secondMul); } allMul *= t; ans = max(ans,allMul); //cout<< ans <<endl; } } printf("%d",ans); return 0; }
C 解法, 执行用时: 17ms, 内存消耗: 408KB, 提交时间: 2022-04-05
#include <stdio.h> int max(int a, int b){ return a>b?a:b; } int main() { int N , ans=-0x3f3f3f3f,allMul=1,secondMul=1,firstNeg=1; scanf("%d",&N); for(int i=1,t;i<=N;++i){ scanf("%d",&t); if(t==0){ ans =max(ans,t); firstNeg = secondMul = allMul = 1; } else{ if(t<0 && firstNeg==1){ firstNeg = 2; } else if(firstNeg==2){ secondMul*=t; ans = max(ans,secondMul); } allMul *= t; ans = max(ans,allMul); } } printf("%d",ans); return 0; }
C++ 解法, 执行用时: 17ms, 内存消耗: 932KB, 提交时间: 2022-07-20
#include <bits/stdc++.h> // C++万能头文件 using namespace std; static const auto io_sync_off = [](){ // lambda表达式 std::ios::sync_with_stdio(false); // 解除与scanf()等函数的同步 std::cin.tie(nullptr); // 解除cin和cout的绑定 return nullptr; }(); int main() { int curmax = 1, curmin = 1, res = INT_MIN; int n; cin >> n; while (n--) { int num; cin >> num; if (num < 0) // 当前最大值乘以负数为最小,最小值乘以负数为最大 swap(curmax, curmin); curmax = max(curmax * num, num); curmin = min(curmin * num, num); res = max(res, curmax); } cout << res << "\n"; return 0; }