zoukankan      html  css  js  c++  java
  • Watering Grass(贪心)

    Watering Grass

     n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each sprinkler is installed at the horizontal center line of the strip. For each sprinkler we are given its position as the distance from the left end of the center line and its radius of operation. What is the minimum number of sprinklers to turn on in order to water the entire strip of grass?

    Input

    Input consists of a number of cases. The first line for each case contains integer numbers n, l and w with n ≤ 10000. The next n lines contain two integers giving the position of a sprinkler and its radius of operation. (The picture above illustrates the first case from the sample input.)

     

    Output

    For each test case output the minimum number of sprinklers needed to water the entire strip of grass. If it is impossible to water the entire strip output ‘-1’.

     

    Sample Input

    8 20 2

    5 3

    4 1

    1 2

    7 2

    10 2

    13 3

    16 2

    19 4

    3 10 1

    3 5

    9 3

    6 1

    3 10 1

    5 3

    1 1

    9 1

    Sample Output

    6

    2

    -1

    //意思是有 n 个喷水的,草地是长为 l 宽为 w 然后是 n 个喷水的东西,坐标和喷洒半径 p, r

    首先要解决的问题是,如何表示一个喷洒的作用范围

    L = p - sqrt ( r * r - w * w / 4 )

    R = p - sqrt ( r * r - w * w / 4 )

    实际完全覆盖了这个区间的草地

    然后,按完全覆盖的最右距离递减排序,然后贪心,每次选出可以向右覆盖的最远喷洒即可

    130ms

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cmath>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 struct Node
     8 {
     9     double l,r;
    10     /*
    11     friend bool operator < (const Node&a,const Node&b)
    12     {
    13         return a.r > b.r;
    14     }
    15     */
    16 
    17 }node[10005];
    18 
    19 void sort_node(int n)
    20 {
    21     for (int i=0;i<n;i++)
    22     {
    23         int k=i;
    24         for (int j=i+1;j<n;j++)
    25         {
    26             if (node[j].r>node[k].r)
    27                 k=j;
    28         }
    29         swap(node[k],node[i]);
    30     }
    31 }
    32 
    33 int main()
    34 {
    35     int n;
    36     while (cin>>n)
    37     {
    38         double L,W;
    39         cin>>L>>W;
    40         int i;
    41         for (i=0;i<n;i++)
    42         {
    43             double p,r;
    44             cin>>p>>r;
    45             node[i].l=p-sqrt(r*r-W*W/4);
    46             node[i].r=p+sqrt(r*r-W*W/4);
    47         }
    48         //sort(node,node+n);//不知道为什么快排就是错的
    49         sort_node(n);
    50         double k=0;
    51         int num=0;
    52         while(k<L)
    53         {
    54             for (i=0;i<n;i++)
    55             {
    56                 if (node[i].l<=k&&node[i].r>k)
    57                 {
    58                     k=node[i].r;
    59                     num++;
    60                     break;
    61                 }
    62             }
    63             if (i==n) break;
    64         }
    65         if (k<L) cout<<"-1"<<endl;
    66         else cout<<num<<endl;
    67     }
    68     return 0;
    69 }
    View Code
  • 相关阅读:
    第二部分 设计类型:第8章 方法
    centos7 网卡重命名
    centos7修改主机名
    修改umask值
    mysql表字段属性
    mysql基础操作
    mysql错误代码ERROR 1045 (转载)
    sed高级用法
    shell拷贝原文件到目标对应文件夹
    函数(位置传参)
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/6171794.html
Copyright © 2011-2022 走看看