雷达设备(贪心)

雷达设备

[Link](AcWing 112. 雷达设备 - AcWing)

题意

给你一些点,问你在 x x x轴上最少放多少个半径为 r r r的圆可以把这些点都覆盖掉。

题解

可以转化一下想每一个点可以被 x x x轴上哪些位置放圆覆盖掉,发现每个点对应的合法位置是一个个线段,问题就转化成如何在 x x x轴上放最少的点使得每个线段上至少有一个点。

我们可以把所有的线段按照右端点排序,如果当前放点的位置在新区间内我们就跳过,如果不在新区间我们就在新区间的最右端放一个点,因为越靠右越能覆盖更多的线段,这样贪出来的就是最优解,复杂度为 O ( n l o g n ) O(nlogn) O(nlogn)

可以简单的证明一下:

假设最优解为 r e s res res,按照上面贪出来的解是 t m p tmp tmp。那么 t m p ≥ r e s tmp \ge res tmpres一定成立,然后因为我们的按照右端点排序后,每个点都是选在了某个线段的最右边,所以一定有 t m p tmp tmp个不相交的区间,因此 r e s ≥ t m p res \ge tmp restmp,所以最优解就是 t m p tmp tmp

Code

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath> 
#include <stack>
#include <iomanip>
#include <deque> 
#include <sstream>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
#define tpyeinput int
inline char nc() {static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}
inline void read(tpyeinput &sum) {char ch=nc();sum=0;while(!(ch>='0'&&ch<='9')) ch=nc();while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();}
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
    e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k, r;
struct Segment {
    double l, r;
    bool operator <(const Segment &t)const {
        return r < t.r;
    } 
}seg[N];
int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    cin >> n >> r;
    bool ok = false;
    for (int i = 0; i < n; i ++ ) {
        int x, y; cin >> x >> y;
        if (y > r) ok = true;
        else {
            double d = sqrt((r * r - y * y));
            seg[i] = {x - d, x + d};
        }
    }
    if (ok) puts("-1");
    else {
        sort(seg, seg + n);
        int cnt = 0; double pre = -1e20;
        for (int i = 0; i < n; i ++ ) 
            if (pre < seg[i].l) {
                pre = seg[i].r;
                cnt ++;
            }
        cout << cnt << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值