您的位置:首页 > 其它

Radar Installation(POJ 1328 区间贪心)

2016-01-28 11:06 337 查看
Radar Installation

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 68578Accepted: 15368
Description

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

需要判断d<0,a[i].y>d情况。
首先,按照x坐标排序,对于每个岛屿求出雷达所能放置的区间,然后对这些进行处理,x1,x2;
设当前雷达放置位置为nowx,对于下一个区间,如果写x1>nowx,显然多需要一个雷达,反之如果nowx>x1,nowx=min(nowx,x2);


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
struct node
{
int x,y;
}a[1000+5];
bool cmp(node q,node p)
{
if(q.x==p.x)
return q.y>=p.y;
return q.x<p.x;
}
int main()
{
int n,d;
int i,j;
int k=1;
freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&d))
{
int coun=1;
if(n==0&&d==0)
break;
bool flag=0;
for(i=0;i<n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
if(a[i].y>d)
flag=1;
}
if(flag||d<=0)
{
printf("Case %d: -1\n",k++);
continue;
}
sort(a,a+n,cmp);
double nowx=sqrt(double(d*d-a[0].y*a[0].y))+a[0].x;
double x1,x2,temp;
for(i=1;i<n;i++)
{
temp=sqrt(double(d*d-a[i].y*a[i].y));
x1=a[i].x-temp;
x2=a[i].x+temp;
if(x1>nowx)
{
nowx=x2;
coun++;
}
else if(nowx>x2)
nowx=x2;
}
printf("Case %d: %d\n",k++,coun);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: