POJ 1328 Radar Installation 贪心算法

Posted 一米阳光213

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 1328 Radar Installation 贪心算法相关的知识,希望对你有一定的参考价值。

代码如下:

// 贪心算法

#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;

// 包含左右端点
struct interval
	double left;
	double right;
;
interval intervals[1002];
double d;
int n;

bool cmp(interval&a, interval&b)
	return a.left < b.left;


int main(int argc, char* argv[])

	int case_num = 0;
	while (scanf("%d%lf", &n, &d) && (n||d) )
		case_num++;
		bool has_solution = true;
		for (int i = 0; i < n; i++)
			double x, y;
			scanf("%lf%lf", &x, &y);
			if (fabs(y) > d) 
				has_solution = false;
			else
				intervals[i].left = x - sqrt(d*d - y*y);
				intervals[i].right = x + sqrt(d*d - y*y);
			
		
		if (has_solution == false)
		
			cout << "Case " << case_num << ": " << -1 << endl;
			continue;
		
		// 对所有区间进行排序
		sort(intervals, intervals + n, cmp);

		// 
		int num_radars = 1;
		double cur_radar = intervals[0].right;
		for (int i = 0; i < n; i++)
			if (intervals[i].left <= cur_radar)
			
				if (intervals[i].right < cur_radar)
					cur_radar = intervals[i].right;
			
			else
			
				num_radars++;
				cur_radar = intervals[i].right;
			
		

		cout << "Case " << case_num << ": " << num_radars << endl;
	
	return 0;


 

以上是关于POJ 1328 Radar Installation 贪心算法的主要内容,如果未能解决你的问题,请参考以下文章

poj 1328 Radar Installation

poj1328 Radar Installation

POJ1328 Radar Installation

[POJ1328] Radar Installation

Radar Installation POJ - 1328

poj1328Radar Installation