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

    VonGang原创,如有错误,欢迎指正。转载请注明:http://www.cnblogs.com/vongang/

       

       

    如图,先求出每一个岛屿所对应的雷达区间,让后利用贪心思想即可,类是会场布置问题。(注意:c语言qsort函数对浮点操作时可能有点误差,本人因为这个贡献出无数WA。。。

       

     

    #include <stdio.h>
    #include
    <stdlib.h>
    #include
    <math.h>
    #define N 1002
    struct node
    {
    double s;
    double e;
    }num[N];

    int cmp(const void * a, const void * b)
    {
    return (*(struct node *)a).s >= (*(struct node *)b).s ? 1 : -1; //特别注意!!!
    }

    int main()
    {
    //freopen("data.in", "r", stdin);
    int n, i, cas = 0;
    int x, y, r;
    while(scanf("%d%d", &n, &r)!=EOF)
    {
    if(n == 0 && r == 0) break;
    int flag = 0;
    for(i = 0; i < n; i++)
    {
    scanf(
    "%d%d", &x, &y);
    if(y > r || y < 0) flag = 1;
    else
    {
    num[i].s
    = x - sqrt((double)(r*r - y*y));
    num[i].e
    = x + sqrt((double)(r*r - y*y));
    }
    }
    if(r < 0 || flag) { printf("Case %d: -1\n", ++cas); continue;}
    qsort(num, n,
    sizeof(struct node), cmp);
    double tmp = num[0].e;
    int count = 1;
    for(i = 1; i < n; i++)
    {
    if(tmp < num[i].s)
    {
    count
    ++;
    tmp
    = num[i].e;
    }
    else
    {
    if(tmp > num[i].e) tmp = num[i].e;
    }
    }
    printf(
    "Case %d: %d\n", ++cas, count);
    }
    return 0;
    }
  • 相关阅读:
    SQLServer 可疑
    String与Long互转
    洛谷 P5644
    洛谷 P3783
    洛谷 P4663
    洛谷 P3438
    Atcoder Grand Contest 054 题解
    迭代器失效问题
    Solution -「CF 232E」Quick Tortoise
    Solution -「NOI 2020」「洛谷 P6776」超现实树
  • 原文地址:https://www.cnblogs.com/vongang/p/2145057.html
Copyright © 2011-2022 走看看