zoukankan      html  css  js  c++  java
  • poj 1328

    http://poj.org/problem?id=1328

    经典贪心问题。

    首先题目要求岸边的雷达要覆盖海中的岛屿,什么情况下岛屿才会被覆盖呢?

    也就是要修建的雷达到岛屿的距离小于雷达的覆盖半径,这样就会想到以岛屿为

    圆心做圆,与岸边的交点就是可以覆盖这个岛屿的雷达的修建区间,我们确定好

    每个岛屿的雷达修建区间后,对这些区间从小到大排序,比较一下就可以了,要

    注意被覆盖的区间。

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    struct node
    {
        double l,r;
    }p[1005];
    int cmp(const void *a,const void *b)
    {
        return (*(node *)a).l>=(*(node *)b).l?1:-1;
    }
    int main()
    {
        int n,d,x,y,ok,i,cnt = 1,ans;
        //freopen("in.txt","r",stdin);
        while(scanf("%d %d",&n,&d) == 2)
        {
            if(!n && !d) break;
            ok = 1;
            for(i = 0;i < n;i ++)
            {
                scanf("%d %d",&x,&y);
                if(d >= y && ok)
                {
                    p[i].l=x - sqrt((double)(d*d - y*y));
                    p[i].r=x + sqrt((double)(d*d - y*y));
                }
                else ok = 0;
            }
            printf("Case %d: ",cnt++);
            if(!ok) printf("-1\n");
            else
            {
                ans = 1;
                qsort(p,n,sizeof(node),cmp);
                double tmp = p[0].r;
                for(i = 1;i < n;i ++)
                {
                    if(tmp < p[i].l)
                    {
                       ans ++;
                       tmp = p[i].r;
                    }
                    if(tmp > p[i].r)
                    tmp = p[i].r;
                }
                printf("%d\n",ans);
            }
        }
        return 0;
    }
    

      


  • 相关阅读:
    mysql(二)数据类型
    mysql(一)
    MySQL8 安装官方示例 employees 数据库
    Elasticsearch入门
    redis入门
    git 基本使用
    vue-cli4环境变量配置和代理跨域配置
    nginx配置
    彻底搞清浏览器和服务器跨域
    File文件过滤器
  • 原文地址:https://www.cnblogs.com/zhangteng512/p/2125856.html
Copyright © 2011-2022 走看看