NYOJ12,喷水装置(二)

来源:互联网 发布:淘宝装修素材 编辑:程序博客网 时间:2024/04/30 01:37

     原题链接:点击打开链接

     这是一个贪心算法中的区间完全覆盖问题,详细解题思路点击打开链接

     不同的是在化为区间完全覆盖问题之前需要先对其有效喷水面积进行筛选,再化为区间,然后问题就明朗化了,下面直接贴代码:

 #include <iostream>#include <stdio.h>#include <algorithm>#include <math.h>using namespace std;struct sb{double from,to;}a[10005];bool cmp(sb t1,sb t2){if(t1.from<t2.from)return true;if(t1.from==t2.from&&t1.to>t2.to)return true;return false;}int main(){int t,n,i;double d,h,r,w,x;cin>>t;while(t--){int count=0,flag=0,sum=0;double begin=0,end=0;cin>>n>>w>>h;for(i=0;i<n;i++){cin>>x>>r;d=sqrt(r*r-((h/2)*(h/2)));if(r>=(h/2)){if(x-d>w||x+d<0)continue;a[count].from=x-d;a[count].to=x+d;count++;}}if(count==0){cout<<0<<endl;continue;}std::sort(a,a+count,cmp);if(a[0].from>0){cout<<0<<endl;continue;}double max=0;int num;for(i=0;i<count;i++){if(a[i].from<=end){for(int num=i;num<count;num++){if(a[num].to>max&&a[num].to>end&&a[num].from<=end){max=a[num].to;i=num;}}end=max;sum++;}if(end>w)break;}if(end<w||begin==end){cout<<0<<endl;continue;}cout<<sum<<endl;}return 0;}                


原创粉丝点击