JSOI建築搶修 (貪心+堆)

來源:互聯網
上載者:User

標籤:

先按照T2從小到大排序,然後進行貪心。

第i個任務能完成的條件是,sigma(T1[j])+T1[i]<=T2[i] ( j 為之前所選的任務)

如果這個任務不能完成,若max(T1[j]) >T1[i]) , 就將i替換為j , 這樣可以使所用任務時間減小。

用一個堆維護最大值即可。

Program XJOI2320;const maxn=150008;var a,b,f:array[0..maxn] of longint;    n,i:longint;    sum,num,ans:int64;    procedure sort(l,r: longint);      var         i,j,x,y: longint;      begin         i:=l;         j:=r;         x:=b[(l+r) div 2];         repeat           while b[i]<x do            inc(i);           while x<b[j] do            dec(j);           if not(i>j) then             begin                y:=a[i];                a[i]:=a[j];                a[j]:=y;                y:=b[i];                b[i]:=b[j];                b[j]:=y;                inc(i);                j:=j-1;             end;         until i>j;         if l<j then           sort(l,j);         if i<r then           sort(i,r);      end;procedure swap(a,b:longint);var temp:longint;begin    temp:=f[a]; f[a]:=f[b];f[b]:=temp;end;procedure insert(x:longint);var i:longint;begin    inc(num);    f[num]:=x;    i:=num;    while i>1 do    begin        if f[i]>f[i div 2] then swap(i,i div 2) else break;        i:=i div 2;    end;end;procedure delete(x:longint);var i:longint;begin    f[1]:=f[num];    dec(num);    i:=2;    while i<=num do    begin        if (i+1<=num) and (f[i+1]>f[i]) then inc(i);        if f[i]>f[i div 2] then swap(i,i div 2) else break;        i:=i*2;    end;end;begin    readln(n);    for i:=1 to n do readln(a[i],b[i]);    sort(1,n);    sum:=0; num:=0; ans:=0;    for i:=1 to n do        if sum+a[i]<=b[i] then        begin            inc(ans);            sum:=sum+a[i];            insert(a[i]);        end else        if a[i]<f[1] then        begin            sum:=sum-f[1];            delete(1);            insert(a[i]);            sum:=sum+a[i];        end;        writeln(ans);end.

 

JSOI建築搶修 (貪心+堆)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.