NYOJ12喷水装置(二)

//NYOJ12喷水装置(二)
//区间覆盖问题之完全覆盖;
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include <algorithm>
using namespace std;
typedef struct Node
{
	double x;
	double r;
	double begin;
	double end;
}Node;
Node a[10001];
int Count = 0;
void greedySelect(Node a[], int n, double w, double h);
bool cmp(Node a, Node b)//以开始下标排序,升序;
{
	return a.begin  < b.begin;
}
int main(void)
{
	int N, n;
	double w, h;
//	freopen("in.txt", "r", stdin);
	scanf("%d", &N);
	while(N--)
	{
		scanf("%d", &n);
		scanf("%lf%lf", &w, &h);
		for(int i = 0; i < n; i++)
		{
			scanf("%lf%lf", &a[i].x , &a[i].r);
			//计算区间长度;
			double length;
			if(a[i].r > h/ 2)
			{
				length = sqrt(a[i].r * a[i].r - (h / 2) * (h / 2));//length = 2 * sqrt(a[i].r * a[i].r - (h / 2) * (h / 2));
			}
			else
			{
				length = 0;
			}
			a[i].begin = a[i].x - length;
			a[i].end = a[i].x + length;
		}
		sort(a, a + n, cmp);
		Count = 0;
		greedySelect(a, n, w, h);
		printf("%d\n", Count);
	}
	return 0;
}
//区间覆盖,可以相交;(但这道题目还没用到,只是0 - n的遍历而已)
void greedySelect(Node a[], int n, double w, double h)
{
	double c = 0;
	int i = 0;
	bool flag = 0;
	while(c < w)
	{
		double max = 0;
		for(i = 0; i < n && a[i].begin <= c; i++)
		{
			double distance = a[i].end - c; 
			if(distance >= max)
			{
				max = distance;
			}
		}
		//剪枝;
		if(max == 0)//只是在这里加了一句就不超时了;
		{
			flag = 1;
			break;
		}
		c = c + max;
		Count++;
	}	
	if(flag == 1)
		Count = 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值