喷水装置(一)(贪心)

喷水装置(一)

时间限制:3000 ms  |  内存限制:65535 KB

难度:3

描述

现有一块草坪,长为20米,宽为2米,要在横中心线上放置半径为Ri的喷水装置,每个喷水装置的效果都会让以它为中心的半径为实数Ri(0<Ri<15)的圆被湿润,这有充足的喷水装置i(1<i<600)个,并且一定能把草坪全部湿润,你要做的是:选择尽量少的喷水装置,把整个草坪的全部湿润。

输入

第一行m表示有m组测试数据
每一组测试数据的第一行有一个整数数n,n表示共有n个喷水装置,随后的一行,有n个实数ri,ri表示该喷水装置能覆盖的圆的半径。

输出

输出所用装置的个数

样例输入

2
5
2 3.2 4 4.5 6 
10
1 2 3 1 2 1.2 3 1.1 1 2

样例输出

2
5

之前错误的想法直接用直径加起来就好了,画了图可以看到实际上不能完全覆盖 如图

自制图(图画的不是很准):

AC代码:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//double cmp(const void*a,const void*b){  本来想快排的结果发现学校的编译器一直报错就用个冒泡了 
  //  return *(double *)a-*(double *)b;    
//}
int main(){
    int T=0;
    scanf("%d",&T);
    while(T--){
       int n=0;
       scanf("%d",&n);
       int i=0;
       double ps[610];
       for(i=0;i<n;i++){
          scanf("%lf",&ps[i]);
       }
       for(i=0;i<n-1;i++){//从大到小排序 
           for(int j=0;j<n-i-1;j++){
               if(ps[j]<ps[j+1]){
                   double temp=ps[j];
                   ps[j]=ps[j+1];
                   ps[j+1]=temp;
               }
           }
       }
       i=0;
       double len=20.0;
       while(len>0 && ps[i]>1 && i<n){//同时满足条件剩下的长度>0  宽为2要放中间 半径要大于1否则没有意义      
            len=len-(sqrt(ps[i]*ps[i]-1.0)*2);
            i++;
       }
       printf("%d\n",i);
               
    }
    
        
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40632760/article/details/80388514