bzoj 3170 manhattan距离

首先将坐标系顺时针旋转45度,得到一个新的坐标系,这个坐标系

对应的坐标的manhattan距离就是原图中的距离,然后快排,利用前缀和

数组O(N)求所有的答案,然后找最小值就行了,总时间O(NlogN),今天

体力不足,在此不再赘述。。。

/**************************************************************
    Problem:
    User: BLADEVIL
    Language: Pascal
    Result: Accepted
    Time: ms
    Memory: kb
****************************************************************/
 
//By BLADEVIL
var
    n                       :int64;
    size                    :array[..,..] of int64;
    ans                     :array[..] of int64;
    sum                     :array[..] of int64;
    print                   :int64;
     
function min(a,b:int64):int64;
begin
    if a>b then min:=b else min:=a;
end;
     
procedure swap(var a,b:int64);
var
    c                       :int64;
begin
    c:=a; a:=b; b:=c;
end;
     
procedure init;
var
    i                       :longint;
    x, y                    :int64;
begin
    read(n);
    for i:= to n do
    begin
        read(x,y);
        size[,i]:=x+y;
        size[,i]:=y-x;
    end;
end;
 
procedure qs(low,high,s:int64);
var
    i, j, xx                :int64;
begin
    i:=low; j:=high; xx:=size[s,(i+j) div ];
    while i<j do
    begin
        while size[s,i]<xx do inc(i);
        while size[s,j]>xx do dec(j);
        if i<=j then
        begin
            swap(size[,i],size[,j]);
            swap(size[,i],size[,j]);
            swap(ans[i],ans[j]);
            inc(i); dec(j);
        end;
    end;
    if i<high then qs(i,high,s);
    if j>low then qs(low,j,s);
end;
 
procedure main;
var
    i                       :longint;
begin
    qs(,n,);
    for i:= to n do sum[i]:=int64(size[,i]);
    for i:= to n do sum[i]:=sum[i]+sum[i-];
    for i:= to n do
        ans[i]:=((i-)*size[,i]-sum[i-])+((sum[n]-sum[i])-(n-i)*size[,i]);
    qs(,n,);
    for i:= to n do sum[i]:=int64(size[,i]);
    for i:= to n do sum[i]:=sum[i]+sum[i-];
    for i:= to n do
        ans[i]:=ans[i]+((i-)*size[,i]-sum[i-])+((sum[n]-sum[i])-(n-i)*size[,i]);
    print:=maxlongint*maxlongint;
    for i:= to n do print:=min(print,ans[i]);
    print:=print div ;
    writeln(print);
end;
 
begin
    init;
    main;
end.
上一篇:手动向Maven本地仓库添加ORACLE ojdbc6jar包


下一篇:Android 完美退出 App (Exit)