Continuous Maximum Product
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission (s): 699 Accepted Submission (s): 275
Problem Description
Tom and his good friend Tom are playing a game. The computer randomly generates an array consisting of-2, 0, and 2, who first calculates the maximum product of a continuous element in this array, even if it wins!
For example, we have the following random array:
2 2 0-2 0 2-2-2 0
Among the many consecutive subsequences in this array, the product of the continuous subsequence 2-2-2 is the largest.
James asks you to calculate the maximum value.
Input
Enter a positive integer T in the first line, indicating a total of T groups of data (T <= 200 ).
For the next T group data, input N in the first row of each group data, indicating the total number of elements in the array (1 <= N <= 10000 ).
Then, enter N elements consisting of 0,-2, and separate them with spaces.
Output
For each group of data, the number of cases is output first.
If the final answer is less than or equal to 0, 0 is output directly.
Otherwise, if the answer is 2 ^ x, output x.
Each group of data occupies one row. For the specific output format, see the sample.
Sample Input
2
2
-2 0
10
2 2 0-2 0 2-2-2 0
Sample Output
Case #1: 0
Case #2: 4
Source
2013 Jinshan xishanju creative game program challenge-semi-finals (2)
Recommend
Liuyiding
Consider 0 as a partition, divide the number of strings into multiple segments, and then calculate the continuous product of a maximum of several numbers in each small segment as a positive number, and then take a maximum value.
#include<stdio.h>int a[50000+100],n;int solve(int s,int e){ int i,cnt=0,st,ed,flag=1,ans=0; if(s==n+1) return 0; for(i=s;i<e;i++) { if(a[i]==-2) { if(flag) {flag=0;st=i;} ed=i; cnt++; } } if(cnt%2==0) return e-s; if(ans<st-s) ans=st-s; if(ans<e-st-1) ans=e-st-1; if(ans<e-ed-1) ans=e-ed-1; if(ans<ed-s) ans=ed-s; return ans;}int main(){ int cas,i,k,ans; scanf("%d",&cas); for(k=1;k<=cas;k++) { ans=0; scanf("%d",&n); for(i=1;i<=n;i++) scanf("%d",&a[i]); a[0]=0; for(i=0;i<=n;) { if(a[i]==0) { int ii=i+1; while(1) { i++; if(i==n+1||a[i]==0) { int mid=solve(ii,i); if(ans<mid) ans=mid; break; } } } } printf("Case #%d: %d\n",k,ans); } return 0;}