NYOJ Radar 贪心之区间选点问题

来源:互联网 发布:淘宝主营类目多久更新 编辑:程序博客网 时间:2024/04/28 02:20
http://acm.nyist.net/JudgeOnline/status.php?pid=287
//贪的是:一个雷达能覆盖尽可能多的岛屿
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 1010
struct zb
{
 double a,b;
}c[N];
int cmp(zb j,zb k)
{
 return j.b<k.b;
}
int main()
{
 int x,y,lap;
 int n,r,i,t,d=0;
 double p,q,tem1,tem2;
 while(cin>>n>>r,n+r)
 {
  t=1; lap=0;
  for(i=0;i<n;i++)
  {
   cin>>x>>y;
   if(y>r)
   lap=1;
   p=x-sqrt(pow(r,2)-pow(y,2));     //将小岛坐标转化为x轴上的区间,进而把问题转化为区间选点问题
   q=x+sqrt(pow(r,2)-pow(y,2));
   c[i].a=p;
   c[i].b=q; 
  }
  if(lap==1)
  {
   cout<<"Case "<<++d<<": -1"<<endl;
   continue;
  }
  sort(c,c+n,cmp);                      
  //按坐标右端点进行升序排列
  //若按左端点排列,除了判断当前坐标段左端点小于之前坐标段的最大右端点外,还要判断此坐标段的右端点与之前坐标的最大右端点那个较大,取较大的
  //tem1=c[0].a;
  tem2=c[0].b;
  for(i=1;i<n;i++)
  {
   if(c[i].a<=tem2)      //有重碟部分的直接跳过
   //tem2=c[i].b;         //不要按区间完全覆盖问题处理!!!
   continue;
   else
   {
    //tem1=c[i].a;
    tem2=c[i].b;
    t++;
   }
  }
  cout<<"Case "<<++d<<": "<<t<<endl;
 }
 return 0;
}
0 0
原创粉丝点击