Thứ Ba, 3 tháng 5, 2022

CANDY1051 PAS

const fi='candy1051.inp';fo='candy1051.out';
var a,b:array[0..round(1e6)]of int64;
    n,m,res:longint;

procedure nhap;
var i:longint;
begin
        readln(n,m);
        for i:=1 to n do read(A[i]);
end;

function check(x:longint):boolean;
var     i:longint;
        res:int64;
begin
        res:=0;
        for i:=1 to n do
                if a[i]<x then res:=res+x-a[i];
        if res<=m then exit(true)
        else exit(false);
end;

function bs(l,r:int64):int64;
var mid,res:int64;
begin
        res:=-1;
        while l<=r do
        begin
                mid:=(l+r) div 2;
                if check(mid)=true then
                begin
                        l:=mid+1;
                        res:=mid;
                end
                else r:=mid-1;
        end;
       exit(res);
end;
procedure xuli;
var i,j:longint;k:int64;
begin
        for i:=1 to n do b[i]:=a[i];
        //qs(1,n);
        k:=bs(0,trunc(1e9)*2);
        for i:=1 to n do
                if a[i]<k then
                begin
                        m:=m-(k-a[i]);
                        a[i]:=k;
                end;
        for i:=1 to n do
        begin
                if m>0 then
                        if a[i]=k then
                        begin
                                a[i]:=k+1;
                                dec(m);
                        end;
        end;

       { end;
            for i:=n downto 1 do
                    if b[i]<res then
                        begin
                            m:=m-(res-1-b[i]);
                            b[i]:=res-1;
                        end;
            for i:=1 to n do
                if m>0 then
                    if b[i]<res then
                        begin
                            b[i]:=res;
                            dec(m);
                        end;}
       for i:=1 to n do write(a[i],' ');
end;

begin
    assign(input,fi);reset(input);
    assign(output,fo);rewrite(output);
    nhap;
    xuli;
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.