OpenJudge 1328:Radar Installation

1328:Radar Installation

总时间限制: 1000ms 内存限制: 65536kB

描述

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.

Figure A Sample Input of Radar Installations

输入

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

输出

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.

样例输入

3 2
1 2
-3 1
2 1

1 2
0 2

0 0

样例输出

Case 1: 2
Case 2: 1

思路:(copy大佬的思路)

如果可以找到一个雷达同时覆盖多个区间,那么把这多个区间按起点坐标从小到大排序 ,则最后一个区间(起点最靠右的)k的起点,就能覆盖所有区间
证明:如果它不能覆盖某个区间x,那么它必然位于 1) x起点的左边,或者2)x终点的右边。情况1) 和 k 的起点是最靠右的矛盾情况2) 如果发生,则不可能找到一个点同时覆盖x和k,也和前提矛盾有了这个结论,就可以只挑区间的起点来放置雷达了。

代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<ctime>
#include<iostream>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<string>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define ll long long
#define dd double
#define ull unsigned long long
#define mes(x,y) memset(x,y,sizeof(x))
#define si(i) scanf("%d",&i)
#define ss(i) scanf("%s",(i))
#define sd(i) scanf("%lf",&i)
#define INF 0x3f3f3f3f
#define eps 1e-16
#define PI acos(-1)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;

const int maxn=2e5+9;
const int mod=1e9+7;

ll gar(ll a,ll b){//最大公约数
    return b==0?a:gar(b,a%b);
}
struct node{
    double x,y;
    bool operator < (const node &n)const{
        return y<n.y;
    }
}a[1030];
int main(){
    FAST_IO;
    ll n,i,j,flag=1,lv=1;double m;
    while(cin>>n>>m&&(m!=0||n!=0)){
        flag=1;
        double q,p,v=m*m;
        for(i=0;i<n;i++){
            cin>>q>>p;
            a[i].x=q-sqrt(v-(double)p*p);
            a[i].y=q+sqrt(v-(double)p*p);
            if(p>m||m<0)flag=0;
        }
        if (flag==0){
            cout<<"Case "<<lv<<": -1"<<endl;lv++;continue;
        }
        sort(a,a+n);
        double begin=a[0].y;ll sum=1;
        for(i=1;i<n;i++){
            if(begin<a[i].x){
                sum++;
                begin=a[i].y;
            }
        }
        cout<<"Case "<<lv<<": "<<sum<<endl;lv++;
    }
}
发布了148 篇原创文章 · 获赞 7 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_44417851/article/details/92176130