[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 ...
随机推荐
- frame busting
[frame busting] 参考:http://book.51cto.com/art/201204/330076.htm
- 关于prototype和__proto__ 以及 constructor的文字总结
//函数的construct 指向了 function Function(){} undefined //实例化的对象 constructor 指向 函数本身 undefined //每个函数都有pr ...
- Spring+springmvc+Mybatis整合案例 annotation版(myeclipse)详细版
Spring+springmvc+Mybatis整合案例 Version:annotation版 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version=&qu ...
- 手动安装m4, autoconf, automake, libtool
转自http://ruby-china.org/topics/2434 系列文章原载于自己的博客,TOPI.CO (http://topi.co) ,某天不小心就push错啦,懒得从头再来,上传到Ru ...
- android之handle
Android中异步消息处理主要由四个部分组成,Message.handler.messageQueue和looper. 1.message message是线程之间传递的消息,他可以在内部携带少量的 ...
- MyEclipse配置Tomcat开发JavaWeb程序JSP以及Servlet
1.安装准备 1).下载安装MyEclipse2014,这已经是最新版本. 2).下载Tomcat 官网:http://tomcat.apache.org/ 我们选择8.0: http://tomca ...
- (转载)python2+selenium自动化测试系列(二)
16.Selenium2+python自动化16-alert\confirm\prompt 17.Selenium2+python自动化17-JS处理滚动条 18.Selenium2+python自动 ...
- 关于BaseExpandableListAdapter
首先要明确,可折叠列表在每个项是包含子项的,那么肯定会用到集合嵌套!下面是封装的两个实体类: package com.yx.pojo;public class Chid { privat ...
- hibernateTemplate中常用查询方法的使用(原文地址: http://dongruan00.iteye.com/blog/1772311)
一.find(String queryString); 示例:this.getHibernateTemplate().find("from bean.User"); 返回所有Use ...
- sublime插件 TortioseSVN
TortioseSVN 可以安装在sublime中,实现svn文件的增加.删除.更新.提交等功能(TortioseSVN用在window系统中,linux安装svn) 安装: 首先在sublime中搜 ...