P1325 radar installation and p1325 radar installation
Description
Description:
Assume that the coastline is an infinite line. One side is land, and the other side is ocean. Every small island is a point on the sea. The radar must be installed on land (including the coastline), and each radar has the same scanning range d. Your task is to build as few radar stations as possible so that all small islands are under scanning.
The data uses the Cartesian coordinate system and defines the coastline as the X axis. On the X axis, the upper side is the ocean, and the lower side is the land.
Example 1
Input/Output Format
Input Format:
The first line contains two integers n and d, n is the number of islands, and d is the radar scanning range.
The next n act island coordinates.
Output Format:
An integer represents the minimum number of radars required. If it is not possible to cover all islands, "-1" is output ".
Input and Output sample
Input example #1:
3 21 2-3 12 1
Output sample #1:
2
This is a greedy question,
Because every radar has a coverage radius, and we need to cover all the points.
Can we think like this?
Because each point needs to be overwritten, we need to expand each point into a circle with a radius that can be covered by the radar.
Then calculate the coordinates of the intersection of each circle and the X axis.
In this way, we have successfully converted this question into a line segment coverage problem.
Sort the generated data in the end order, and place a radar at the end of each node that has not been accessed.
Reference code:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 struct node 8 { 9 double x;10 double y;11 }a[10001];12 int n,d;13 int vis[10001];14 int ans=0;15 int comp(const node & a , const node & b)16 {17 if(a.y==b.y)return a.x<b.x;18 else return a.y<b.y;19 }20 int main()21 {22 scanf("%d%d",&n,&d);23 for(int i=1;i<=n;i++)24 {25 int x,y;26 scanf("%d%d",&x,&y);27 if(d<y)28 {29 printf("-1");30 return 0;31 }32 double l=sqrt(d*d-y*y);33 a[i].x=(double)x-l;34 a[i].y=(double)x+l;35 }36 sort(a+1,a+n+1,comp);37 for(int i=1;i<=n;i++)38 {39 if(vis[i]==0)40 {41 ans++;42 for(int j=i;j<=n;j++)43 if(a[j].x<a[i].y)vis[j]=1; 44 }45 }46 printf("%d",ans);47 return 0;48 }