Bzoj1572 [Usaco2009 Open]工作安排Job

4 篇文章 0 订阅
1 篇文章 0 订阅

原题网址:http://www.lydsy.com/JudgeOnline/problem.php?id=1572
反过来做,每个任务都有一个开始时间,到了开始时间就压进堆里,每秒弹出一个价值最高的。

var
  d,v,heap:array[0..100050] of longint;
  n,i,time,cnt,p:longint;
  ans:int64;
procedure swap(var a,b:longint);
  var t:longint;
  begin t:=a;a:=b;b:=t;end;
procedure sort(l,r:longint);
  var
    i,j,e:longint;
  begin
    i:=l;j:=r;e:=d[(l+r)>>1];
    repeat
      while d[i]>e do inc(i);
      while e>d[j] do dec(j);
      if not (i>j) then
        begin
          swap(d[i],d[j]);
          swap(v[i],v[j]);
          inc(i);dec(j);
        end;
    until i>j;
    if l<j then sort(l,j);
    if i<r then sort(i,r);
  end;
procedure heapup(x:longint);
  begin
    while ((x>1)and(heap[x]>heap[x>>1])) do
      begin
        swap(heap[x],heap[x>>1]);
        x:=x>>1;
      end;
  end;
procedure heapdown(x:longint);
  var
    e:longint;
  begin
    while ((x<<1<=cnt)and(heap[x<<1]>heap[x])or(x<<1+1<=cnt)and(heap[x<<1+1]>heap[x])) do
      begin
        if ((x<<1+1<=cnt)and(heap[x<<1+1]>heap[x<<1]))
          then e:=x<<1+1
          else e:=x<<1;
        swap(heap[x],heap[e]);
        x:=e;
      end;
  end;
procedure push(x:longint);
  begin
    inc(cnt);
    heap[cnt]:=x;
    heapup(cnt);
  end;
procedure poop;
  begin
    heap[1]:=heap[cnt];
    dec(cnt);
    heapdown(1);
  end;
begin
  read(n);
  for i:=1 to n do read(d[i],v[i]);
  sort(1,n);
  time:=1000000000;
  cnt:=0;p:=0;ans:=0;
  repeat
    while ((p+1<=n)and(d[p+1]=time)) do
      begin
        inc(p);
        push(v[p]);
      end;
    if (cnt>0) then
      begin
        inc(ans,heap[1]);
        poop;
      end;
    if (cnt=0)and(p<n)
      then time:=d[p+1]
      else dec(time);
  until (p=n)and(cnt=0)or(time=0);
  writeln(ans);
end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值