Thứ Ba, 3 tháng 5, 2022

SHARE948 PASCAL

type pt=record
        cs,gt:longint;
end;
var     a:array[0..100000] of pt;
        kq:array[0..1000005] of int64;
    n,m,tb,s:int64;

procedure nhap;
var i:longint;
begin
    readln(n,m);
    for i:=1 to n do
    begin
        read(a[i].gt);
        a[i].cs:=i;
        s:=s+a[i].gt;
    end;
end;

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

procedure xuly;
var i:longint;
begin
    sort(1,n);
    if s < m then
    begin
        writeln(-1);
        exit;
    end;

    for i:=1 to n do
    begin
        tb:=m div (n+1-i);
        if a[i].gt <= tb then
            kq[a[i].cs]:=a[i].gt
        else
            kq[a[i].cs]:=tb;
        m:=m-kq[a[i].cs];
    end;
    for i:=1 to n do write(kq[i],' ');
end;

begin
    assign(input,'SHARE948.inp');reset(input);
    assign(output,'SHARE948.out');rewrite(output);
    nhap;
    xuly;
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.