First, let's make sure that the longest Manhattan distance is only possible for x1+y2-(X2+y2) and x1-y1-(x2-y2) so we only need to maintain four values,
Represents Max (X+y), respectively; Max (x-y); Min (x+y); Min (x-y);
So the answer is Max (max (x+y)-min (x+y), Max (x-y)-min (x-y).
1 Constmaxn=3000000;2 varMaxadd,minadd,maxdec,mindec,i,a,b,n:longint;3 function Max (a,b:longint): Longint;4 begin5 ifA>B then exit (a)6 Elseexit (b);7 end;8 function min (a,b:longint): Longint;9 beginTen ifa<B then exit (a) One Elseexit (b); A end; - begin -MINADD:=MAXN; mindec:=MAXN; the READLN (n); - fori:=1to n Do - begin - Readln (A, b); +Maxadd:=max (maxadd,a+b); -Minadd:=min (minadd,a+b); +Maxdec:=max (maxdec,a-b); AMindec:=min (mindec,a-b); at end; -Writeln (Max (maxadd-minadd,maxdec-mindec)); -End.
View Code
(Reproduced Please specify Source: http://www.cnblogs.com/Kalenda/)
P3382: [Usaco2004 Open]cave cows 3 The three cows in the cave