当前位置:网站首页>Radar equipment (greedy)
Radar equipment (greedy)
2022-04-23 05:33:00 【Ice Cream~】
https://www.acwing.com/problem/content/description/114/
Suppose the coast is an infinite straight line , Land is on the side of the coast , The ocean is on the other side .
Each island is at a point on the side of the ocean .
Radar device All on the coastline , And the radar monitoring range is d, When the distance between the island and a radar does not exceed d when , The island can be covered by radar .
We use Cartesian coordinates , Define the coastline as x Axis , One side of the sea is x Above the axis , The land side is x Below axis .
Now give the specific coordinates of each island and the detection range of radar , Please find out what can make All the islands are covered by radar Minimum number of radars required .
Input format
Enter two integers on the first line n and d, They represent the number of islands and the range of radar detection .
Next n That's ok , Enter two integers per line , They represent the island x,y Axis coordinates .
Separate the data in the same row with spaces .
Output format
Output an integer , Represents the minimum number of radars required , if If there is no solution, the required number of outputs −1.
Data range
1≤n≤1000
−1000≤x,y≤1000sample input :
3 2 1 2 -3 1 2 1sample output :
2
Ideas : Minimum radar coverage is required for all points , The area scanned by the radar is d The area of a circle with a radius , We try to transform this binary space into a one-dimensional space , That is, the range that the point can be covered by radar

If you add another point (x1,y1)

The radar range of these two points intersects , Description in Choose any point at the intersection of two intervals as the radar , Can cover these two points , According to this information , Let's record the interval , And sort according to the right endpoint , If the right end of the first interval is within the range of the following interval , It shows that there is an intersection , If it is less than the left end point of the following interval , It means there is no intersection .

For the upper interval , Need at least 3 Only one radar can cover all points . So this question turns into Find interval , Sort , Greedy solution
Code segment :
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef struct node{
double a,b;
}p;
int cmp(node p1, node p2)// Sort in ascending order at the right endpoint
{
return p1.b < p2.b;
}
int main()
{
int n,d;
cin>>n>>d;
int x,y;
p k[1010];
int ans=0;
for(int i=0;i<n;i++)
{
cin>>x>>y;
if(d*d<y*y)// If y Greater than d, Radar scan failed , Output -1
{
ans=1;
}
else{
k[i].a=x-sqrt(d*d-y*y);// Find the left end point of the interval
k[i].b=x+sqrt(d*d-y*y);// Find the right endpoint of the interval
}
}
if(ans==1){cout<<"-1";}
else {
sort(k,k+n,cmp);
double t=k[0].b;// Assign the right end of the first interval to t
int count=1;
for(int i=1;i<n;i++)
{
if(t<k[i].a)// If t This point is smaller than the left end of the next interval , It means there is no intersection
{
count++;
t=k[i].b;// Assign the right end of this interval to t, Overwrite the last right endpoint
}
}
cout<<count<<endl;
}
return 0;
}
版权声明
本文为[Ice Cream~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220534525218.html
边栏推荐
- Use of uniapp native plug-ins
- qt. qpa. plugin: Could not find the Qt platform plugin “xcb“ in ““
- Three methods of list rendering
- d. TS --- for more detailed knowledge, please refer to the introduction on the official website (chapter of declaration document)
- Camera imaging + homography transformation + camera calibration + stereo correction
- Markdown syntax support test
- Processus d'exécution du programme exécutable
- Basic knowledge of redis
- npm升级后问题,慌得一批
- 使用宝塔+xdebug+vscode远程调试代码
猜你喜欢

QT displays the specified position and size of the picture

3d slicer中拉直体的生成

Laravel database

Requirements for SQL server to retrieve SQL and user information

Laravel implements the Holy Grail model with template inheritance

弘玑Cyclone RPA为国金证券提供技术支撑,超200个业务场景实现流程自动化

what is wifi6?

Create process memory management copy_ Mm - processes and threads (IX)

Nécessité de précharger les cookies dans le sélénium

C# ,类库
随机推荐
Excel 2016 打开文件第一次打不开,有时空白,有时很慢要打开第二次才行
Nécessité de précharger les cookies dans le sélénium
Necessity of selenium preloading cookies
Solve the problem of JS calculation accuracy
Uniapp wechat sharing
es6数组的使用
Use pagoda + Xdebug + vscode to debug code remotely
Cmake basic tutorial (39) pkgconfig
数据安全入门产品——数据库审计系统详解
Create a tabbar component under the components folder, which is public
CORS and proxy (づ  ̄ 3  ̄) in egg ~ the process of stepping on the pit and filling the pit ~ tot~
弘玑Cyclone RPA为国金证券提供技术支撑,超200个业务场景实现流程自动化
How to set the initial value of El input number to null
Pol / select / EPO
Use of ES6 array
Tslint annotations ignore errors and restful understanding
Redis的基本知识
[no title] Click the classification jump page to display the details
If I am PM's performance, movie VR ticket purchase display
Markdown syntax support test