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. error-2015-9-9

    类型的建键部分无效,该键的所有部分均不可为null 映射从第行开始的片段时有问题 表的键具有潜在运行时冲突 列映射到概念端的属性 但是它们未形成entitySet的键属性 报错: 未能加载文件或程序集 ...

  2. Python--Argparse学习感悟

    笔者在https://docs.python.org/2/howto/argparse.html#id1上,学习到了argparse的基本概念和使用规范,学习过后忍不住将自己的一些体会和大家分享一下. ...

  3. C# IntPtr转换为Byte[]

    [DllImport("OpenNetStream.dll")] public static extern int OpenSDK_Data_GetDevList(IntPtr s ...

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

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

  5. 第13章 .NET应用程序配置

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...

  6. 你不知道的this指向

    javascript中,我们预想的this指向,有时候与预期不一样,直接上经典例子 window.name=2; var test={ 'name':1, 'getName':function(){ ...

  7. 使用Cookie实现跨域单点登录的原理

    对于构建分布式系统来说业务功能的物理部署会随着新业务模块的增加而增加或改变物理部署的位置.而每个用户都有统一的帐号作为我们登录系统时的一个认证.当新业务或子系统部署在不同的物理机上,我们去访问不同的业 ...

  8. Struts2 验证框架 validation.xml 常用的验证规则

    validation.xml 的命名规则和放置路径: 文件名:<ActionClassName>-validation.xml <ActionClassName>就是要验证的A ...

  9. ubuntu-apache如何解决跨域资源访问

    参考:http://blog.csdn.net/emily201314/article/details/52877277 步骤1 #打开apache的headers模块 sudo a2enmod he ...

  10. HTTP 协议缓存

    http 缓存分为客户端缓存和服务端缓存 1. 客户端缓存 客户端缓存指的是浏览器缓存, 浏览器缓存是最快的缓存, 因为它直接从本地获取(但有可能需要发送一个请求), 它的优势是可以减少网络流量, 加 ...