Bzoj 3523 [Poi2014]Bricks

原题网址:http://www.lydsy.com/JudgeOnline/problem.php?id=3523
很显然的贪心,每次找不和前面元素重复的最多的,尾部颜色在数量相同时优先级更高。WA了好多发因为姿势丑,如果堆顶元素和前面元素一样只要先把堆顶拿出来就好了。。

type
  rec=record
        c,cnt:longint;
      end;  
const
  MAXN=1000050;
var
  heap:array[0..MAXN] of rec;
  a,c,cp:array[0..MAXN] of longint;
  n,m,st,nd,i,cnt:longint;
  t:rec;
procedure swp(var a,b:longint);
  var t:longint;
  begin t:=a;a:=b;b:=t; end;
procedure swap(a,b:longint);
  begin
    swp(heap[a].c,heap[b].c);
    swp(heap[a].cnt,heap[b].cnt);
  end;
operator >(a,b:rec)c:boolean;
  begin exit((a.cnt>b.cnt)or(a.cnt=b.cnt)and(a.c=nd));end;  
procedure heapup(x:longint);
  begin
    while ((x>1)and(heap[x]>heap[x>>1])) do
      begin
        swap(x,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
        e:=x<<1+ord((x<<1+1<=cnt)and(heap[x<<1+1].cnt>heap[x<<1].cnt));
        swap(x,e);
        x:=e;
      end;
  end;
procedure push(x:rec);
  begin
    inc(cnt);
    heap[cnt]:=x;
    heapup(cnt);
  end;
procedure poop;
  begin
    heap[1]:=heap[cnt];
    dec(cnt);
    heapdown(1);
  end;
procedure no_solution;
  begin
    writeln(0);
    halt;
  end;
begin
  read(n,st,nd);m:=0;
  for i:=1 to n do read(a[i]);
  cp:=a;
  for i:=1 to n do inc(m,a[i]);
  dec(a[st]);if (a[st]<0) then no_solution;
  dec(a[nd]);if (a[nd]<0) then no_solution;
  for i:=1 to n do
    if (a[i]>0) then
      begin
        t.c:=i;
        t.cnt:=a[i];
        push(t);
      end;
  c[1]:=st;c[m]:=nd;
  for i:=2 to m-1 do
    if (heap[1].c<>c[i-1])
      then
        begin
          c[i]:=heap[1].c;
          dec(heap[1].cnt);
          if (heap[1].cnt=0)
            then poop
            else heapdown(1);
        end
      else
        begin
          if (cnt=1) then no_solution;
          t:=heap[1];poop;
          c[i]:=heap[1].c;
          dec(heap[1].cnt);
          if (heap[1].cnt=0)
            then poop
            else heapdown(1);
          push(t);  
        end;
  if (c[m]=c[m-1]) then no_solution;  
  for i:=1 to m-1 do
    write(c[i],' ');
  writeln(c[m]);
end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值