poj 1328 Radar Installatio【贪心】

题目地址:http://poj.org/problem?id=1328

Sample Input

3 2
1 2
-3 1
2 1

1 2
0 2

0 0

Sample Output

Case 1: 2
Case 2: 1

参考博客地址:http://www.cnblogs.com/jackge/archive/2013/03/05/2944427.html
分析:一个岛屿坐标(x,y),在x轴上会存在一个线段区间,在这个线段区间内任意位置放置雷达都可以。
int dd=sqrt(d*d-y*y); [(double)x-dd, (double)y+dd]就是这个区间。注意区间的左右要用double类型。

当然啦如果雷达本来就覆盖不到(y>d),则是另当别论。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

int n, d;

struct node
{
	double ll, rr;
	bool operator<(const node &dd)const{
		return ll<dd.ll;
	}
}seg[1010];

int main()
{
	int i, j;
	int cnt=1;
	int x, y;
	bool flag;

	while(scanf("%d %d", &n, &d)!=EOF ){
        if(n==0 && d==0 ) break;

		flag=true;
		for(i=0; i<n; i++)
		{
			scanf("%d %d", &x, &y);
			double dd=sqrt((double)(d*d)-y*y );
			seg[i].ll = x-dd;
			seg[i].rr = x+dd;
			if( y>d ){ //有的岛屿位置太高, x轴上的雷达无法覆盖到 即d<当下岛屿的y
				flag=false;
			}
		}
	   if(flag==false){
		   printf("Case %d: -1
", cnt++); //无法解决
		   continue;
	   }
		sort(seg, seg+n);
		int ans=1;
		node cur; cur=seg[0];

		for(i=1; i<n; i++){
			double li=seg[i].ll; double ri=seg[i].rr;
                        //一定是double类型
			if( cur.rr >= ri){
				cur=seg[i];
			}
			else if(cur.rr<li ){
				ans++; cur=seg[i];
			}
		}
		printf("Case %d: %d
", cnt++, ans );
	}
	return 0;
}

原文地址:https://www.cnblogs.com/yspworld/p/4691757.html