zoukankan      html  css  js  c++  java
  • UVA 10382.Watering Grass-贪心

    10382 - Watering Grass

    Time limit: 3.000 seconds

    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

    题意就是浇水,整片草地都要浇上水,装置在草地水平中间的线上,要注意的是不是圆的面积,是近似矩形的面积。

    然后就是直接算长度就可以,要求装置洒水的半径要>草地一半的宽度。

    然后长度的话也不是圆的半径,是图上红色的长度。(图画大了,挠头。。。)

    关键就是左右两边的长度计算,精度很重要,打训练赛的时候就是卡死在精度这里,wa了8发,gg。

    a[i].l=max(0.0,w-sqrt((double)r*r-(double)s*s/4.0));
    a[i].r=min((double)l,w+sqrt((double)r*r-(double)s*s/4.0));

    就是这个,还是一个大佬帮我改对的。

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e5+10;
    struct node{
        double l,r;
    }a[N];
    double cmp(node a,node b){
        return a.r>b.r;
    }
    int main(){
        int n,num,l,s,w,r;
        double p;
        while(~scanf("%d%d%d",&n,&l,&s)){
                p=0.0;num=0;
            for(int i=0;i<n;i++){
                scanf("%d%d",&w,&r);
                if(r>=s/2.0){
                a[i].l=max(0.0,w-sqrt((double)r*r-(double)s*s/4.0));                               //wa在这里了。
                a[i].r=min((double)l,w+sqrt((double)r*r-(double)s*s/4.0));}
            }
            sort(a,a+n,cmp);
            //for(int i=0;i<n;i++) cout<<a[i].l<<" "<<a[i].r<<endl;
            while(p<l){
                int i;
                for(i=0;i<n;i++){
                if(a[i].l<=p&&a[i].r>p){
                    p=a[i].r;
                    num++;
                    break;
                }
                }
                if(i==n)break;
            }
            if(p<l) printf("-1
    ");
            else printf("%d
    ",num);
        }
        return 0;
    }

    网上好多代码都是错的,卡精度上了。

    ==

    然后就是NYOJ上有一道贪心长得差不多,但是比上面的水。。。

    NYOJ 6.喷水装置(一)-贪心

     

     

    
    
  • 相关阅读:
    手机端布局
    雪碧图优缺点
    es6的基本数据详解
    react生命周期函数
    第七周作业-使用Python实现抽样分布的验证(正态分布、卡方分布、T分布等)
    第六章统计量及其抽样分布
    Python实现概率分布(二项分布、伯努利分布、泊松分布、几何分布、正态分布等)
    4.概率与概率分布
    3.描述性统计
    统计学小组
  • 原文地址:https://www.cnblogs.com/ZERO-/p/7211486.html
Copyright © 2011-2022 走看看