http://poj.org/problem?id=1328
这个题目就是每个岛屿对应一个雷达区间,然后确定好雷达区间后,然后在将区间的s或者e从小到大排序,然后贪心。
第一种按s从小到大排序:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define maxn 1007
using namespace std;
struct node
{
int x,y;
}tagp[maxn];
struct mode
{
double s,e;
}tagq[maxn];
int cmp(mode a,mode b)
{
return a.s < b.s;
}
int main()
{
//freopen("d.in","r",stdin);
int i,n,d;
int cas = 1;
while (scanf("%d%d",&n,&d))
{
if (!n && !d) break;
for (i = 0; i < n; ++i)
scanf("%d%d",&tagp[i].x,&tagp[i].y);
int flag = 0;
for (i = 0; i < n; ++i)
{
if (tagp[i].y > d)
{
flag = 1;
break;
}
double tmp = sqrt((1.0*d*d) - (1.0*tagp[i].y*tagp[i].y));
double s = 1.0*tagp[i].x - tmp;
double e = 1.0*tagp[i].x + tmp;
tagq[i].s = s;
tagq[i].e = e;
}
if (flag)
{
printf("Case %d: %d\n",cas++,-1);
continue;
}
sort(tagq,tagq + n,cmp);
double e = tagq[0].e;
int count = 1;
for (i = 1; i < n; ++i)
{
if (tagq[i].s > e)
{
count++;
e = tagq[i].e;
}
else
{
if (tagq[i].e < e)//注意新的终点的确定
{
e = tagq[i].e;
}
}
}
printf("Case %d: %d\n",cas++,count);
}
return 0;
}
第二中是按e从小到大排序,这里定义int型来接受double型的数据,悲催死我了。。检查了很长时间。。就是经典贪心之会场安排,不过还是有点差别的。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define maxn 1007
using namespace std;
struct node
{
int x,y;
}tagp[maxn];
struct mode
{
double s,e;
}tagq[maxn];
bool hash[maxn];
int cmp(mode a,mode b)
{
return a.e < b.e;
}
int main()
{
//freopen("d.in","r",stdin);
int i,count;
int n,d;
int cas = 1;
while (scanf("%d%d",&n,&d))
{
if (!n && !d) break;
int flag = 0;
for (i = 0; i < n; ++i)
{
scanf("%d%d",&tagp[i].x,&tagp[i].y);
if (tagp[i].y > d)
{
flag = 1;
}
double tmp = sqrt((1.0*d*d) - 1.0*(tagp[i].y*tagp[i].y));
tagq[i].s = 1.0*tagp[i].x - tmp;
tagq[i].e = 1.0*tagp[i].x + tmp;
}
if (flag)
{
printf("Case %d: %d\n",cas++,-1);
continue;
}
sort(tagq,tagq + n,cmp);
//for (i = 0; i < n; ++i)
//printf("%.2lf %.2lf\n",tagq[i].s,tagq[i].e);
int mark;
memset(hash,false,sizeof(hash));
count = 0;
int j;
j = 0;
while (1)
{
mark = 0;
double e = tagq[j].e;
for(i = 0; i < n; ++i)
{
//printf(">>%.2lf %.2lf\n",tagq[i].s,e);
if (!hash[i] && tagq[i].s <= e)//只有起点小于这个区间的终点的才能公用一个雷达
{
hash[i] = true;
}
}
count++;
for (i = 0; i < n; ++i)
{
if (!hash[i])
{
j = i;
mark = 1;
break;
}
}
if (!mark)
{
printf("Case %d: %d\n",cas++,count);
break;
}
}
}
return 0;
}