nyoj 12 喷水装置(二) 贪心 区间覆盖

来源:互联网 发布:宁夏隆盛软件下载 编辑:程序博客网 时间:2024/05/17 01:10

思路很简单 根据坐标和半径 得到每个喷水设置能够覆盖的左右坐标 

然后按左坐标从小到大排序 

筛选时候如果喷水设置的左坐标小于等于起始点star用while循环寻找能够覆盖最大的右坐标

然后更新起始点star为寻找到的最大右坐标

具体看代码:

#include <stdio.h>#include <math.h>#include <algorithm>using namespace std;struct node {double left,right;}c[10005];bool cmp(node x,node y)//左坐标排序{return x.left<y.left;}int main(){int t,n;double l,h,x,r,temp,star,p;scanf("%d",&t);while(t--){scanf("%d %lf %lf",&n,&l,&h);for(int i=0;i<n;i++){scanf("%lf %lf",&x,&r);if(r<=h/2)//如果半径小于高的一半  不能覆盖,temp=0;temp=0;elsetemp=sqrt(pow(r,2)-pow(h/2,2));c[i].left=x-temp,c[i].right=x+temp;}sort(c,c+n,cmp);int sum=0;star=0;for(int i=0;i<n;i++){if(c[i].left<=star)//找到第一个左坐标小于等于star的{        p=c[i].right;while(c[i].left<=star)//寻找最大的右坐标{p=max(p,c[i].right);i++;if(i==n)break;}star=p;//更新右坐标i--,sum++;}if(star>=l)//提前结束循环break;}if(star>=l)printf("%d\n",sum);elseprintf("0\n",star);}return 0;}


1 0
原创粉丝点击