poj1328 Radar Installation 贪心

4 篇文章 0 订阅
思路在代码的注释中
///2014.3.1 - 2014.3.2
///poj1328

/**
 *对要扫描到的每个岛求出雷达可以安置在海岸上的区间[begin,end],
 *对这些区间按begin从小到大排序
 *从第一个区间的end点开始安置雷达,然后依次检索下一个区间,
 *若下一个区间完全在刚刚安置雷达的区间内(new.begin>=pre.begin &&
 * new.end<=pre.end ),则将刚刚安置的雷达重新安置在这个区间(new)的右端;
 *若下一个区间的左端点在刚刚安置雷达的区间内,而右端点在刚刚安置雷达的
 *区间右侧,则可忽略这个区间向下进行;
 *若下一个区间与刚刚安置雷达的区间完全不相交,则在这个新区间右端内安置
 *一个新雷达。
 */

#include <cstdio>
#include <algorithm>
#include <cmath>

struct Section
{
    double begin;
    double end;
};
bool mycmp(Section a,Section b)
{
    return a.begin < b.begin;
}

int n; ///记录区间(小岛)的数目
int d; ///记录雷达的扫描半径
Section section[1010]; ///存储每个小岛需要的雷达区间

bool init( )
{
    bool legalData = true;
    int x,y;
    for(int i=0 ; i<n ; i++){
        scanf("%d%d",&x,&y);
        section[i].begin = x - sqrt(d*d-y*y);
        section[i].end = x + sqrt(d*d-y*y);
        if( y>d )
            legalData = false;
    }
    std::sort(section,section+n,mycmp);
    return legalData;
}

int main( )
{
//    freopen("in","r",stdin);
//    freopen("out","w",stdout);

    int cas = 1;
    while( scanf("%d%d",&n,&d)!=EOF && (n || d) ){
        if( !init() ){
            printf("Case %d: -1\n",cas);
            cas++;
            continue;
        }
        if( n==1 ){
            printf("Case %d: 1\n",cas);
            cas++;
            continue;
        }

        double pre = section[0].end;
        Section* now;
        int num = 1;
        for(int i=1 ; i<n ; i++){
            now = &(section[i]);
            if(  now->begin > pre ){
                num++;
                pre = now->end;
            }
            else if( now->end < pre ){
                pre = now->end;
            }
        }

        printf("Case %d: %d\n",cas,num);
        cas++;
     }
     return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值