Problem Introduction

The dot product of two sequences \(a_1,a_2,\cdots,a_n\) and \(b_1,b_2,\cdots,b_n\) of the same length is equal to \(\sum\limits_{i=1}^na_ib_i=a_1b_1+a_2b_2+\cdots+a_nb_n\)

Problem Description

Task.The goal is,given two sequences $a_1,a_2,\cdots,a_n $ and \(b_1,b_2,\cdots,b_n\) find a permutation \(\pi\) of the second sequence such that the dot product of \(a_1,a_2,\cdots,a_n\) and \(b_{{\large{\pi}}_1}, b_{{\large{\pi}}_2}, \cdots, b_{{\large{\pi}}_n}\) is minumum.
Input Format.

Constraints.$1 \leq n \leq 10^3; -10^5 \leq a_i, b_i \leq 10^5; $ for all \(1 \leq i \leq n\).

Output Format.Out put the minimum possible product.

Sample 1.
Input:

1
23
39

Output:

897

Sample 2.
Input:

3
1 3 -5
-2 4 1

Output:

-25

Solution

#Uses python3
import sys

def min_dot_product(a, b):
    res = 0
    a = sorted(a)
    b = sorted(b, reverse=True)
    for idx in range(len(a)):
        res += a[idx] * b[idx]
    return res

if __name__ == '__main__':
    input = sys.stdin.read()
    data = list(map(int, input.split()))
    n = data[0]
    a = data[1:(n + 1)]
    b = data[(n + 1):]
    print(min_dot_product(a, b))

[UCSD白板题] Minimum Dot Product的更多相关文章

  1. [UCSD白板题] Maximum Pairwise Product

    Problem Description Task.Given a sequence of non-negative integers \(a_0, ..., a_{n-1}\),find the ma ...

  2. [UCSD白板题] Compute the Edit Distance Between Two Strings

    Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...

  3. [UCSD白板题] Primitive Calculator

    Problem Introduction You are given a primitive calculator that can perform the following three opera ...

  4. [UCSD白板题] Covering Segments by Points

    Problem Introduction You are given a set of segments on a line and your goal is to mark as few point ...

  5. [UCSD白板题] Changing Money

    Problem Introduction In this problem,you will design an algorithm for changing money optimally. Prob ...

  6. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  7. [UCSD白板题] Maximize the Value of an Arithmetic Expression

    Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...

  8. [UCSD白板题] Take as Much Gold as Possible

    Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...

  9. [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 ...

随机推荐

  1. Halcon学习之tuple

    * define a tuple for int, double, string... not for object d:=[] * assignment d[0] := 'a string' * g ...

  2. eclipse color theme 选择一款适合你的代码样式

    1 点击eclipse菜单栏上的Help ->Install New Softwave , 点击add按钮 随便写个名字 ,添加  地址   http://eclipse-color-theme ...

  3. sql server 2008 外键关联的设置和取消

    直接上图片 选中表右击-设计 找到需要设置外键的字段.右击-关系,在弹出的对话框中点击添加 选择右边的小按钮点击.选择主键表和关联的主键ID,以及外建表的关联字段. 建立外键完成. 删除的话选中某个外 ...

  4. 启动项目时,报错;Address already in use: JVM_Bind<null>:8080

    Address already in use: JVM_Bind<null>:8080在MyEclipse启动或者是tomcat启动的时候出现:Address already in use ...

  5. Ubuntu:我不小心把/var/lock文件夹给删了

    在一个风和日丽的下午,不正常关闭minicom导致了device 没有正常解锁,于是使用minicom的时候提示 device is locked: 根据网上看到的方法只要把/var/lock 里面的 ...

  6. iOS基础-NSString及NSMutableString剖析

    一.NSString头文件 NSString : NSObject 实现协议: NSCopying/NSMutableCopying/NSSecureCoding 类别: //扩展类别 NSStrin ...

  7. 反汇编工具capstone安装后import error

    使用sudo pip install capstone后,使用如下代码import时出现error. from capstone import * 错误信息: File "/usr/loca ...

  8. js实例:验证只能输入数字和一个小数点

    分享一个javascript脚本代码,用于验证只能输入数字和一个小数点,检测数字输入是否符合要求,效果不错,有用到的朋友拿去吧. 原文地址:http://www.jbxue.com/article/1 ...

  9. ajax跨域往php程序post数据时,php程序总是执行两次的解决方法

    php程序是部署在IIS7上面,ajax提交数据时,遇到了两个问题,一个就是跨域,一个php程序总会被执行两次. 第一个问题的解决方法,是百度出来的,添加下面几行代码就可以了: header('Acc ...

  10. 去除inline-block之间的间距

    a标签的父容器添加: font-size: 0; -webkit-text-size-adjust:none;