BWPOINTS - VOI 2011 Nối điểm đen trắng

Tác giả: ladpro98

Ngôn ngữ: Pascal

program bwpoints;
uses    math;
type    e=record
        v,t:longint;
        end;
const   fi='';
        maxN=222222;
        b=0;w=1;
var     a:array[1..maxN] of e;
        f:array[-1..maxN] of longint;
        n,res:longint;

procedure input;
var     inp:text;
        i:longint;
begin
        assign(inp,fi);
        reset(inp);
        readln(inp,n);
        for i:=1 to n do
        begin
                read(inp,a[i].v);
                a[i].t:=b;
        end;
        for i:=n+1 to 2*n do
        begin
                read(inp,a[i].v);
                a[i].t:=w
        end;
        close(inp);
end;

procedure swap(i,j:longint);
var     t:e;
begin
        t:=a[i];
        a[i]:=a[j];
        a[j]:=t;
end;

procedure sort(l,r:longint);
var     p,i,j:longint;
begin
        if l>=r then exit;
        p:=a[random(r-l+1)+l].v;
        i:=l;j:=r;
        repeat
                while a[i].v<p do inc(i);
                while a[j].v>p do dec(j);
                if i<=j then
                begin
                        if i<j then swap(i,j);
                        inc(i);dec(j);
                end;
        until i>j;
        sort(l,j);sort(i,r);
end;

procedure greedy;
var     i,black,white:longint;
begin
        i:=1;
        while i<2*n do
        begin
                if a[i].t<>a[i+1].t then
                begin
                        inc(res);
                        inc(i,2);
                end
                else
                inc(i);
        end;
end;

begin
        input;
        sort(1,n*2);
        greedy;
        write(res);
end.

Download