bzoj1029: [JSOI2007]建筑抢修

来源:互联网 发布:淘宝网空竹 编辑:程序博客网 时间:2024/05/02 09:48

传送门

贪心显然。

1.按照最后时限排序,暴力插入。

但是显然会错。

5

10 10

10 20

2 21

2 21

2 21

答案是2,但是显然最优的是4

于是考虑改进。

2.在无法修理时我们可以放弃花费时间最长的工作。

明显花费时间最长的工作时间大于当前工作是修改更优。

我们可以用一个大根堆实现。

然后就通过了。

我才不会告诉你我wa了5次

var  a,b,c:array [0..200005] of int64;  n,i,ans,top:longint;  tt:int64;procedure kp(l,r:longint);  var i,j:longint;      m,t:int64;  begin    i:=l; j:=r; m:=b[(l+r) div 2];    while (i<=j) do begin      while (b[i]<m) do inc(i);      while (b[j]>m) do dec(j);      if (i<=j) then begin        t:=a[i]; a[i]:=a[j]; a[j]:=t;        t:=b[i]; b[i]:=b[j]; b[j]:=t;        inc(i); dec(j);    end; end;    if (l<j) then kp(l,j);    if (i<r) then kp(i,r);  end;procedure ins(x:int64);  var s:longint; t:int64;  begin    inc(top); c[top]:=x; s:=top;    while (s<>1) and (c[s]>c[s div 2]) do      begin t:=c[s]; c[s]:=c[s div 2]; c[s div 2]:=t; s:=s div 2; end;  end;procedure del;  var s,p:longint; t:int64;  begin    c[1]:=c[top]; dec(top); s:=1;    while (s*2<=top) do begin      if (s*2+1>top) or (c[s*2]>c[s*2+1]) then p:=s*2 else p:=s*2+1;      if (c[s]<c[p]) then begin t:=c[s]; c[s]:=c[p]; c[p]:=t; s:=p; end        else break;    end;  end;begin  read(n);  for i:=1 to n do read(a[i],b[i]);  kp(1,n);  tt:=0; ans:=0; top:=0;  for i:=1 to n do    if (tt+a[i]<=b[i]) then begin inc(ans); tt:=tt+a[i]; ins(a[i]); end      else if (a[i]<c[1]) and (tt-c[1]+a[i]<=b[i]) and (top<>0) then begin tt:=tt-c[1]+a[i]; del; ins(a[i]); end;  write(ans);end.


2 0
原创粉丝点击