原题链接:题目12 - ACM在线评测系统
时间限制:3000 ms | 内存限制:65535 KB
难度:4
描述
有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的喷水装置,每个喷水装置i喷水的效果是让以它为中心半径为Ri的圆都被润湿。请在给出的喷水装置中选择尽量少的喷水装置,把整个草坪全部润湿。
输入
第一行输入一个正整数N表示共有n次测试数据。
每一组测试数据的第一行有三个整数n,w,h,n表示共有n个喷水装置,w表示草坪的横向长度,h表示草坪的纵向长度。
随后的n行,都有两个整数xi和ri,xi表示第i个喷水装置的的横坐标(最左边为0),ri表示该喷水装置能覆盖的圆的半径。
输出
每组测试数据输出一个正整数,表示共需要多少个喷水装置,每个输出单独占一行。
如果不存在一种能够把整个草坪湿润的方案,请输出0。
样例输入
2
2 8 6
1 1
4 5
2 10 6
4 5
6 5
样例输出
1
2
解题思路:
若能从纵向覆盖,在表中记录其横向覆盖的起点和终点;然后遍历能从纵向覆盖草坪的喷水装置表,当横向起点接近已覆盖的终点,找到能从横向覆盖更远的喷水装置。
注意事项:
草坪的橫向中心线上不同位置处装有n(n<=10000)个点状的喷水装置,C可以定义数组存放喷水装置的覆盖信息,C++的STL用vector也不错。
参考代码:
用静态链表记录每个喷水装置可覆盖草坪的起点和终点
#include<stdio.h>
#include<math.h>
typedef struct z{
double a;
double b;
int x;
} v;
int main(){
int N,n,a,b,f,g;
double w,h,c,d,e,x,r;
v y[10001];
scanf("%d",&N);
while(N--){
scanf("%d%lf%lf",&n,&w,&h);
y[0].x=1;//表头指向第一个元素
for(b=1,a=0;a<n;a++){
scanf("%lf%lf",&x,&r);
c=h/2;
if(r>c){//若能从纵向覆盖
c=sqrt(r*r-c*c);
y[b].a=x-c;//横向覆盖起点
y[b].b=x+c;//横向覆盖终点
y[b].x=b+1;//指向静态链表下一元素
b++;
}
}
y[b-1].x=0;//表尾
c=.0;f=0;
while(c<w){
d=.0;
for(a=0;y[a].x;){//遍历能从纵向覆盖草坪的喷水装置表
g=y[a].x;
if(y[g].a-c<1e-9){//横向起点接近已覆盖的终点
e=y[g].b-c;
if(e>d)d=e;//从横向覆盖更远
y[a].x=y[g].x;
continue;
}
a=g;
}
if(d>.0)f++;
else break;
c+=d;
}
if(c<w)puts("0");
else printf("%d\n",f);
}
}用二维数组记录
/*
查看代码---运行号:35709----结果:Accepted
运行时间:2011-04-27 15:56:59 | 运行人:dianxiangan32
*/
#include<math.h>
#include<stdio.h>
int main(void){
int N,o,n,i,j,counter;
float w,h,x[1000][2],p,q,zuo,you,max;
scanf("%d",&N);
for(o=1;o<=N;o++){
for(i=0;i<1000;i++){
x[i][0]=0;
x[i][1]=0;
}
scanf("%d %f %f",&n,&w,&h);
for(i=1;i<=n;i++){
scanf("%f %f",&p,&q);
if(q*q-h*h/4>0){
x[i][0]=p-sqrt(q*q-h*h/4);
x[i][1]=p+sqrt(q*q-h*h/4);
}
}
zuo=0;you=w;counter=0;
while(zuo<you){
max=0;
for(i=1;i<=n;i++){
if(x[i][0]<=zuo&&x[i][1]>=zuo&&x[i][1]>max){
max=x[i][1];
}
}
if(max==zuo){
counter=0;
break;
}
zuo=max;
counter++;
}
printf("%d\n",counter);
}
return 0;
}C++用vector记录
/*
题目12优秀代码--运行号:78
运行时间:2010-09-19 21:59:09 | 编程语言:C/C++ | 运行人:张云聪
*/
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
double r,x,y,width,height;
class Circle
{
public:
Circle(double x,double r):x(x),r(r){}
double Left() const{if(r*r-height*height/4<0) return x;return x-sqrt(r*r-(height*height/4));}
double Right() const{if(r*r-height*height/4<0) return x;return x+sqrt(r*r-(height*height/4));}
friend ostream & operator<<(ostream& out,const Circle& circle);
private:
double x,r;
};
ostream & operator<<(ostream& out,const Circle& circle)
{
out<<circle.x<<" "<<circle.r;
return out;
}
struct CircleLess
{
bool operator()(const Circle& c1,const Circle& c2)
{
return c1.Right()<c2.Right();
}
};
int main()
{
int m;
cin>>m;
while(m--)
{
int n;
vector<Circle> Rs;
cin>>n>>width>>height;
Rs.reserve(n);
while(n--)
{
cin>>x>>r;
Rs.push_back(Circle(x,r));
}
sort(Rs.begin(),Rs.end(),CircleLess());
vector<int> choosed;
choosed.push_back(-1);
double len=0;
try{
while(len<width)
{
int last=-1;
for(vector<Circle>::size_type i=choosed.back()+1;i!=Rs.size();i++)
{
if (Rs[i].Left()<=len)
{
last=i;
}
}
if (last==-1) throw -1;
else
{
choosed.push_back(last);
len=Rs[last].Right();
}
}
cout<<choosed.size()-1<<endl;
}catch(...)
{
cout<<0<<endl;
}
}
}0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复