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
- UVa 11059----Maximum Product(最长上升子序列乘积)
- 最大乘积(Maximum Product,UVA 11059)
- 最大乘积(Maximum Product,UVa 11059)
- 最大乘积(Maximum Product,UVA 11059)
- UVA 11059 Maximum Product 最大乘积
- 最大乘积(Maximum Product, UVa 11059)
- [leetcode]Maximum Product Subarray最大子序列乘积
- [Lintcode]Maximum Product Subarray乘积最大子序列
- uva 10051(最长上升子序列)
- uva 10534 nlogn最长上升子序列
- uva 10635(最长上升子序列)
- UVA 10534最长上升子序列运用
- 最长乘积子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- spring集成quartz定时任务的配置
- 安卓开发中遇到的Intent的startActivityForResult方法回调后不执行onActivityResult方法
- 到哈哈哈吧
- Java开发记录
- 关于oracle with as用法
- UVa 11059----Maximum Product(最长上升子序列乘积)
- 一些软件工具注册码
- 【RequireJS 学习笔记】RequireJS 学习笔记 1 —— RequireJS 界的 Hello World
- VC解析XML
- 关于反转链表的问题
- Adaboost学习资料
- 如何关闭应用程序
- 动态规划 基础总结
- Android table布局开发的一个简单的计算器