[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_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的更多相关文章
- [UCSD白板题] Maximum Pairwise Product
Problem Description Task.Given a sequence of non-negative integers \(a_0, ..., a_{n-1}\),find the ma ...
- [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白板题] 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 ...
- [UCSD白板题] Changing Money
Problem Introduction In this problem,you will design an algorithm for changing money optimally. Prob ...
- [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白板题] 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白板题] Points and Segments
Problem Introduction The goal in this problem is given a set of segments on a line and a set of poin ...
随机推荐
- centos rar安装
wget http://www.rarsoft.com/rar/rarlinux-x64-5.1.1.tar.gz tar -zxvf rarlinux-x64-5.1.1.tar.gz # cd r ...
- Autofac 及 Autofac.WebApi 与MVC版本兼容问题
环境:vs2013 下载 Autofac 时,会自动把 Microsoft.AspNet.Mvc 更新到 5.2.3.原来是 5.0.0.0 再使用 Autofac.WebApi , 出现以下问题: ...
- IE6/IE7/IE8兼容H5标签
可以使用html5shiv(html5shiv主要解决HTML5提出的新元素不被IE6-8识别,这些新元素不能作为父节点包裹子元素,并且不能应用CSS样式)来解决 <!--[if lt IE 9 ...
- mybatis组合模糊+分页查询
//组合模糊查询就是这么简单 <select id="findAllJiemu" parameterType="java.util.Map" result ...
- [整理]Matlab之中心平滑滤波
滑动平均(moving average):在地球物理异常图上,选定某一尺寸的窗口,将窗口内的所有异常值做算术平均,将平均值作为窗口中心点的异常值.按点距或线距移动窗口,重复此平均方法,直到对整幅图完成 ...
- ABP mapto 映射
obj1.MapTo(obj2); obj1=>obj2: 在obj1实体里添加映射 [AutoMap(typeof(obj2))] public class obj1 { }
- Noip2016提高组 组合数问题problem
Day2 T1 题目大意 告诉你组合数公式,其中n!=1*2*3*4*5*...*n:意思是从n个物体取出m个物体的方案数 现给定n.m.k,问在所有i(1<=i<=n),所有j(1< ...
- NSDate和NSString相互转换
一.NSDate转NSString //获取系统当前时间 NSDate *currentDate = [NSDate date]; //用于格式化NSDate对象 NSDateFormatter *d ...
- python学习之——pip的安装与使用
安装步骤: 前提:已经安装python 1.下载安装包并解压: easy_install 地址:https://pypi.python.org/pypi/setuptools#downloads pi ...
- c++父类和子类转化致命的代码错误
最近在工作中,出现了严重的代码错误,对象的基类和子类的继承,代码大致如下: class A { }; class B : public A { } void main() { A* a;(用于子类对象 ...