[POJ] 1328.Radar Installation

题目链接

思路:
每一个岛屿点在y轴的距离超出雷达范围时,即无法搜索到,输出-1;
计算每一个岛屿距离一个可被搜索到的雷达在X轴上的距离,作为区间,保存每一个区间。
判断当前岛屿是否在雷达范围,在即判断下一个;不在就在当前区间的最右边放一个雷达。

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

struct node
{
  double left;
  double right;
} rad[2002], temp;

struct
{
  double x;
  double y;
} island[1001];

int cmp(node a, node b) { return a.left < b.left; }

int main()
{
  int n, d;
  int cant = 0;

  while (cin >> n >> d && n != 0 && d != 0)
  {
    cant++;
    int flag = 0;
    for (int i = 0; i < n; i++)
    {
      cin >> island[i].x >> island[i].y;
      if (island[i].y > d)
        flag = 1;
    }
    if (flag)
    {
      printf("Case %d: -1\n", cant);
      continue;
    }

    for (int i = 0; i < n; i++)
    {
      double t = sqrt(d * d * 1.0 - island[i].y * island[i].y * 1.0);
      rad[i].left = island[i].x - t;
      rad[i].right = island[i].x + t;
    }
    sort(rad, rad + n, cmp);
    int num = 0;
    double maxleft = -10000000;

    for (int i = 0; i < n; i++)
    {
      if (rad[i].left > maxleft)
      {
        num++;
        maxleft = rad[i].right;
      }

      else if (rad[i].right < maxleft)
      {
        maxleft = rad[i].right;
      }
    }
    printf("Case %d: %d\n", cant, num);
  }

  return 0;
}
posted @ 2018-09-14 17:34  Ruohua3kou  阅读(105)  评论(0编辑  收藏  举报