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.