AcWing 112. 雷达设备

原题链接

考察:贪心

一开始的思路是两两小岛构造一个半径为d的⚪,没做出来,⚪的交集太难求.

思路:

        这道题的思路不是每个雷达覆盖尽可能多的岛,而是每个岛能被x轴上哪些点覆盖.对于每个岛求出它在x轴被覆盖的最小坐标和最大坐标,这样形成的区间就是能覆盖该岛的坐标区间.这样就转化成区间选点问题.

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cmath>
 4 using namespace std;
 5 const int N = 1010;
 6 typedef pair<int,int> PII;
 7 PII p[N];
 8 struct Road{
 9     double l,r;
10     bool operator<(Road x)
11     {
12         return this->r<x.r;
13     }
14 }road[N];
15 int main()
16 {
17     int n,d,ans = 0;
18     scanf("%d%d",&n,&d);
19     for(int i=1;i<=n;i++) scanf("%d%d",&p[i].first,&p[i].second);
20     for(int i=1;i<=n;i++)
21     {
22         if(p[i].second>d) {printf("-1\n");return 0;}
23         int t = d*d-p[i].second*p[i].second;
24         road[i].l = p[i].first-sqrt(t);
25         road[i].r = p[i].first+sqrt(t);
26     }
27     sort(road+1,road+n+1);
28     double ed = -9999999999999;
29     for(int i=1;i<=n;i++)
30         if(road[i].l>ed) ed = road[i].r,ans++;
31     printf("%d\n",ans);
32     return 0;
33 }

 

posted @ 2021-02-26 13:56  acmloser  阅读(59)  评论(0编辑  收藏  举报