POJ1328 贪心

2017年3月18日 | ljfcnyali
题目大意
地图的x轴的上方为海,下方为陆地,海中有n个小岛,坐标为(isl[i].x,isl[i].y)。有一种雷达,能探测到的范围为以d为半径的圆。问海岸线上至少造多少雷达可以把所有的小岛都包含在内。注意雷达是建在海岸线上的,也就是x轴上的。

Sample Input

3 2
1 2
-3 1
2 1

1 2
0 2

0 0

Sample Output

Case 1: 2
Case 2: 1

题目分析
使用贪心,从左往右建立雷达,要尽可能多的覆盖岛屿。

AC代码

/*************************************************************************
    > File Name: POJ1328.cpp
    > Author: ljf-cnyali
    > Mail: ljfcnyali@gmail.com 
    > Created Time: 2017/3/18 13:34:16
 ************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<queue>

using namespace std;

#define REP(i, a, b) for(int i = (a), _end_ = (b);i <= _end_; ++ i)
#define mem(a) memset((a), 0, sizeof(a))
#define str(a) strlen(a)

const int maxn = 1010;

struct Island {
    int x, y;
} isl[maxn];

struct node {
    float sta, end;
} red[maxn];

bool cmp(node x, node y) {
    return x.end < y.end;
}

int main() {
    int n, d, t = 0;
    while(cin >> n >> d && n != 0) {
        cout << "Case " << ++ t << ": ";
        int max_isl = 0;
        REP(i, 0, n - 1) {
            scanf("%d%d", &isl[i].x, &isl[i].y);
            max_isl = max(max_isl, isl[i].y);
        }
        if(max_isl > d || d < 0) {
            cout << "-1" << endl;
            continue;
        }
        float len;
        REP(i, 0, n - 1) {
            len = sqrt(1.0 * d * d - isl[i].y * isl[i].y);
            red[i].sta = isl[i].x - len;
            red[i].end = isl[i].x + len;
        }
        sort(red, red + n, cmp);
        int ans = 0;
        bool vis[maxn];
        mem(vis);
        REP(i, 0, n - 1) 
            if(!vis[i]) {
                vis[i] = true;  
                REP(j, 0, n - 1)
                    if(!vis[j] && red[j].sta <= red[i].end)
                        vis[j] = true;
                ++ ans;
            }
        printf("%d\n", ans);
    }
    return 0;
}

本文转自:http://ljf-cnyali.cn/index.php/archives/66

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值