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. fork/join 全面剖析

    fork/join作为一个并发框架在jdk7的时候就加入到了我们的java并发包java.util.concurrent中,并且在java 8 的lambda并行流中充当着底层框架的角色.这样一个优秀 ...

  2. 腾讯云下的CentOS7 配置 Apache服务器

    第一步 :安装Apache服务程序(软件包名为httpd) * yum install httpd 第二步:配置httpd.conf文件 * vi /etc/httpd/conf/httpd.conf ...

  3. 【 js 工具 】如何在Github Pages搭建自己写的页面?

    最近发现 github 改版了,已没有像原来的 Launch automatic page generator 这样的按钮等,所以我对我的文章也进行了修正,对于新版来说,步骤更加简单了.欢迎享用. - ...

  4. 玩游戏 学Flex布局

    大家好,今天推荐一个学习Flex布局的网页小游戏,非常不错,是国外的牛人开发的,值得一试,重复几次,就会大概了解Flex的属性了! 地址: http://flexboxfroggy.com/#zh-c ...

  5. Phaser.js之简单的跑酷游戏

    采用的物理引擎是Phaser.js 官网地址:http://phaser.io/ 在这里对此引擎不做过多介绍(因为我也是小白,嘿嘿) 效果展示: 源码(详细源码图片资源可点击文章下方或屏幕右上方的gi ...

  6. wangEditor更改默认高度

    在使用WangEditor时觉得高度太低,默认是300px;想调下高度,借鉴https://blog.csdn.net/qq_31384551/article/details/83240188, 网址 ...

  7. (网页)websocket后台调用Service层

    百度论坛里面有很多好的方法,借鉴. 重点:因为项目是StringBoot所以我用的是下面的方法很好使: Service.... service = (Service....) ContextLoade ...

  8. SAP生产机该不该开放Debuger权限

    前段时间公司定制系统在调用SAP RFC接口的时候报错了,看错误消息一时半会儿也不知道是哪里参数数据错误,就想着进到SAP系统里面对这个接口做远程Debuger,跟踪一下参数变量的变化,结果发现根本就 ...

  9. ERP按序打印问题

    按序打印只适合一个机器,不适合主副机模式,主副机模式请勾选同时打印 如果开启主副机模式勾选了按序打印,会造成副机下厨后厨不出单

  10. HBase最佳实践之Scan

    一.简介 HBase中Scan从大的层面来看主要有三种常见用法:ScanAPI.TableScanMR以及SnapshotScanMR.三种用法的原理不尽相同,扫描效率当然相差甚远,最重要的是这几种用 ...