poj1328

#include
#include <cstdio>
#include <algorithm>
#include <cmath>


using namespace std;


const int maxx=1010;


struct node
{
     double left,right;
}a[maxx];


bool cmp(node i,node j)
{
     return i.left<j.left;
}


int main()
{
    int n,d,t=0;
    while (cin>>n>>d)
    {
         if (n==0&&d==0) break;else t++;
         int x,y,flag=1;
         for (int i=1;i<=n;i++)
         {
              cin>>x>>y;
              if (y>d) flag=0;
              a[i].left=x-sqrt(d*d-y*y);
              a[i].right=x+sqrt(d*d-y*y);
         }
         if (flag)
         {
              sort(a+1,a+1+n,cmp);
              int ans=1;
              double radar;
              radar=a[1].right;
              for (int i=2;i<=n;i++)
              {
                if (a[i].right<radar) radar=a[i].right;
                else if (a[i].left>radar)
                {
                  ans++;
                  radar=a[i].right;
                }
              }
              cout<<"Case "<<t<<": "<<ans<<endl;
         }
         else cout<<"Case "<<t<<": -1"<<endl;


    }
    //cout << "Hello world!" << endl;
    return 0;
}
/*題意:有一個座標軸 在X軸上方是海 下方是陸地 X軸是海岸線,海上有N個小島,現在要在海岸線上安裝雷達,
雷達的覆蓋範圍是一個以R爲半徑的圓,請用最少的雷達覆蓋所有的小島;當無法覆蓋時 輸出-1


思路:算出 每個小島能被覆蓋的雷達的圓心,即以小島爲圓心 R爲半徑 作圓,該圓與X軸的交點:
左交點爲x-sqrt(R*R-y*y); 右交點爲x+sqrt(R*R-y*y);
令當前雷達在第一個島的右交點
按照 左交點 排序,如果i點的左交點在 當前雷達的右邊 則需安裝一個新雷達,更新雷達
否則 如果 i點的右交點也在當前雷達的左邊 則把當前雷達的圓心更新爲該點的右交點;*/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章