【贪心】Highway

[UVa1615]Highway

算法竞赛入门经典第8章8-11(P255)

题目大意:给定平面上N个点和D,要求在x轴上选出一些点,每个给定的点至少与一个选出的点欧几里得距离<=D

试题分析:对于每个点,我们只需要以其为圆心做半径为D的圆,求其与x轴的两个交点,确定这个点的一条线段,然后进行线段覆盖形贪心即可。

代码:

 

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define LL long long
using namespace std;

inline int read(){
	int x=0,f=1;char c=getchar();
	for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
	for(;isdigit(c);c=getchar()) x=x*10+c-'0';
	return x*f;
}
const int INF = 999999;
const int MAXN = 100001;
int L,D; int N;
struct data{
	int x,y;
	double lx,ly;
}a[MAXN+1];

bool cmp(data a,data b){
	return a.ly<b.ly;
}

int main(){
	while(scanf("%d%d",&L,&D)!=EOF){
		N=read();
		for(int i=1;i<=N;i++){
			a[i].x=read(),a[i].y=read();
		     a[i].lx=(double)a[i].x-sqrt((double)D*(double)D-(double)a[i].y*(double)a[i].y);
             a[i].ly=(double)a[i].x+sqrt((double)D*(double)D-(double)a[i].y*(double)a[i].y);
             a[i].lx=max(a[i].lx,0.0);
             a[i].ly=min(a[i].ly,(double)L);
		}
		sort(a+1,a+N+1,cmp);
		int tmp=-1,ans=0;
		for(int i=1;i<=N;i++){
			if(tmp<a[i].lx){
				tmp=a[i].ly; ans++;
			}
		}
		printf("%d\n",ans);
	}
}

 

posted @ 2017-09-18 17:17  wxjor  阅读(204)  评论(0编辑  收藏  举报