poj1328
来源:互联网 发布:欢乐树的朋友们知乎 编辑:程序博客网 时间:2024/06/10 20:19
题目大意是:一条水平线,上半部有岛屿,给出n个岛屿的坐标,在x轴上有雷达,雷达最多覆盖d距离,问最少设置多少个雷达可以扫遍所有岛屿,明显的区间覆盖问题。一开始想的是只要两两岛屿之间距离小于2×d就可以被同一个雷达扫到,然后WA了,原因在于不一定能构造出圆。
解题思路是:以每一个岛屿为圆心,半径为d,作圆与x轴相交,则交到的范围就是可以放置雷达扫到该岛屿的范围,当然,如果没有交到,则证明没有答案,因为雷达只能在x轴上。得到n个区间,对这些区间的左端点排序,从左扫起,找公共区间。
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <cstdio>
#include <cstdlib>
using namespace std;
int d;
struct point
{
double l,r;
}p[1005];
bool cmp (point a,point b)
{
return a.l<b.l;
}
int main()
{
int n;
int r=1;
while(cin>>n>>d)
{
if(n+d==0)break;
memset(vis,0,sizeof(vis));
bool f=1;
for(int i=1;i<=n;i++)
{
double a,b;
cin>>a>>b;
if(b>d)
{
f=0;
}
p[i].r=sqrt((d*d-b*b)*1.0)+a;
p[i].l=a-sqrt(1.0*(d*d-b*b));
}
if(!f){
cout<<"Case "<<r++<<": "<<-1<<endl;
continue;
}
sort(p+1,p+n+1,cmp);
int ans=1;
double s=p[1].l,e=p[1].r;
for(int i=2;i<=n;i++)
{
if(p[i].l<=e)
{
s=p[i].l;
}
if(p[i].r<e)e=p[i].r;
if(p[i].l>e)
{
ans++;
s=p[i].l;
e=p[i].r;
}
}
cout<<"Case "<<r++<<": "<<ans<<endl;
}
return 0;
}
- POJ1328
- poj1328
- POJ1328
- poj1328
- POJ1328
- poj1328
- poj1328
- poj1328
- poj1328
- poj1328
- poj1328
- poj1328
- poj1328
- POJ1328
- POJ1328
- POJ1328
- poj1328
- POJ1328
- eclipse中提高编程效率的快捷键
- IntelliJ IDEA 主题设置
- 数据存储
- iOS 广告轮播
- artTemplate访问外部公用函数(二)
- poj1328
- android布局及属性
- 详解Java的自动装箱与拆箱(Autoboxing and unboxing)
- C++设计模式——单例模式
- Java虚拟机的类加载机制
- C++多线程Singleton模式
- 项目实战No11 精华模块
- hibernate下oracle转mysql中数据类型的转换
- 一个上网APP如何让全民拥有免费wifi的秘诀