Radar Installation 贪心 水

果然好久没A题,都不记得题目给的数据有可能有坑。。。

1.注意小岛的y有可能是负数。。。

2.看清题目,坐标为整数

4.关于priority queue 要想它先吐出小的数可以按照下面结构体的运算符重载 

5.思路

1)将小岛从左到右编号1~n。
2)访问第一个小岛,根据小岛坐标计算出雷达在海岸线上的坐标范围,取该区间的最右侧建立雷达。
3)继续访问下一座小岛,如果该小岛处于已经建立的雷达范围内,跳过。如果不在范围内,同2)建立新的雷达站。
4)访问所有的小岛,结束。

6. 要注意有可能下一个小岛不在当前所放置的雷达范围,但是根据dis函数得到新的雷达的x坐标要比原来的还要小,则只要将原来的雷达挪一挪位置就好了,sum就不要++了

#include <iostream>
#include <queue>
#include <cmath>
using namespace std;
double n,d,x,y;
class node
{
    public:
    double x,y;
    bool operator<(const node& a)const
    {
        if(x!=a.x)
            return x>a.x;
        else return y>a.y;
    }

}a[1005];
double getdis(double x1,double x2,double y1,double y2)
{
    return (double)sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double dis(double x,double y)
{
    return x+(double)sqrt(d*d-y*y);
}
int main()
{
    int r=1;

    while(cin>>n>>d)
    {
        bool flag=true;
        if(n==0&&d==0) break;
        priority_queue<node> p;
        for(int i=0; i<n; i++)
        {
            cin>>a[i].x>>a[i].y;
            if(a[i].y>d||a[i].y<0) flag=false;
        }
        if(!flag)
        {
            cout<<"Case "<<r<<": "<<-1<<endl;
            r++;
            continue;
        }
        for(int i=0; i<n; i++) p.push(a[i]);
        double tx;
        node t=p.top();
        p.pop();
        tx=dis(t.x,t.y);
        int sum=1;
        while(1)
        {
             if(p.empty()) break;
            while(getdis(t.x,tx,t.y,0)-d<=0.001)
            {
                if(p.empty())
                {
                    flag=false;
                    break;
                }
                t=p.top();
                p.pop();
            }
            if(!flag) break;
            double kk=dis(t.x,t.y);
            if(kk-tx<=0.01){}
            else {
            sum++;
            }
            tx=kk;
        }

        cout<<"Case "<<r<<": "<<sum<<endl;
        r++;

    }
}

Radar Installation

10-26

DescriptionnnAssume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d. nnWe use Cartesian coordinate system, defining the coasting is the x-axis. The sea side is above x-axis, and the land side below. Given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. Note that the position of an island is represented by its x-y coordinates. n ![](http://poj.org/images/1328_1.jpg)nFigure A Sample Input of Radar InstallationsnnnInputnnThe input consists of several test cases. The first line of each case contains two integers n (1<=n<=1000) and d, where n is the number of islands in the sea and d is the distance of coverage of the radar installation. This is followed by n lines each containing two integers representing the coordinate of the position of each island. Then a blank line follows to separate the cases. nnThe input is terminated by a line containing pair of zeros nOutputnnFor each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. "-1" installation means no solution for that case.nSample Inputnn3 2n1 2n-3 1n2 1nn1 2n0 2nn0 0nSample OutputnnCase 1: 2nCase 2: 1

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

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭