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. 【Java每日一题】20170310

    20170309问题解析请点击今日问题下方的“[Java每日一题]20170310”查看(问题解析在公众号首发,公众号ID:weknow619) package Mar2017; public cla ...

  2. python使用gevent实现并发下载器

    并发下载原理 import gevent from gevent import monkey import urllib.request monkey.patch_all() def my_downl ...

  3. Gvim 和vim 有什么区别

    Gvim 和vim 有什么区别 Gvim是windows的 vim是linux的黑色的命令符 Gvim是单独的窗口下的vim,像notepad一样. vim就是在黑乎乎的cmd窗口下的编辑器.wind ...

  4. 什么是Docker Volume?

    摘要:Docker Volume,通常翻译为数据卷,用于保存持久化数据.当我们将数据库例如MySQL运行在Docker容器中时,一般将数据通过Docker Volume保存在主机上,这样即使删除MyS ...

  5. 洛谷P4064 [JXOI2017]加法(贪心 差分)

    题意 题目链接 Sol 这题就是一个很显然的贪心... 首先二分一个答案,然后check是否可行.check的时候我们需要对每个位置\(i\),维护出所有左端点在\(i\)左侧,右端点在\(i\)右侧 ...

  6. Android和H5进行数据交互,Android获取H5Input框中的内容

    项目中嵌入了H5 页面,这个时候就需要拿到H5 input中的内容进行数据传递,先看实现的效果图

  7. matlab练习程序(求向量间的旋转矩阵与四元数)

    问题是这样,如果我们知道两个向量v1和v2,计算从v1转到v2的旋转矩阵和四元数,由于旋转矩阵和四元数可以互转,所以我们先计算四元数. 我们可以认为v1绕着向量u旋转θ​角度到v2,u垂直于v1-v2 ...

  8. Docker Data Center系列(四)- 离线安装UCP和DTR

    本系列文章演示如何搭建一个mini的云平台和DevOps实践环境. 基于这套实践环境,可以部署微服务架构的应用栈,演练提升DevOps实践能力. 1 离线安装UCP 1.1 可用版本 Version ...

  9. turnserver 配置说明记录

    coTurn工程提供了较完整的STUN和TURN服务,记录其主要的命令行参数配置说明 针对TURN/STUN服务进程turnserver.exe的使用参数做简单说明 -L 监听的IP地址 -p 监听端 ...

  10. 漫说996icu黑名单

    以实际行动声援996icu项目. https://github.com/996icu/996.ICU/blob/master/blacklist/blacklist.md 996公司黑名单,京东,华为 ...