喷水装置(一) 贪心


描述 现有一块草坪,长为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


分析:


要想使得使用喷头使用最少,那就需要先尽可能的使用辐射范围比较大的喷头,那么覆盖的范围就大
先将半径从大到小排序。由图可知:
草坪的长度为20,要想全部将草坪全部湿润,那么就要将辐射范围在水平方向上的 数累加,当大于20的时候,就全部湿润,如l1*2就是半径为r1的喷头所能在水平方向上喷射的最远距离


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <string>
using namespace std;
int cmp(double a,double b)
{
	return a > b;
}
double Tri(double a,double b)
{
   return sqrt(a*a-b*b);
}
int main()
{
    int m;
    cin >> m;
    while(m--)
    {
      int n;
      cin >> n;
      vector<double>v(n);
      double tmp;
      for(int i = 0;i < n;i++)
      {
        cin >> tmp;
        v.push_back(tmp);
      }
      sort(v.begin(),v.end(),cmp);
      double sum = 0;
      for(int i = 0;i < v.size();i++)
      {
      	sum += 2.0*Tri(v[i],1.0);
      	if(sum > 20)
      	{
      		cout << i+1 << endl;
      		break;
      	}
      }
    }
	return 0;
}












猜你喜欢

转载自blog.csdn.net/lidafoye/article/details/77622766