您的位置:首页 > 其它

NYOJ 题目6 喷水装置(一) 水贪心

2016-03-25 16:55 309 查看
半径1或者以下的喷水装置毫无用处,因为题目是要求在一个矩形的两条长为20米的边的中位线上装喷水器,而且要求全部覆盖,且宽为2米。

按半径大小从大到小排序,之后进行贪心。


喷水装置(一)

时间限制: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


来源[苗栋栋]原创
上传者苗栋栋

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int INF =0x3f3f3f3f;
const int maxn= 600   ;
const int L=20;
const int mR=1;
int n;
double a[maxn+5];

bool cmp(double x,double y)
{
return x>y;
}
int main()
{
int T;scanf("%d",&T);
double x;
while(T--)
{
scanf("%d",&n);
int cnt=0;
for(int i=1;i<=n;i++)
{
scanf("%lf",&x);
if(x<=mR)  continue;
a[++cnt]=2*sqrt(x*x-1)  ;
}
sort(a+1,a+1+cnt,cmp);
double now=0;int ans=0;
for(int i=1;now<L&&i<=cnt;i++)
{
now+=a[i];
ans++;
}
printf("%d\n",ans);

}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: