会员专享
请先
登录,登录后可使用今日免费解锁;
开通会员,或
购买
该题目所属题库
,可解锁完整内容。
思路:贪心
step1:题目简化
本质就是给定两个长度相等为n的数组a,b , 求一种排列顺序使得对应位置两两相减的绝对值的和最小。
step2:贪心
我们不妨先规定a是升序的,那么任务就是只需要给b一个顺序,使得两两绝对值的和最小。
P1664.2024.1.30-后端开发机试-第一题
An AWS client has brought servers and databases from data centers in different parts ofthe world for their application. For simplicity, let's assume all the servers and data centersare located on a 1-dimensional line.
You have been given the task of optimizing the network connection. Each data centermust be connected to a server. The positions of n data centers and n servers are given inthe form of arrays. Any particular data center, center[i], can deliver to any particularserver destination, destination/j, The lag is defined distance between a data center atlocation xand a server destination at location y is |x-y|, i.e., the absolute differencebetween xand y. Determine the minimum lag to establish the entire network.
Example