CSU-ACM2017暑期训练7-模拟&&贪心A

来源:互联网 发布:永久关闭防火墙linux 编辑:程序博客网 时间:2024/05/22 17:05

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

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

写这一题题解是因为想要说明一下贪心的思路,一开始本来以为贪心选择是,从左往右选择每个点,然后对当前最左的,且未覆盖的点的最右边可覆盖到它的地方放雷达,但是这种选择是错误的,我们假定这样的贪心选择是正确的,首先我们注意到,一个雷达要覆盖到一个点,那么这个雷达必定满足这样的条件,假定该点坐标为(xi,yi),圆的半径为R,那么能覆盖到它的最左边的雷达的坐标X_min_left = xi - sqrt(R^2 - yi^2),最右边的X_max_right = xi + sqrt(R^2 - yi^2),一个雷达要想覆盖某一点,则必定其坐标
X 在 [x_min_left,x_max_right]中,为什么说之前的贪心选择是错误,假设它是正确的,那么这样的贪心选择得到的雷达,必定是覆盖了之前必须覆盖的(即不得不再这个地方放一个雷达),那么我们可以尝试再它的前面放雷达,如果在它前面放雷达反而产生了更优解,那么这种贪心选择是错误的,在何种情况下,再前面放雷达反而会产生更优解呢,显然,这样的贪心选择每次都选最左的点来放最右的雷达,并且将右边可覆盖的点去除(即已覆盖),那么我们只需要构造一种情形,即把雷达放在前面反而可以覆盖到更多的点的情形,可以简单设想一下对于某个点放雷达的区间,显然当其纵坐标越来越大时,此时产生的可放雷达的区间会越来越小,那么我们就可以把某个右边的点的纵坐标放大,就可以使得其区间落在最左边点的区间内,这个时候这个贪心选择的雷达显然无法达到最优解。可以看一下图:这里写图片描述
那么经过这样的分析,我们很容易就可以得到贪心选择,即选择当前必须放的雷达(实际上就是最右的雷达),然后将覆盖的点去除,再去放其它最右的雷达,实际上就是一个简单的区间覆盖问题,之所以写了这么多,是想把思路写清楚。
给出代码:
另外感觉自己错了好多地方,n * n < y * y,原本自己是这样写的,后来换成long long 先存取,再比较,没想明白为何会错,
因为一改成n < y,就对了,int 类型的最大为2 ^31 - 1,那么怎么long long 还会爆呢,这里求个解释。

#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <vector>#include <string> #include <algorithm>#include <list>#include <set>#include <cmath>#include <queue> using namespace std;struct island{    double l;    double r;};bool cmp(island a,island b){    return a.l < b.l;}int main(){    int n,t = 0,d;    island a[2050];    while(scanf("%d%d",&n,&d) != EOF){        if(n == 0 && d == 0)            break;        t++;        int x,y,yes = 1;        for(int i = 0;i < n;i++){            scanf("%d%d",&x,&y);            if(yes == 0)                continue;            if(d < y){                yes = 0;            }            else{                a[i].l = (double)x - sqrt((double)d * d - y * y);                a[i].r = (double)x + sqrt((double)d * d - y * y);            }        }        if(yes == 0){            printf("Case %d: -1\n",t);            continue;        }        else{            sort(a,a + n,cmp);            double cur = a[0].r;            int c = 1;            for(int i = 1;i < n;i++){                    if(cur > a[i].r){                        cur = a[i].r;                    }                    else if(cur < a[i].l){                        cur = a[i].r;                        c++;                    }            }            printf("Case %d: %d\n",t,c);        }    }    return 0;}
原创粉丝点击