NYOJ 喷水装置(一) (贪心)

来源:互联网 发布:photoshopcs6 for mac 编辑:程序博客网 时间:2024/05/17 12:49

喷水装置(一)

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
描述
现有一块草坪,长为20米,宽为2米,要在横中心线上放置半径为Ri的喷水装置,每个喷水装置的效果都会让以它为中心的半径为实数Ri(0<Ri<15)的圆被湿润,这有充足的喷水装置i(1<i<600)个,并且一定能把草坪全部湿润,你要做的是:选择尽量少的喷水装置,把整个草坪的全部湿润。
输入
第一行m表示有m组测试数据
每一组测试数据的第一行有一个整数数n,n表示共有n个喷水装置,随后的一行,有n个实数ri,ri表示该喷水装置能覆盖的圆的半径。
输出
输出所用装置的个数
样例输入
252 3.2 4 4.5 6 101 2 3 1 2 1.2 3 1.1 1 2
样例输出
25

贪心算法,将每个喷水装置的半径按照从大到小排序,在覆盖面积时需要注意的是圆覆盖的情况(并不是直接半径相加)

#include <cstdio>#include <cmath>#include <algorithm>using namespace std;const int maxn = 600 + 10;int t;int n;double r[maxn];double p[maxn];int cmp(double a, double b){    return a > b;}int main(){    scanf("%d", &t);    while (t--){        scanf("%d", &n);        for (int i = 0; i < n; i++){            scanf("%lf", &r[i]);            p[i] =(double)sqrt(r[i] * r[i] - 1);        }        sort(p, p + n, cmp);        int ans = 1;        double pos = 0;        for (int i = 0; i < n; i++){            pos += p[i] * 2;            if (pos < 20){                ans++;                continue;            }        }        printf("%d\n", ans);    }    return 0;}


0 0
原创粉丝点击