HLG 2116 Maximum continuous product (最大连续积 DP)

链接:  http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2116

Description

Wind and his GF(game friend) are playing a small game. They use the computer to randomly generated a number sequence which only include number 2,0 and -2. To be the winner,wind must

have to find a continuous subsequence whose continuous product is maximum.

For example, we have a sequence blow:

2 2 0 -2 0 2 2 -2 -2 0

Among all of it‘s continuous subsequences, 2 2 -2 -2 own the maximum continuous product.

(2*2*(-2)*(-2) = 16 ,and 16 is the maximum continuous product)

You,wind‘s friend,can give him a hand.

Input

The first line is an integer T which is the Case number(T <= 200).

For each test case, there is an integer N indicating the length of the number sequence.(1<= N <= 10000)

The next line,there are N integers which only include 2,0 and -2.

Output

For each case,you have to output the case number first(Reference the sample).

If the answer is smaller than 0, you just need to output 0 as the answer.

If the answer‘s format is 2^x,you need to output the x as the answer.

Output the answer in one line.

Sample Input

2

2

-2 0

10

2 2 0 -2 0 2 2 -2 -2 0

Sample Output

Case #1: 0

Case #2: 4

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstring>
#define MAXN 15000
#define RST(N)memset(N, 0, sizeof(N))
using namespace std;

int n, ans, cas, T = 1;
int a[MAXN], mn[MAXN], mx[MAXN];

void Init()
{
    scanf("%d", &n);
    for(int i=1; i<=n; i++) scanf("%d", &a[i]);
    RST(mx), RST(mn), ans = 0;
}

int solve()
{
    for(int i=1; i<=n; i++) {
        if(a[i] > 0) {
            if(mn[i-1] > 0) mn[i] = mn[i-1]+1;
            mx[i] = mx[i-1]+1;
        }else if(a[i] < 0) {
            if(mn[i-1] > 0) mx[i] = mn[i-1]+1;
            if(mx[i-1] > 0) mn[i] = mx[i-1]+1;
            else mn[i] = 1;
        }else {
            mx[i] = 0;
            mn[i] = 0;
        }
        ans = max(ans, mx[i]);
    }
    return ans;
}

int main()
{
    scanf("%d", &cas);
    while(cas--) {
        Init();
        printf("Case #%d: %d\n", T++, solve());
    }
    return 0;
}

HLG 2116 Maximum continuous product (最大连续积 DP)

时间: 2024-08-25 17:20:50

HLG 2116 Maximum continuous product (最大连续积 DP)的相关文章

Maximum Pairwise Product

问题描述 Given a sequence of non-negative integers a0,…,an−1, find the maximum pairwise product, that is, the largest integer that can be obtained by multiplying two different elements from the sequence (or, more formally, max0≤i≠j≤n−1aiaj). Different el

UVA108 - Maximum Sum(最大连续和)

题意:从一个n*n的矩阵中找出和最大的子矩阵 思路:最大连续和的求解.我们可以将二维的转化为一维进行计算.sum[i][j]表示以(1, 1)和(i, j)为对角的矩阵的和.之后只要逐个枚举每个可能出现的值,保存最大值即可. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int INF = 0x3

HDU 1231 最大连续子序列 DP题解

典型的DP题目,增加一个额外要求,输出子序列的开始和结尾的数值. 增加一个记录方法,nothing special. 记录最终ans的时候,同时记录开始和结尾下标: 更新当前最大值sum的时候,更新开始节点. const int MAX_N = 10001; long long arr[MAX_N]; int N, sta, end; long long getMaxSubs() { long long sum = 0, ans = LLONG_MIN; int ts = 0; for (int

[LeetCode] Maximum Product Subarray 连续数列最大积

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. Hide Tags Array Dynamic Programming 这个问题是给

UVa 108 - Maximum Sum(最大连续子序列)

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=44  Maximum Sum  Background A problem that is simple to solve in one dimension is often much more difficult to solve in more th

算法设计与分析[0009] Dynamic Programming(II)(Maximum Sum/Product Subarray)

原文引用https://www.dazhuanlan.com/2019/08/25/5d625b5c4d1ea/ 本文通过 53. Maximum Subarray & 152. Maximum Product Subarray 分析根据动态规划思路进行问题求解中的一个关键环节:子问题的拆分和求解. Problem Description 两道题解决的问题相似,都是求解给定序列中满足某种数学特征(和最大/乘积最大)的子序列,虽然不需要将该子序列输出. 留意的关键字眼是:containing at

[LintCode] Continuous Subarray Sum 连续子数组之和

Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your code should return the index of the first number and the index of the last number. (If their are duplicate answer, return anyone) Have you met this quest

HDU 4002 Find the maximum (欧拉函数-积性函数的性质(2011年大连赛区网络赛第二题)

[题目链接]:click here~~ [题目大意]: 给出一个整数n,求一个数x,x在1到n之间,并且x/φ(x)最大(其中φ(x)为x的欧拉函数). [思路]: 由欧拉函数为积性函数,即:如果 则有: 且: 则有: 要使f(x)最大,须使x含尽量多的不同素数因子. 代码: /* * Problem: HDU No.4002 * Running time: 1700MS * Complier: java * Author: javaherongwei * Create Time: 0:08 2

HDU ACM 4561 连续最大积-&gt;DP

分析:dp[i][0]表示到第i个数成绩为负时的最大积个数,dp[i][1]表示到第i个数成绩为正时的最大积个数. #include<iostream> using namespace std; #define N 10005 #define max(a,b) ((a)>(b)?(a):(b)) int dp[N][2]; int main() { int T,t,i,n,sum,x; t=1; scanf("%d",&T); while(T--) { sca