zoukankan      html  css  js  c++  java
  • fzuoj1111Radar Installation (贪心)

    题目大意是在海岸线布置n个雷达,要求雷达的范围要包含所有的小岛;

    思路:逆向思维把小岛看成一个个范围,与海岸线的交集,从最左端的开始找 (贪心最左端的点),接着不用一个一个去遍历,直接用前一个的右端点去替换下一个的左端点。。。。直至最后一个点。大致思想就是贪心,还是比较正常的题,适合刚学c语言的新生做(小白我就是一枚)。

    下面是代码:

     1 #include <iostream>
     2 #include <cmath>
     3 #include <algorithm>
     4 #include <cstdio>
     5 using namespace std;
     6 struct node
     7 {
     8     double left,right;
     9 }island[1001];
    10 bool cmp(node a,node b)
    11 {
    12     return a.left < b.left;
    13 }
    14 int main()
    15 {
    16     double x,y,d,temp;
    17     int i,cnt,n,k=0;
    18     bool flag;
    19     while(scanf("%d%lf",&n,&d)&&!(n==0&&d==0))
    20     {
    21         k++;
    22         flag=false;
    23         for(i=0; i<n; i++)
    24         {
    25             scanf("%lf%lf",&x,&y);
    26             if(y > d||d<0)
    27             {
    28                 flag = true;
    29             }
    30             island[i].right = x+sqrt(d*d-y*y);///岛屿右端点初始化
    31             island[i].left = x-sqrt(d*d-y*y);///岛屿左端点初始化
    32         }
    33         if(flag)
    34         {
    35             printf("Case %d: -1\n",k);
    36             continue;
    37         }
    38         sort(island,island+n,cmp);
    39         temp=island[0].right;
    40         cnt=1;
    41         for(i=1; i<n; i++)
    42         {
    43             if(island[i].right <= temp)
    44             {
    45                 temp = island[i].right;///岛屿右端点的替换
    46             }
    47             else if(island[i].left > temp)
    48             {
    49                 cnt++;
    50                 temp = island[i].right;
    51             }
    52         }
    53         printf("Case %d: %d\n",k,cnt);
    54     }
    55     return 0;
    56 }
  • 相关阅读:
    centos7 源码安装python3
    gitlab 迁移
    gitlab 搭建
    亚伦史沃茨 公开信
    误删了sudo包怎么办
    在中国历史上,我个人最喜欢的三个皇帝
    安装PHP7
    Mac OS上设置Django开发环境
    The Pragmatic Programmer Quick Reference Guide
    Nginx的第一个模块-HelloWorld
  • 原文地址:https://www.cnblogs.com/guyahan/p/5450961.html
Copyright © 2011-2022 走看看