bzoj1029: [JSOI2007]建筑抢修

传送门

贪心显然。

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值