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 elements here mean ai and ajwith i≠j (it can be the case that ai=aj).

输入格式

The first line of the input contains an integer n. The next line contains nnon-negative integers a0,…,an−1 (separated by spaces).

限制条件

2≤n≤2⋅105; 0≤a0,…,an−1≤105.

输出格式

Output a single number — the maximum pairwise product.

样例 1

输入:

3

1 2 3

输出:

6

样例 2

输入:

10

7 5 14 2 8 8 10 1 2 3

输出:

140

样例 3 

输入:

5

4 6 2 6 1

输出:

36

n = int(input())
a = [int(x) for x in input().split()]
assert(len(a) == n)

curMax=sndMax=0
for idx in range(0, n):
    if curMax < a[idx]:
        sndMax = curMax
        curMax = a[idx]
    elif sndMax < a[idx]:
        sndMax=a[idx]
print(curMax*sndMax)
时间: 2024-12-26 05:16:49

Maximum Pairwise Product的相关文章

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 -

算法设计与分析[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

HackerRank - Find Maximum Index Product

I treated it too difficult.. nothing special, just some optimization mentioned as: http://saraguru.weebly.com/how-i-solved/find-maximum-index-product-hackerrank #include <cmath> #include <cstdio> #include <vector> #include <map> #i

LeetCode Maximum Product Subarray(枚举)

LeetCode Maximum Product Subarray 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

UVA OJ-11095 Maximum Product(暴力求解法)

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 pro

leetcode-Maximum Product Subarray zz

LinkedIn 高频题 – Maximum Sum/Product Subarray Maximum Sum Subarray是leetcode原题,跟Gas Station的想法几乎一模一样.解答中用到的结论需要用数学简单地证明一下. 1 2 3 4 5 6 7 8 9 10 11 12 public int maxSubArray(int[] A) {     int sum = 0;     int max = Integer.MIN_VALUE;     for (int i = 0;

Floyd-Warshall+二进制枚举SRM 661 Div2 Medium: BridgeBuilding

SRM 661-Medium: BridgeBuildingDiv2  Problem Statement You have two rows of nodes. Each row contains N nodes, numbered 0 through N-1 from the left to the right. Within each row, adjacent nodes are already connected by edges. You are given the lengths

Program B 暴力求解

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 ?nd a positive sequence, you should consider 0 as the value of the maximum product

暴力求解最大乘积

题意: 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 maximu