Radar
时间限制:
1000 ms | 内存限制:
65535 KB
难度:
3
-
描述
-
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.
-
输入
-
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
输出
- 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. 样例输入
-
3 2 1 2 -3 1 2 1 1 2 0 2 0 0
样例输出
-
Case 1: 2 Case 2: 1
-
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.
这个题经过转化,可以转化为区间选点问题,只需要对初始数据进行处理,就可以得到区间选点的模型了,区间选点问题和区间不相交问题区别不大,思路都一样,只是后期处理的方式不一样,这个题主要是看明白题意了.....
贪心算法有点不太清楚,理解的不够透彻,以后还需要多练练
数据类型比较坑,注意类型的转换!
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
struct pt
{
double l,r;
}p[1005];
int cmp (pt a,pt b)
{
if(a.r==b.r)
{
return a.l>b.l;
}
return a.r<b.r;
}
double zh(int a,int b)
{
return sqrt(a*a*1.0-b*b);
}
int t;
int main()
{
t=0;
int n,i,kase,d,a,b;
double right,k;
while(scanf("%d%d",&n,&d),n||d)
{
kase=1;
for(i=0;i<n;++i)
{
scanf("%d%d",&a,&b);
if(b>d&&kase)
{
kase=0;
}
if(kase)
{
k=zh(d,b);
p[i].l=a-k;
p[i].r=a+k;
}
}
printf("Case %d: ",++t);
if(kase==0)
{
printf("-1\n");
continue;
}
sort(p,p+n,cmp);
right=p[0].r;
int sum=1;
for(i=1;i<n;++i)
{
if(right<p[i].l)
{
right=p[i].r;
++sum;
}
}
printf("%d\n",sum);
}
return 0;
}
POJ 1328
/*
2016年3月13日18:33
*/
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int cnt=0;
struct node
{
int kase;//标记不可能达到的点
double l,r;
}x[10005];
bool cmp(node a,node b)
{
/*if(a.l==b.l)//错误
{
return a.r<b.r;
}*/
return a.l<b.l;
if(a.r==b.r)
{
return a.l<b.l;
}
return a.r<b.r;
}
node dis(double r,double tx,double ty)
{
node tp={0};
if(r>=ty)
{
double td=sqrt(r*r*1.0-ty*ty);
tp.l=tx-td;tp.r=tx+td;
}
else
{
tp.kase=1;//标记不可能的点
}
return tp;
}
int slove(int n)
{
sort(x,x+n,cmp);
int ans=0;
double last=x[0].l-1;
for(int i=0;i<n;++i)
{
if(x[i].kase)//无法到达的点
{
return -1;
}
if(x[i].l>last)
{
last=x[i].r;
++ans;
}
}
return ans;
}
int main()
{
int n,r;
while(scanf("%d%d",&n,&r),n|r)
{
for(int i=0;i<n;++i)
{
double tx,ty;
scanf("%lf%lf",&tx,&ty);
x[i]=dis(r,tx,ty);
}
printf("Case %d: %d\n",++cnt,slove(n));
}
return 0;
}