当前位置:网站首页>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
边栏推荐
- OSI层常用协议
- MySQL series - install MySQL 5.6.27 on Linux and solve common problems
- Utf8 to STD: string and STD: string to utf8
- Membarrier (personal learning and understanding)
- Multiple mainstream SQL queries only take the latest one of the data
- College entrance examination volunteer filling reference
- On the use of constant pointer and pointer constant -- exercise (record)
- Differences between auto and decltype inference methods (learning notes)
- Frequently asked interview questions - 3 (operating system)
- Branch and loop statements
猜你喜欢

Uncle wolf is looking for a translator -- Plato -- ongoing translation

Generation of straightening body in 3D slicer

SQL Server检索SQL和用户信息的需求

Use of uniapp native plug-ins

Hongji | how does HR carry out self change and organizational change in the digital era?
![[the background color changes after clicking a line]](/img/3a/709d47fd3a370d86569fb9b560b403.png)
[the background color changes after clicking a line]

Parameter analysis of open3d material setting

相机成像+单应性变换+相机标定+立体校正

Double click The jar package cannot run the solution

Nécessité de précharger les cookies dans le sélénium
随机推荐
Uniapp wechat sharing
Redis in node -- ioredis
Frequently asked interview questions - 2 (computer network)
Requirements for SQL server to retrieve SQL and user information
[the background color changes after clicking a line]
what is wifi6?
SQL语句简单优化
创建进程内存管理copy_mm - 进程与线程(九)
Tslint annotations ignore errors and restful understanding
Error handling mechanism of the strongest egg framework in history
Qwebsocket communication
Multi process model in egg -- egg document Porter
How to set the initial value of El input number to null
Similarities and differences between vector and array (notes)
After NPM was upgraded, there was a lot of panic
Processus d'exécution du programme exécutable
转置卷积(Transposed Convolution)
es6数组的使用
Output string in reverse order
egg中的cors和proxy(づ ̄3 ̄)づ╭~踩坑填坑的过程~ToT~