Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 55857 | Accepted: 12596 |
Description
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 is terminated by a line containing pair of zeros
Output
Sample Input
3 2 1 2 -3 1 2 1 1 2 0 2 0 0
Sample Output
Case 1: 2 Case 2: 1
Source
int cmp(const void *a,const void *b)
{
Point *p1=(Point *)a,*p2=(Point *)b;
if(p1->l!=p2->l)
return p1->l-p2->l;
return p1->r-p2->r;
}
應該這樣寫:int cmp(const void *a,const void *b)
{
Point *p1=(Point *)a,*p2=(Point *)b;
if(p1->l>p2->l)
return 1;
if(p1->l<p2->l)
return -1;
if(p1->r>p2->r)
return 1;
if(p1->r<p2->r)
return -1;
return 0;
}
#include <iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define N 1010
#define getx2(x) (x)*(x)
#define eps 1e-10
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
struct Point
{
double l,r;
int x,y;
}p[N];
void init(int n)
{
for(int i=0;i<=n;i++)
{
p[i].l=p[i].r=p[i].x=p[i].y=0;
}
}
int cmp(const void *a,const void *b)
{
Point *p1=(Point *)a,*p2=(Point *)b;
if(p1->l>p2->l)
return 1;
if(p1->l<p2->l)
return -1;
if(p1->r>p2->r)
return 1;
if(p1->r<p2->r)
return -1;
return 0;
}
int main(int argc, char** argv) {
int n,d,cnt=0,i;
bool flag;
while(~scanf("%d%d",&n,&d)&&(n||d))
{
flag=true;
init(n);
for(i=0;i<n;i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
if(p[i].y>d)
{
flag=false;
continue;
}
p[i].l=p[i].x-sqrt(getx2(d)-getx2(p[i].y));
p[i].r=p[i].x+sqrt(getx2(d)-getx2(p[i].y));
}
if(!flag)
{
printf("Case %d: -1\n",++cnt);
continue;
}
qsort(p,n,sizeof(p[0]),cmp);
int ans=1;
double cr=p[0].r;
for(i=1;i<n;i++)
{
if(p[i].l>cr)
{
ans++,cr=p[i].r;
}
else if(p[i].r<cr)
cr=p[i].r;
}
printf("Case %d: %d\n",++cnt,ans);
}
return 0;
}