挑战程序设计竞赛 Radar Installation

题目
思路来源于csdn的其他博客,由小岛的位置推出雷达的区间,通过雷达的区间求出雷达的最少数量

#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip>
#include <vector>
#include <map>
#include <cstring>
#include <algorithm>
using namespace std;

struct Interval{
    double begin;
    double end;
};
typedef long long ll;
int N, D;
Interval radar[1001];

bool Cmp(Interval a, Interval b)
{
    if (a.begin != b.begin) {
        return a.begin < b.begin;
    }
    return a.end < b.end;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int cnt = 0;
    while (cin >> N >> D && !(N == 0 && D == 0) ) {
        double x, y;
        bool flag = true;
        for (int i = 0; i < N; i++) {
            cin >> x >> y;
            if (y > D) {
                flag = false;
            }
            else {
                radar[i].begin = x - sqrt(D * D - y * y);
                radar[i].end = x + sqrt(D * D - y * y);//勾股定理
            }
        }

        if (!flag) {//特殊情况
            cout << "Case " << ++cnt << ": " << -1 << endl; 
            continue;
        }
        sort(radar, radar + N, Cmp);
        int ans = 1;
        Interval last = radar[0];
        for (int i = 1; i < N; i++) {//通过区间求出最小雷达数量
            if (radar[i].begin <= last.end) {
                last.begin = radar[i].begin;
                if (radar[i].end < last.end) {
                    last.end = radar[i].end;
                }
            }
            else {
                ans++;
                last = radar[i];
            }
        }
        cout << "Case " << ++cnt << ": " << ans << endl; 
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值