贪心思想:尽可能使一个雷达能辐射到更多的岛屿
核心难点:将求雷达的辐射面积(圆)转换成求岛屿可以被辐射的海岸线范围(线)
性能瓶颈:排序
时间复杂度:O(NlogN)
JAVA代码:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static final int N = 1010;
static int n, d;
static double segs[][] = new double[N][2];
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
n = in.nextInt();
d = in.nextInt();
for (int i = 0; i < n; i++) {
int x = in.nextInt(), y = in.nextInt();
if (y > d) {
System.out.println(-1);
return;
}
double side = Math.sqrt(d * d - y * y);
segs[i][0] = x - side;
segs[i][1] = x + side;
}
Arrays.sort(segs, 0, n,(a, b)->Double.compare(a[1], b[1])); // JDK 1.7
int res = 0;
double last = - 0x7fffffff;
for (double[] seg : segs) {
if (seg[0] > last) {
res ++ ;
last = seg[1];
}
}
System.out.println(res);
}
}
C++代码:
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 1010;
int n, a[N];
struct Node
{
double l, r;
}seg[N];
// 以x y 为最远辐射距离 确定雷达的范围:x +- sqrt(r * r - y * y)
// 求的就是这n段范围 能用几个雷达覆盖住:n - 线段交集数
int main()
{
int n, d;
cin >> n >> d;
for (int i = 0; i < n; i ++ )
{
int x, y;
cin >> x >> y;
if (y > d)
{
cout << - 1;
return 0;
}
seg[i] = {x - sqrt(d * d - y * y), x + sqrt(d * d - y * y)}; // cpp 11 语法
}
sort(seg, seg + n, [](Node u, Node v) {return u.r < v.r;});
// for (int i = 0; i < n; i ++ ) cout << seg[i].l << ' ' << seg[i].r << endl;
double last = - 0x7fffffff;
int cnt = 0;
for (int i = 0; i < n; i ++ )
{
if (seg[i].l > last)
{
cnt ++ ;
last = seg[i].r;
}
}
cout << cnt << endl;
return 0;
}
萌新,请多多支持!QAQ