2018北大暑校acm算法训练课程 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
来源
Beijing 2002

老熟悉了这个题,和这个题有缘的不行,当初贪心思想有问题,所以一直出错
贼有缘

正确贪心思路:
  对每个岛屿可以算出覆盖它的雷达必须在x轴的区间
  将所有区间按照起点从小到大排序
  依次考察每个区间的起点,看要不要放雷达,复杂度O(n^2)

代码如下:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
#define eps 1e-7
//#define Kongxiangzhouye
#define _for(i,a,b) for( int i=(a); i<(b); ++i)
#define _rep(i,a,b) for( int i=(a); i<=(b); ++i)
int readint(){int x;scanf("%d",&x);return x;}

int x[1010];
int y[1010];
struct Line{
    double l,r;
}line[1010];

struct cmpFunctor{
    inline bool operator ()(const Line& t1,const Line& t2){
        return t1.r<t2.r;
    }
};

int main(){
    ios::sync_with_stdio(false);
    #ifdef Kongxiangzhouye
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    int n,d;
    int cas=1;
    while(scanf("%d%d",&n,&d)!=EOF&&n!=0&d!=0){
        _for(i,0,n){
            x[i]=readint();
            y[i]=readint();
        }
        int answer=1;
        if(d<=0||n<=0)
            answer=-1;
        else{
            _for(i,0,n){
                double dis=d*d*1.0-y[i]*y[i]*1.0;
                if(dis<0){
                    answer=-1;
                    break;
                }
                line[i].l=x[i]-sqrt(dis);
                line[i].r=x[i]+sqrt(dis);
            }
            if(answer!=-1){
                sort(line,line+n,cmpFunctor());
                int t=0;
                _for(i,0,n){
                    if(line[i].l>line[t].r){
                        t=i;
                        answer++;
                    }
                }
            }
        }
        printf("Case %d: %d\n",cas++,answer);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38842456/article/details/81271222