码迷,mamicode.com
首页 > 编程语言 > 详细

数组差的绝对值

时间:2014-10-31 20:43:45      阅读:353      评论:0      收藏:0      [点我收藏+]

标签:io   os   ar   sp   on   cti   bs   ad   size   

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|



数组差的绝对值

标签:io   os   ar   sp   on   cti   bs   ad   size   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!