POJ 1328(贪心)

贪心,区间排序


如果区间包含 略过,否则答案+1;


Program P1328;
var
   n,d,i,j,k,ans:longint;
   x:double;
   tag:boolean;
   map:array[1..1000,1..2] of double;
procedure qsort(l,r:longint);
var
   i,j:longint;
   m,p:double;
begin
   i:=l;
   j:=r;
   m:=map[(l+r) div 2,1];
   repeat
      while map[i,1]<m do inc(i);
      while map[j,1]>m do dec(j);
      if i<=j then
      begin
         p:=map[i,1];
         map[i,1]:=map[j,1];
         map[j,1]:=p;
         p:=map[i,2];
         map[i,2]:=map[j,2];
         map[j,2]:=p;
         inc(i);
         dec(j);
      end;
   until i>j;
   if l<j then qsort(l,j);
   if i<r then qsort(i,r);
end;
begin
   i:=1;
{   assign(input,'P1328.in');
   assign(output,'P1328.out');
   reset(input);
   rewrite(output);   }
   while not seekeof do
   begin
      tag:=true;
      read(n,d);
      if d<=0 then tag:=false;
      if (n=0) and (d=0) then break;
      j:=1;
      for k:=1 to n do
      begin
         read(map[j,1],map[j,2]);
         if map[j,2]<0  then continue;
         if d<map[j,2] then continue;
         x:=sqrt(d*d-map[j,2]*map[j,2]);
         map[j,2]:=map[j,1]+x;
         map[j,1]:=map[j,1]-x;
         inc(j);
      end;
      dec(j);
      if tag and (j=n) then
      begin
         qsort(1,j);
         ans:=1;
         x:=map[1,2];
         for k:=2 to j do
         begin
            if (map[k,2]<x) then
            begin
               x:=map[k,2];
            end
            else if (x<map[k,1]) then
            begin
               inc(ans);
               x:=map[k,2];
            end;

         end;

         writeln('Case ',i,': ',ans);
      end
      else writeln('Case ',i,': -1');
      inc(i);
   end;
 {  close(input);
   close(output);   }
end.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值