poj 1328 Radar Installation

http://poj.org/problem?id=1328

题意:在一个海岸线上按一些雷达,是雷达可以覆盖海面的小岛,问最少安装多少雷达;

思路:快拍+贪心;

代码:

View Code
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <string.h>
#include <cmath>
using namespace std;
struct node
{
double l;
double r;
}link[1001];
int num = 0;
double len = 0;
int cmp(node a,node b)
{
return a.l <b.l;
}
int main()
{
double a = 0,b = 0;
int time = 0;
while(scanf("%d%lf",&num,&len),num != 0&&len != 0)
{
++time;
int biao = 0;
double dd = 0;
int nu = 1;
for(int i = 0; i < num ;++i)
{
scanf("%lf%lf",&a,&b);
if(b > len)
biao = 1;
link[i].l = a - sqrt(len *len - b * b);
link[i].r = a + sqrt(len *len - b * b);
}
if(biao)
{
printf("Case %d: -1\n",time);
continue;
}
sort(link,link+num,cmp);
dd = link[0].r;
for(int i = 1; i <num; ++i)
{
if(link[i].l <= dd)
dd = min(link[i].r,dd);
else
{
nu++;
dd = link[i].r;
}
}
printf("Case %d: %d\n",time,nu);
}
return 0;
}



posted @ 2012-02-25 09:06  LT-blogs  阅读(146)  评论(0编辑  收藏  举报