首页 > 编程语言 > 详细

数组差的绝对值

时间:2014-10-31 20:41:09      阅读:283      评论:0      收藏:0      [点我收藏+]

Given two arrays A, B with size n, you could justify the positions of number in array B to maximum the sum[abs(ai-bi)], i from 0 to n or minimize the sum[abs(ai-bi)].

---------------------------------------------------------------------

Maximum: sort all numbers in A and B, Use the last n numbers to minus the first n numbers. 


Minimum: sort A and B separately. Use proof by contradiction:

Image Ai < Aj and Bi > Bj, discuss the6 situations:

Ai < Aj < Bi < Bj

Ai < Bi< Aj < Bj

Ai < Bi < Bj< Aj

Bi < Ai < Aj < Bj

Bi < Ai < Bj < Aj

Bi < Bj < Ai < Aj

In all of the situations , |Ai-Bi|+|Aj-Bj| <= |Ai-Bj|+|Aj-Bi|



数组差的绝对值

原文:http://blog.csdn.net/taoqick/article/details/40659639

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!