nyoj+区间覆盖贪心

点击打开链接

///根据坐标和半径 得到每个喷水设置能够覆盖的左右坐标
///然后按左坐标从小到大排序
///筛选时候如果喷水设置的左坐标小于等于起始点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 N,n;
	double w,h,x,r,temp,star,p;
	scanf("%d",&N);
	while(N--){
		scanf("%d %lf %lf",&n,&w,&h);
		for(int i=0;i<n;i++){
			scanf("%lf %lf",&x,&r);
			if(r<=h/2)///如果半径小于高的一半  不能覆盖,temp=0;
			temp=0;
			else
			temp=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);///合理的右边最大,满足情况下的最长的区间
					if(i==n) break;
					i++;
					}
				star=p;///更新右坐标,新的起点
				i--,sum++;///在刚刚的while循环i++了
			}
			if(star>=w)///提前结束循环
			break;
		}
		if(star>=w)
		printf("%d\n",sum);
		else
		printf("0\n");
	}
	return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值