[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 ...
随机推荐
- 获取图片工具类:BitmapUtil
package com.example.administrator.filemanager.utils;import android.content.Context;import android.gr ...
- [Docker] docker 基础学习笔记4(共6篇)
离线安装nginx apache 如何启动war包 linux 离线升级内核 nginx和Apache的使用 nginx 的负载均衡配置 是如此的简单,比weblogic的要简单100 ...
- scala 学习之: list.fill 用法
题目描述: Decode a run-length encoded list. Given a run-length code list generated as specified in probl ...
- Postgresql死锁处理
今天遇到Postgresql的一个问题,部分表记录的update一直无效报错,初步判断为锁表,赶紧进行解决. 1. 查询死锁进程列表 select * from pg_stat_activity wh ...
- 转-servlet 获取 post body 体用流读取为空的问题
目前基于rest风格的很多API开始使用通过body data来传输来代替之前的key-value传输方式.在Java servlet或者springmvc中可以通过如下代码来获取并图片通过流方式传输 ...
- iOS 上架被拒原因保存
一.后台一直在获取用户的定位,需要给用户电池消耗提示 Your app uses the Location Background mode but does not include the requi ...
- ZT 北大青鸟APTECH(南京泰思特)
北大青鸟APTECH(南京泰思特)授权培训中心诚招 高级软件测试/开发培训讲师 Posted on 2008-01-14 11:38 追求卓越 阅读(590) 评论(7) 编辑 收藏 北京阿博泰克北大 ...
- ItextDemo<二>
Nested tables TableTemplate.java /** * Example written by Bruno Lowagie in answer to the following q ...
- adb devices 不识别设备(或者偶尔识别设备) -破解
问题:当在CMD中输入adb devices时,没有设备信息显示,居然显示下面的信息 问题是有时候可以识别,有时候不可以识别.当被别人连接后,自己在连接是好的,过了一会以后又不好了
- Nodejs Express下引入本地文件的方法
Express的结构如下: |---node_modules------用于安装本地模块. |---public------------用于存放用户可以下载到的文件,比如图片.脚本文件.样式表 ...