现在的位置: 首页 > 综合 > 正文

poj1065

2018年01月15日 ⁄ 综合 ⁄ 共 976字 ⁄ 字号 评论关闭

【题意】

有n根木棒,已知他们的质量和长度,需对他们进行加工,开机1分钟,加工一根一分钟,

且每次开机后,加工的第i+1根必须比第i根的长度和质量都要大才行,问最短加工时间。

【输入】

第一行T表示T组数据。

每组数据第一行一个N,表示几根木棒。

然后N个二元组,表示每根木棒的长度与质量。

【输出】

对于每组数据,输出最短加工时间。

贪心。

排下序,每次尽可能加工多。

program poj1065;
type
  stick=record
          w,l:longint;
        end;
var
  t,n,i,j,k,q,ans,total,now:longint;
  dl:array [0..5001] of stick;
  yes:array [0..5001] of boolean;
procedure swap (var a,b:stick);
var
  i:stick;
begin
  i:=a;
  a:=b;
  b:=i;
end;
procedure qsort (s,e:longint);
var
  i,j:longint;
  k:stick;
begin
  if s>=e then exit;
  i:=s;
  j:=e;
  k:=dl[(s+e) div 2];
  while i<=j do
    begin
      while (dl[i].w<k.w)or((dl[i].w=k.w)and(dl[i].l<k.l)) do inc(i);
      while (dl[j].w>k.w)or((dl[j].w=k.w)and(dl[j].l>k.l)) do dec(j);
      if i>j then break;
      swap(dl[i],dl[j]);
      inc(i);
      dec(j);
    end;
  qsort(s,j);
  qsort(i,e);
end;
begin
  read(t);
  for q:=1 to t do
    begin
      fillchar(yes,sizeof(yes),false);
      ans:=0;
      read(n);
      for i:=1 to n do
        read(dl[i].w,dl[i].l);
      qsort(1,n);
      total:=0;
      now:=0;
      while total<n do
        begin
          inc(ans);
          for i:=1 to n do
            if (not yes[i])and(dl[i].l>=now) then
              begin
                yes[i]:=true;
                inc(total);
                now:=dl[i].l;
              end;
          now:=0;
        end;
      writeln(ans);
    end;
end.
【上篇】
【下篇】

抱歉!评论已关闭.