贪心之建立雷达


如图所示,将问题转化为用最少的雷达点使得每条线段上至少有一个雷达

#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <memory>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
struct node
{
    int x,y;
    double left,right;
};
bool cmp(const node &a,const node &b)
{
    return a.left < b.left;
}
int n,d,kase=1;
node rader[1005];
int main()
{
    while(cin >> n >> d,n!=0&&d!=0)
    {
        int flag=1;
        for(int i=0; i<n; i++)
        {
            cin >> rader[i].x >> rader[i].y;
            if(abs(rader[i].y)>d)
            {
                flag=0;
            }
            rader[i].left = 1.0*rader[i].x - sqrt(1.0*(d*d-rader[i].y*rader[i].y));
            rader[i].right = 1.0*rader[i].x + sqrt(1.0*(d*d-rader[i].y*rader[i].y));
        }
        sort(rader,rader+n,cmp);
        int num = 1;
        double Left = rader[0].left,Right = rader[0].right;
        for(int i=1; i<n; i++)
        {
            if(rader[i].left <= Right)//注意此时right也需要更新
                {
                    Right = min(Right,rader[i].right);
                }
            else
            {
                num++;
                Right = rader[i].right;
            }
        }
        if(flag)
            cout<<"Case"<<' '<<kase<<": "<<num<<endl;
        else
            cout<<"Case"<<' '<<kase<<": "<<-1<<endl;
        kase++;
        getchar();
    }
    return 0;
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页