Problem UVA11059-Maximum Product

Accept:4769  Submit:38713

Time Limit: 3000 mSec

 Problem Description

Given a sequence of integers S = {S1, S2, . . . , Sn}, you should determine what is the value of the maximum 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 Si is an integer such that −10 ≤ Si ≤ 10. Next line will have N integers, representing the value of each element in the sequence. There is a blank line after each test case. The input is terminated by end of file (EOF).

 Output

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

 Sample Input

3 2 4 -3
 
5
2 5 -1 2 -1
 

 Sample Ouput

Case #1: The maximum product is 8.

Case #2: The maximum product is 20.

题解:lrj给的是暴力的做法,不够这个题显然可以dp去做,维护两个dp数组,一个维护最大正值,一个维护最小负值,状态转移很简单,看代码就能明白。

P.S.原来之一用printf("%lld\n",x)来输出long long,这一次用了I64d输出,WA到怀疑人生,不知道为啥。(写这一篇题解就是为了记录这个WA点)

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
typedef long long LL; const int maxn = ;
LL dp_max[maxn],dp_min[maxn];
int iCase = ; int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int n;
while(cin >> n){
dp_max[] = dp_min[] = 0LL;
LL Max = ;
int x;
for(int i = ;i <= n;i++){
cin >> x;
if(x > ){
dp_max[i] = dp_max[i-]*x;
dp_min[i] = dp_min[i-]*x;
if(!dp_max[i]) dp_max[i] = x;
}
else if(x < ){
dp_max[i] = dp_min[i-]*x;
dp_min[i] = dp_max[i-]*x;
if(!dp_min[i]) dp_min[i] = x;
}
else{
dp_max[i] = dp_min[i] = 0LL;
}
if(Max < dp_max[i] && x) Max = dp_max[i];
}
printf("Case #%d: The maximum product is %lld.\n\n",iCase++,Max);
}
return ;
}

UVA11059-Maximum Product(动态规划)的更多相关文章

  1. UVA11059 - Maximum Product

    1.题目名称 Maximum Product 2.题目地址 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemi ...

  2. LeetCode_Maximum Subarray | Maximum Product Subarray

    Maximum Subarray 一.题目描写叙述 就是求一个数组的最大子序列 二.思路及代码 首先我们想到暴力破解 public class Solution { public int maxSub ...

  3. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

  4. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

  5. 【刷题-LeetCode】152 Maximum Product Subarray

    Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...

  6. 152. Maximum Product Subarray - LeetCode

    Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...

  7. uva 11059 maximum product(水题)——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAB1QAAAMcCAIAAABo0QCJAAAgAElEQVR4nOydW7msuhKF2wIasIAHJK

  8. [LeetCode] Maximum Product Subarray 求最大子数组乘积

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  9. 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray

    题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...

  10. Uva 11059 Maximum Product

    注意long long  long long  longlong !!!!!!   还有 printf的时候 明明longlong型的答案 用了%d  WA了也看不出,这个细节要注意!!! #incl ...

随机推荐

  1. WarShall算法

    1.引言 图的连通性问题是图论研究的重要问题之一,在实际中有着广泛的应用.例如在通信网络的联通问题中,运输路线的规划问题等等都涉及图的连通性.因此传递闭包的计算需要一个高效率的算法,一个著名的算法就是 ...

  2. python进程间通信

    Process之间有时需要通信,操作系统提供了很多机制来实现进程间的通信. 1. Queue的使用 可以使用multiprocessing模块的Queue实现多进程之间的数据传递,Queue本身是一个 ...

  3. mysql启动事件

    1.查看时间是否开启 SHOW VARIABLES LIKE 'event_scheduler' 若是查看结果为OFF,的话开启ON 2.设置事件开启 SET GLOBAL event_schedul ...

  4. 浅谈css中的盒模型(框模型)

    css中的盒模型是css的基础,盒模型的理解可以帮助我们进行对样式进行修改.废话不多说,进入正题: 在w3c中,CSS 框模型 (Box Model) 规定了元素框处理元素内容.内边框.边框 和 外边 ...

  5. SAP WM 有无保存WM Level历史库存的Table?

    SAP WM 有无保存WM Level历史库存的Table? 前日下班回家的路上,收到一个前客户内部顾问同行发过来的微信,问我在SAP系统里哪个表是用来存储WM Level历史库存的. 这个问题问住了 ...

  6. 浅谈运维中的安全问题-FTP篇

    写这一系列文章的动因很简单,在年前最后一个项目的时候在客户现场做了的几个安全加固.由于时间问题,很多东西就拿来主义没经过思考直接更改了,并未细细品味其中的原理和方法,所以特地搭建实验环境,分析下其中的 ...

  7. 计算机图形学(第2版 于万波 于硕 编著)第45页的Bresenham算法有错误

    计算机图形学(第2版 于万波 于硕 编著)第45页的Bresenham算法有错误: 书上本来要写的是以x为阶越步长的方法,但是他写的是用一部分y为阶越步长的方法(其实也写的不对),最后以x为阶越步长的 ...

  8. 监控mysql主从同步

    1,昨天看到shell一道面试题,需求如下: 监控MySQL主从同步是否异常,如果异常,则发送短信或者邮件给管理员.提示:如果没主从同步环境,可以用下面文本放到文件里读取来模拟:阶段1:开发一个守护进 ...

  9. zsh快捷键

    bash和zsh两种shell功能非常相似,只不过zsh的功能比bash更强大一些.这两种shell的快捷键命令也非常相似,对于常用shell的人来说,了解一些快捷键操作将会十分有益,因为使用快捷键将 ...

  10. [20190219]那个更快(11g).txt

    [20190219]那个更快(11g).txt --//前几天测试11g Query Result Cache RC Latches时,链接http://blog.itpub.net/267265/v ...