雷达安装

传送门

这道题一开始没想出来怎么贪心……

后来发现,既然要让雷达能看到岛,我们没有必要一直死盯着雷达,雷达是动的,但是岛是静止的,所以一个雷达如果能探到岛的话,它必然是在某个区间之内。

所以我们可以使用勾股定理计算出所有的区间,之后就特别像活动选择了……我们按末端排序,每次选取区间末端,能跳就跳,直到结束为止。

看一下代码。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<set>
#include<queue>
#define rep(i,a,n) for(int i = a;i <= n;i++)
#define per(i,n,a) for(int i = n;i >= a;i--)
#define enter putchar('
')

using namespace std;
typedef long long ll;
const int M = 200005;
const int N = 1005;
const int INF = 2147483647;

int read()
{
    int ans = 0,op = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
    if(ch == '-') op = -1;
    ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
    ans *= 10;
    ans += ch - '0';
    ch = getchar();
    }
    return ans * op;
}

struct node
{
    double l,r,x,y;
    bool operator < (const node &g) const
    {
        return r < g.r;
    }
}a[M];

int n,ans;
double d;
bool vis[M];

double calc(int p)
{
    return sqrt(d * d - a[p].y * a[p].y);
}

int main()
{
    n = read(),scanf("%lf",&d);
    rep(i,1,n)
    {
    scanf("%lf%lf",&a[i].x,&a[i].y);
    if(a[i].y > d) printf("-1
"),exit(0);
    double k = calc(i);
    a[i].l = a[i].x - k,a[i].r = a[i].x + k;
    }
    sort(a+1,a+1+n);
    rep(i,1,n)
    {
    if(!vis[i])
    {
        vis[i] = 1;
        rep(j,i+1,n) if(a[i].r > a[j].l) vis[j] = 1;
        ans++;
    }
    }
    printf("%d
",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/captain1/p/9853392.html