Thứ Ba, 3 tháng 5, 2022

XEPDOAN PASCAL

type doan = record
    x,y:longint;
end;

var
    i,n:longint;
    d:array[1..100005] of doan;

procedure nhap;
var i,k:longint;
begin
    readln(n);
    for i:=1 to n do
    begin
        readln(d[i].x,k);
        d[i].y:=d[i].x+k;
    end;
end;

function ss(p,q:doan):boolean;
begin
    if (p.x<q.x) then exit(true)
    else exit(false);
end;


procedure quicksort(l,r:longint);
var i,j:longint;
    p,t:doan;
begin
    i:=l;j:=r;
    p:=d[(l+r) div 2];
     repeat
           while ss(d[i],p)=true do inc(i);
           while ss(p,d[j])=true do dec(j);
           if i<=j then
           begin
                t:=d[i];d[i]:=d[j];d[j]:=t;
                inc(i);dec(j);
           end;
     until i>j;
     if l<j then quicksort(l,j);
     if i<r then quicksort(i,r);
end;

procedure giai;
var i:longint;
    a,b,res:int64;
begin
    quicksort(1,n);
   // for i:=1 to n do
    //    writeln(d[i].x,' ',d[i].y);
    res:=0;
    a:=d[1].x; b:=d[1].y;
    for i:=2 to n do
        if (d[i].x <= b) then
        begin
            if d[i].y>b then  b:=d[i].y;
        end
        else
        begin
            res:=res+(b-a);
            a:=d[i].x;b:=d[i].y;
        end;
    res:=res+(b-a);
    write(res);
end;

begin
    assign(input,'xepdoan.inp');reset(input);
    assign(output,'xepdoan.out');rewrite(output);
    nhap;
    giai;
    close(input);
    close(output);

end.

Không có nhận xét nào:

Đăng nhận xét

Lưu ý: Chỉ thành viên của blog này mới được đăng nhận xét.