Continuous Maximum Product
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission (s): 813 Accepted Submission (s): 305
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
#include <iostream>#include <vector>using namespace std;int main(void){int T;int i;cin>>T;vector <int> vr;for(i=0;i<T;i++){int N;cin>>N;int j;vector <int> vk;for(j=0;j<N;j++){int k;cin>>k;vk.push_back(k);}int max=0;int temp=0;int cur=0;for(j=0;j<N;j++){int k=vk[j];if(k>0)temp++;if(k<0){if(cur!=0){temp+=cur+1;cur=0;}else{if(temp>max)max=temp;cur=temp+1;temp=0;}}if(k==0||j==N-1){if(temp>max)max=temp;temp=0;cur=0;}}temp=0;cur=0;for(j=N-1;j>-1;j--){int k=vk[j];if(k>0)temp++;if(k<0){if(cur!=0){temp+=cur+1;cur=0;}else{if(temp>max)max=temp;cur=temp+1;temp=0;}}if(k==0||j==0){if(temp>max)max=temp;temp=0;cur=0;}}vr.push_back(max);}for(i=0;i<T;i++){cout<<"Case #"<<i+1<<": "<<vr[i]<<endl;}return 0;}