UVa 11059----Maximum Product(最长上升子序列乘积)

来源:互联网 发布:看澳洲股市软件 编辑:程序博客网 时间:2024/05/17 04:42
Maximum Product
Time limit: 3.000 seconds
Given a sequence of integers S = {S1, S2, . . . , Sn}, you should determine what is the value of themaximum positive product involving consecutive terms of S. If you cannot find a positive sequence,you should consider 0 as the value of the maximum product.

Input

Each test case starts with 1 ≤ N ≤ 18, the number of elements in a sequence. Each element Siisan integer such that −10 ≤ Si ≤ 10. Next line will have N integers, representing the value of eachelement in the sequence. There is a blank line after each test case. The input is terminated by end offile (EOF).

Output

For each test case you must print the message: ‘Case #M: The maximum product is P.’, whereM is the number of the test case, starting from 1, and P is the value of the maximum product. Aftereach test case you must print a blank line.

Sample Input

3
2 4 -3

5
2 5 -1 2 -1

Sample Output

Case #1: The maximum product is 8.

Case #2: The maximum product is 20.



最大连续子序列乘积,是动态规划,应该注意负负得正的情况,还得注意对空间复杂度的优化!!

min,max函数的头文件是#include<algorithm> usingnamespace std;


#include<bits/stdc++.h>using namespace std;long long a[20];int main(){long long ans,Cmin,Cmax,x,y;int t=0,i,n;while(~scanf("%d",&n)){for(i=0;i<n;i++) scanf("%lld",&a[i]);ans=Cmin=Cmax=a[0];for(i=1;i<n;i++)        {        x=max(a[i],max(Cmax*a[i],Cmin*a[i]));            y=min(a[i],min(Cmax*a[i],Cmin*a[i]));            Cmax=x;Cmin=y;            if(ans<Cmax) ans=Cmax;        }        if(ans<0) ans=0;        printf("Case #%d: The maximum product is %lld.\n\n",++t,ans);}return 0;}


0 0