贪心习题

习题

1. 雷达设备

思路:

  1. 先求出能覆盖某一个岛屿的雷达可以存在的线段,问题变成了在若干条线段里,找最少的点,使得每一个线段上最少有一个点。
  2. 右端点排序,依次考虑每一条线段的左端点和上一次建雷达站的线段的右端点R。有交集则continue。无交集则建一个新雷达站,并更新R。
/*
 * Author:  Chen_zhuozhuo
 * Created Time:  2020/3/17 20:49:33
 * File Name: d.cpp
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <time.h>
using namespace std;
const int maxint = -1u>>1;
const double inf = 1e10;
const int maxn = 1005;
typedef pair<double, double> PII;
int n;
double esp = 1e-6;
double d;

///a[i].first = right, a[i].second = left;
PII a[maxn];

int main() {
    cin>>n>>d;
    for(int i=1;i<=n;i++){
        int x,y;
        cin>>x>>y;
        if(y > d){
            cout<<"-1"<<endl;
            return 0;
        }
        double temp = sqrt(d*d - y*y);
        a[i].first = x + temp;
        a[i].second = x - temp;///右端点相同怎么排序
    }
    sort(a + 1, a + 1 + n);
    int cnt = 0;
    double R = -inf;
    for(int i = 1; i <= n; i ++){
        if(a[i].second < R + esp)
            continue;
        R = a[i].first;
        cnt ++;
    }
    cout<<cnt<<endl;
    return 0;
}

/*
输入样例:
3 2
1 2
-3 1
2 1
输出样例:
2
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值