[UCSD白板题] Maximum Pairwise Product
Problem Description
Task.Given a sequence of non-negative integers \(a_0, ..., a_{n-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,\(\max \limits_{0\leq{i \neq j}\leq {n-1}}\ a_ia_j\)).Different elements here mean \(a_i\) and \(a_j\) with \(i \neq j\) (it can be the case that \(a_i=a_j\)).
Input format.The first line of the input contains an integer \(n\).The next line contains\(n\)non-negative integers \(a_0, ..., a_{n-1}\) (separated by spaces).
Constraints.\(2 \leq n \leq 2 \cdot 10^5; 0 \leq a_0, ..., a_{n-1} \leq 10^5\).
Output format.Output a single number - the maximum pairwise product.
Sample 1.
Input:
3
1 2 3
Output:
6
Sample 2.
Input:
10
7 5 14 2 8 8 10 1 2 3
Output:
140
Sample 3.
Input:
5
4 6 2 6 1
Output:
36
Solution
# Uses python3
n = int(input())
a = [int(x) for x in input().split()]
assert(len(a) == n)
fstMax = sndMax = 0
for idx in range(0, n):
if fstMax < a[idx]:
fstMax, sndMax = a[idx], fstMax
elif sndMax < a[idx]:
sndMax=a[idx]
print(fstMax*sndMax)
[UCSD白板题] Maximum Pairwise Product的更多相关文章
- [UCSD白板题] Minimum Dot Product
Problem Introduction The dot product of two sequences \(a_1,a_2,\cdots,a_n\) and \(b_1,b_2,\cdots,b_ ...
- [UCSD白板题] Pairwise Distinct Summands
Problem Introduction This is an example of a problem where a subproblem of the corresponding greedy ...
- [UCSD白板题] Maximize the Value of an Arithmetic Expression
Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...
- [UCSD白板题] Take as Much Gold as Possible
Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...
- [UCSD白板题] Binary Search
Problem Introduction In this problem, you will implemented the binary search algorithm that allows s ...
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- [UCSD白板题] Compute the Edit Distance Between Two Strings
Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...
- [UCSD白板题] Primitive Calculator
Problem Introduction You are given a primitive calculator that can perform the following three opera ...
- [UCSD白板题] Points and Segments
Problem Introduction The goal in this problem is given a set of segments on a line and a set of poin ...
随机推荐
- PO、VO、BO、DTO、POJO、DAO
J2EE开发中大量的专业缩略语很是让人迷惑,尤其是跟一些高手讨论问题的时候,三分钟就被人家满口的专业术语喷晕了,PO VO BO DTO POJO DAO,一大堆的就来了(听过老罗对这种现象的批判的朋 ...
- 【BZOJ2874】训练士兵(主席树)
题意:有一个N*M的矩阵,给出一些形如(x1,y1,x2,y2,s)的操作,代表(x1,y1)到(x2,y2)都被加上了s这个数 现在有一些强制在线的询问,询问(x1,y1)到(x2,y2)的和 对于 ...
- Android剪贴板操作----ClipboardManager
andrid developers java.lang.Object ---android.text.ClipboardManager ------android.context.ClipboardM ...
- Masonry控制台打印约束冲突问题解决
不知道你是不是视图的布局也是用的第三方Masonry,在使用中是不是也遇到了控制台约束冲突的警告打印,看下图: 从输出的信息可以知道,有的控件的约束明显重复了设置,所以指出了是哪个控件,重复设置了哪些 ...
- 事务BEGIN TRANSACTION
事务(Transaction)是并发控制的基本单位.所谓的事务,它是一个操作序列,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位.例如,银行转账工作:从一个账号扣款并使另一个账号增款,这 ...
- Oracle中的正则表达式
检查约束 --密码的长度必须在3-6 --年龄必须在1-120 --性别只能是男或女 --电话号码必须满足电话的格式: 手机格式,座机格式 drop table test; select * from ...
- android访问webservice
// nameSpace 命名空间,methodName:方法名字:maps:参数集合:webserviceUrl:访问的webservice的网址:比如:http://17.18.199.100:8 ...
- PDFsharp Samples
http://www.pdfsharp.net/wiki/PDFsharpSamples.ashx http://www.pdfsharp.net/?AspxAutoDetectCookieSuppo ...
- 根据UIColor对象,获取对应的RGBA值
- (NSArray *)getRGBWithColor:(UIColor *)color { CGFloat red = 0.0; CGFloat green = 0.0; CGFloat blue ...
- Oracle循环查询结果集 自定义函数
create or replace function Fun_GetRoleIDList(d_fid char) return varchar is rolelist varchar(2000);b ...