• Watering Grass (贪心,最小覆盖)


    参考:  

    https://blog.csdn.net/shuangde800/article/details/7828675

    https://www.cnblogs.com/haoabcd2010/p/6171794.html?utm_source=itdadao&utm_medium=referral

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 
     7 
     8 using namespace std;
     9 
    10 struct node
    11 {
    12     double L;
    13     double R; 
    14 }a[10010];
    15 
    16 bool cmp(node a, node b)
    17 {
    18     return a.R > b.R;
    19 }
    20 
    21 void sort_node(int n)
    22 {
    23     for (int i=0;i<n;i++)
    24     {
    25         int k=i;
    26         for (int j=i+1;j<n;j++)
    27         {
    28             if (a[j].R>a[k].R)
    29                 k=j;
    30         }
    31         swap(a[k],a[i]);
    32     }
    33 }
    34 
    35 int main() 
    36 {
    37     int n;
    38     double length, wide;    // 都定义为double类型,因为使用勾股定理的时候肯定会算出小数
    39     double center, radius;     // center为圆心,radius为半径 
    40     double t;
    41     while(scanf("%d%lf%lf", &n, &length, &wide) != EOF)
    42     {
    43         for(int i = 0; i < n; ++i)
    44         {
    45             cin >> center >> radius;
    46             t = sqrt(radius*radius - (wide/2.0)*(wide/2.0));
    47             a[i].L = center - t;
    48             a[i].R = center + t;    
    49         }
    50         
    51         //sort(a, a+n, cmp);    快排不知道为什么不能AC 
    52         sort_node(n);
    53         
    54         double border = 0;
    55         int cnt = 0;
    56         while(border < length)  // 此类型题目的固定套路
    57         {
    58             int i;
    59             for(i = 0; i < n; ++i)
    60             {
    61                 if(a[i].L <= border && a[i].R > border)
    62                 {
    63                     border = a[i].R;
    64                     cnt++;
    65                     break;
    66                 }
    67             }
    68             
    69             if(i == n)    // 如果i==n,说明遍历完了这n个区间,仍然没有找到符合条件的,即不能完全覆盖 
    70                 break;
    71         }
    72         
    73         if(border < length)
    74             cout << -1 << endl;
    75         else
    76             cout << cnt << endl;
    77     } 
    78             
    79     return 0;
    80 }
  • 相关阅读:
    密码学
    MD5
    计算机基础之操作系统
    python中列表之间求差集、交集、并集
    Python语言中各种进制相互转换
    计算机基础
    bzoj2705 [SDOI2012]Longge的问题
    bzoj3160 万径人踪灭
    codeforces 528D Fuzzy Search
    杜教筛 && bzoj3944 Sum
  • 原文地址:https://www.cnblogs.com/FengZeng666/p/11120490.html
Copyright © 2020-2023  润新知