POJ1328(Greedy,Vector,Sort,Iterator)

1328:Radar Installation

时间限制:

1000ms

内存限制:

65536kB

描述

Assume 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.

We 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.


Figure A Sample Input of Radar Installations

输入

The input consists of several test cases. (1)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.

The input is (2)terminated by a line containing pair of zeros

输出

For 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.

样例输入

3 2

1 2

-3 1

2 1

 

1 2

0 2

 

0 0

样例输出

Case 1: 2

Case 2: 1

#include"iostream"
#include"cmath"
#include"algorithm"
#include"vector"
#include"functional"
using namespace std;
class segment
{
public:
 int x,y;
 double left,right;
};
bool  compare(segment one,segment two)//为sort函数定义比较函数,若从小到大则返回true
 {
  if(one.left<two.left)
   return true;
  else
   return false;
 }
int main()
{
 int n,d;
 vector<segment> radar;
 cin>>n>>d;
 int n_case=1;
 int flag;//将flag的声明放在上面,根据试验,相对放在下面循环可以节省10ms的时间
 while(n&&d){
  segment temp;
  flag=1;//注意必须重置flag,否则会出现Output Limit Exceeded
  for(int i=0;i<n;i++)//输入
  {
   cin>>temp.x>>temp.y;
   if(temp.y>d)//判定是否有解
    flag=-1;
   temp.left=temp.x-sqrt(pow((double)d,2.0)-pow((double)temp.y,2.0));
   temp.right=temp.x+sqrt(pow((double)d,2.0)-pow((double)temp.y,2.0));
   radar.push_back(temp);
  }
  
  if(flag==-1)//flag将在上面重置;处理无解的类型和下面的有解情况必须要用if,else来分治,因为后面还要cin>>n>>d
  {
   cout<<"Case "<<n_case<<": "<<flag<<endl;
   radar.clear();
  }
  else
  {
  sort(radar.begin(),radar.end(),compare);//默认从小到大
  temp=radar[0];
  int ans=1;
  for(vector<segment>::iterator i=radar.begin()+1;i!=radar.end();i++)//利用迭代器遍历并修正相关区域范围
  {
   if((*i).left<=temp.right)//区域有交叉,取交集;注意是小于等于符
   {
    if((*i).left>temp.left)//判断左边
    {
     temp.left=(*i).left;
    }
    if((*i).right<temp.right)//判断右边
    {
     temp.right=(*i).right;
    }
   }
   else//无交叉
   {
    ans++;//雷达数加1
    temp=(*i);//temp更新
   }
  }//结束迭代器遍历
  cout<<"Case "<<n_case<<": "<<ans<<endl;
  radar.clear();//注意使用迭代器遍历的时候必须要将radar重置
  }//结束else判断下的有解情况
  n_case++;
  cin>>n>>d;
 }//结束n,d判定的循环
}

原文地址:https://www.cnblogs.com/lzhitian/p/2140071.html