码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1328 (Radar Installation)

时间:2014-11-10 21:22:02      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   ar   os   sp   

Radar Installation

Time Limit:1000MS  Memory Limit:10000K Total Submit:2704 Accepted:564

Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d. We use  Cartesian coordinate system, defining the coasting is the x-axis. The sea side  is above x-axis, and the land side below. Given the position of each island in  the sea, and given the distance of the coverage of the radar installation, your  task is to write a program to find the minimal number of radar installations to  cover all the islands. Note that the position of an island is represented by its  x-y coordinates.

bubuko.com,布布扣 Figure A Sample Input of Radar Installations

 

 

Input The input consists of several test cases. The first line  of each case contains two integers n (1<=n<=1000) and d, where n is the  number of islands in the sea and d is the distance of coverage of the radar  installation. This is followed by n lines each containing two integers  representing the coordinate of the position of each island. Then a blank line  follows to separate the cases. The input is terminated by a line  containing pair of zeros 

Output For each test case output one line consisting of the test  case number followed by the minimal number of radar installations needed. "-1"  installation means no solution for that case.

Sample Input

3 2
1 2
-3 1
2 1
1 2
0 2
0 0

 

Sample Output

Case 1: 2
Case 2: 1

 

Source Beijing 2002

 

诈一看这个题目,或许就有了写想法.觉得不是无从下手,之前思路错了一次.之后看了网上的解题报告,才发现自己错了.

开始错的: 先把所有的点X点从大到小排一下. 再早左极限 再与s=2*(sqrt(d*d-y*y)).比较,错的原因就是如果下一个点,甚至下下个点的左极限在上个点的左极限之内的话,就会出错!

后来改了一点点:先把所有的左极限找出来,再来排左极限,当然 相应的s也要跟随排过去(这样就可以用数组结构体,只是当时用了快排了,就添加了一下,代码也就麻烦了一点.当然数据不多也是一个原因.) 废话不多说,直接贴代码!

#include <stdio.h>
#include <math.h>
void quick_sort(double b[],double a[],int l,int r)//这个快排就是来排左极限的。 
{
    if(l<r)
    {   int i,j ,k,t;
        double x,y;
        i=l;j=r;x=b[l],y=a[l];
        while(i<j)
        {
            while(i<j&&b[j]>=x)
            j--;
            if(i<j)
            {k=i;t=j;
            b[i++]=b[j];
            a[k++]=a[t];
            }
            while(i<j&&b[i]<x)
            i++;
            if(i<j)
            {k=i;t=j;
            b[j--]=b[i];
            a[t--]=a[k];
            }
        }
        b[i]=x;
        a[i]=y;
        quick_sort(b,a,l,i-1);
        quick_sort(b,a,i+1,r);
    }
}
int main()
{
    int n,i,j,z,sum,temp=1;
    double d,t,p,q;
    double a[1500],b[1500],k[1500],s[1500];
    while(~scanf("%d %lf",&n,&d))
    {   z=0;
    sum=0;
        if(n==0&&d==0)
        break;
        for(i=1;i<=n;i++)
        {
        scanf("%lf%lf",&a[i],&b[i]);
        if(b[i]>d)
            {
                z=1;
            }
        }
        for(i=1;i<=n;i++)
        {
            k[i]=a[i]-sqrt(d*d-b[i]*b[i]);
            s[i]=sqrt(d*d-b[i]*b[i]);
            s[i]=2*s[i];
        }        
        quick_sort(k,s,1,n);
       
       for(i=n;i>=1;i--)//这个循环就是来和s比较的。 
             {  p=k[i];
                for(j=i-1;j>0;j--)
                  {
                      if(p-k[j]>s[j])//如果两个左极限之间的距离比s还大的话,说明要加一个雷达! 
                            {
                                  sum++;
                                  break;//确定要了之后就从下一个开始判定了.直至最后一个i. 
                            }
                       i--;
                 }
                 if(i==1)//判断最后一次还要不要再装一个雷达. 
                   sum++;//雷达数. 
             }

        if(z==1)
        printf("Case %d: -1\n",temp);
        else
        printf("Case %d: %d\n",temp,sum);
        temp++;
    }
    return 0;
}

 

POJ 1328 (Radar Installation)

标签:des   style   blog   http   io   color   ar   os   sp   

原文地址:http://www.cnblogs.com/ikids/p/4087925.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!