*【UVA - 10382】Watering Grass(贪心,区间覆盖问题,思维)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/84838820

题干:

题目大意:

有一块草坪,长为l,宽为w,在它的水平中心线上有n个位置可以安装喷水装置,各个位置上的喷水装置的覆盖范围为以它们自己的半径ri为圆。求出最少需要的喷水装置个数,如果无论如何都不能覆盖,就输出-1。

解题报告:

   这题就是个区间覆盖问题的变形,,虽然给的是一个个的圆,但是我们不难发现求出与上下边的交点,这一部分区域才是我们的有效区域,然后求个区间覆盖就行了、、、nlogn的算法按说不应该TLE啊,,,但是该优化的都优化了还是TLE,看了题解发现有个剪枝,,(但是说实话这个题卡时间没必要吧、、TLE变0ms)

   emmm今天又想了一下好像不是TLE的问题,,这样会WA吧、、因为本来可能覆盖不到的地方你都变成覆盖得到了、、你求边界那里就不对、、对一个负数去开平方根???可能这样会认为是TLE吧、、

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
struct Node {
	double st,ed;
	Node(){}
	Node(double st,double ed):st(st),ed(ed){}
	bool operator<(const Node & b) const{
		if(st != b.st) return st < b.st;
		return ed > b.ed;
	}
} node[MAX];
int tot,cnt;
int main()
{
	int n;
	double l,w,x,r;
	while(~scanf("%d%lf%lf",&n,&l,&w)) {
		tot=cnt=0;
		for(int i = 1; i<=n; i++) {
			scanf("%lf %lf",&x,&r);
			if(r < w/2) continue;
			//cin>>x>>r;
			//cout << x<< r << endl;
			node[++tot] = Node(x-sqrt(r*r-w*w/4),x+sqrt(r*r-w*w/4));
			//cout << x-sqrt(r*r-w*w/4) << endl;
		}
		sort(node+1,node+tot+1);
		//for(int i = 1; i<=tot; i++) printf("%f  %f\n",node[i].st,node[i].ed);
		double cure,curs;
		curs=cure=0;
		int flag = 0;
		for(int i = 1; i<=tot; ) {
			if(node[i].st > curs) {
				break;	
			}
			while(i<=tot && node[i].st<=curs) {
				if(node[i].ed > cure) {
					cure = node[i].ed;
				}
				i++;
			}
			cnt++;
			curs = cure;
			if(curs >= l) {
				flag=1;break;
			}
		}
		if(flag == 0) puts("-1");
		else printf("%d\n",cnt);
	}
	return 0 ;
 } 

还有一个没有排序的算法、这样写就不需要加剪枝了。,。但是不知道为什么这样可以。

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/84838820