zoj1360/poj1328 Radar Installation(贪心)

对每个岛屿,能覆盖它的雷达位于线段[x-sqrt(d*d-y*y),x+sqrt(d*d+y*y)],那么把每个岛屿对应的线段求出来后,其实就转化成了经典的贪心法案例:区间选点问题。数轴上有n个闭区间[ai,bi],取尽量少的点,使得每个区间内都至少有一个点。选法是:把区间按右端点从小到大排序(右端点相同时按左端点从大到小),然后每个没选的区间选最右边的一个点即可。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<list>
#include<deque>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>
using namespace std;
#define pii pair<int,int>
#define LL long long int
const double eps=1e-;
const int INF=;
const int maxn=+; int n,cas=;
double d,x,y;
struct Line
{
double l,r;
bool used;
Line(double ll=,double rr=,bool uu=false):l(ll),r(rr),used(uu) {}
Line(const Line& C)
{
l=C.l;
r=C.r;
used=C.used;
}
bool operator < (const Line& B) const
{
if(r!=B.r)
return r<B.r;
else
return l>B.l;
}
} a[maxn]; int main()
{
//freopen("in2.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d%lf",&n,&d)==)
{
if(n==&&d==) break;
printf("Case %d: ",cas++);
int ans=;
for(int i=; i<n; i++)
{
scanf("%lf%lf",&x,&y);
if(y>d)
{
ans=-;
break;
}
a[i].l=x-sqrt(d*d-y*y);
a[i].r=x+sqrt(d*d-y*y);
//cout<<a[i].l<<' '<<a[i].r<<endl;
a[i].used=false;
}
if(ans==-)
{
printf("-1\n");
}
else
{
sort(a,a+n);
for(int i=; i<n; i++)
{
//cout<<a[i].l<<' '<<a[i].r<<endl;
if(a[i].used==false)
{
a[i].used=true;
ans++;
for(int j=i+; j<n; j++)
{
if(a[j].l<=a[i].r)
{
a[j].used=true;
}
else break;
}
}
}
printf("%d\n",ans);
}
}
//fclose(stdin);
//fclose(stdout);
return ;
}
上一篇:POJ1328 Radar Installation(贪心)


下一篇:ipython及Python初体验