poj 1328 Radar Installation

英语的战五渣看懂了题,就不翻译了吧

枚举好奇妙,可以做出大部分题,先对他们在x轴的大小排序,相邻的两个如果有交集就计算出交集,交集是圆心的范围,没交集,雷达数加一

#include 
#include 
#include 
#include 
using namespace std;
struct node {
    int x, y;
};
node g[1111];
double posr[1111], posl[1111];
int n, d, ans;
int cmp (node a, node b) {
    if (a.x == b.x) return a.y > b.y;
    return a.x < b.x;
}
double fx (node x) {
    double k = sqrt (1.*d * d - x.y * x.y);
    return k;
}
int main() {
    for (int t = 1; cin >> n >> d; t++) {
        if (n == 0 && d == 0) break;
        ans = 0;
        for (int i = 1; i <= n; i++) {
            cin >> g[i].x >> g[i].y;
            if (g[i].y > d) ans = -1;
        }
        if (ans == 0) {
            sort (g + 1, g + 1 + n, cmp);
            if (n >= 1) {
                posl[++ans] = g[1].x - fx (g[1]);
                posr[ans] = g[1].x + fx (g[1]);
            }
            for (int i = 2; i <= n; i++) {
                if (g[i].x == g[i - 1].x) continue;
                if (g[i].x - fx (g[i]) > posr[ans]) {
                    posl[++ans] = g[i].x - fx (g[i]), posr[ans] = g[i].x + fx (g[i]);
                    continue;
                }
                posl[ans] = max (posl[ans], g[i].x - fx (g[i]) ), posr[ans] = min (posr[ans], g[i].x + fx (g[i]) );
            }
        }
        printf ("Case %d: %d\n", t, ans);
    }
    return 0;
}


你可能感兴趣的:(ACM-枚举)