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. HTML第三天作业做的表格

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  2. Angularjs学习笔记(五)----显示和格式化数据

    一.引用指令 在AngularJS的文档中,所有指令的名字以驼峰命名法.而在模板中,则需要以蛇形命名法.可以以冒号分割(ng:model)或下划线分割(ng_model),更常见的是以ng-model ...

  3. Everything Be True

    function every(collection, pre) { // Is everyone being true? //return pre; for(var i in collection){ ...

  4. 取出Object对象里面的字段,

    Type s = result.GetType(); bool f = (bool)s.GetField("Succeed").GetValue(result);//Succeed ...

  5. [NOIP2015]信息传递

    [NOIP2015]信息传递[问题描述]有

  6. mb_系列函数和普通字符函数的区别

    <?php //phpinfo(); $str = 'abcdef'; echo strlen($str);// 6 echo '<br/>'; echo substr($str, ...

  7. coredump

    COREDUMP调试的使用 一,什么是coredump 跑程序的时候经常碰到SIGNAL 或者 call trace的问题,需要定位解决,这里说的大部分是指对应程序由于各种异常或者bug导致在运行过程 ...

  8. 如何使用Nginx对抗DDoS攻击?

    时不时的就有客户会被DDoS一下.很多时候攻击很简单也容易封堵,但是攻击的目标是应用的时候就更难防御.在这里云端卫士介绍一下使用Nginx作为代理过滤器来封堵一些这种攻击. Apache DDoS攻击 ...

  9. 易全解token获取

    //易全解app             string strClientID = "2016061711434943493606";             string str ...

  10. Groovy 处理 XML

    1. Parsing XML 1.1. XmlParser and XmlSlurper The most commonly used approach for parsing XML with Gr ...