[HDU3847][凸包] WF2011K:trash removal

版权声明:虽然博主很菜,但是还是请注明出处(我觉得应该没人偷我的博客) https://blog.csdn.net/qq_43346903/article/details/87933304

HDU3847

这是WF?
求个凸包,枚举每条边,再枚举每个点算一下和枚举的边的距离然后取个max,再把max和ans取个min

Code:

#include<bits/stdc++.h>
#define db double
using namespace std;
inline int read(){
	int res=0,f=1;char ch=getchar();
	while(!isdigit(ch)) {if(ch=='-') f=-f;ch=getchar();}
	while(isdigit(ch)) {res=(res<<1)+(res<<3)+(ch^48);ch=getchar();}
	return res*f;
}
const int N=105;
struct point{
	db x,y;
	point(){}
	point(db _x,db _y): x(_x),y(_y){}
	friend inline point operator - (const point &a,const point &b) {return point(a.x-b.x,a.y-b.y);}
	friend inline db operator * (const point &a,const point &b) {return a.x*b.y-a.y*b.x;}
	inline db dis() {return sqrt(x*x+y*y);}
}p[N],q[N];
int n,tot=0;
inline db calc(point a,point b,point p){
	point v1=b-a,v2=p-a;
	return fabs(v1*v2/v1.dis());	
}
inline bool cmp(const point &a,const point &b){
	int det=(a-p[1])*(b-p[1]);
	if(det) return det<0;
	return (a-p[1]).dis()<(b-p[1]).dis();
}
inline void graham(){
	int id=1;
	for(int i=2;i<=n;i++) if(p[i].x<p[id].x || (p[i].x==p[id].x && p[i].y<p[id].y)) id=i;
	swap(p[1],p[id]);
	sort(p+2,p+n+1,cmp);
	q[++tot]=p[1];q[++tot]=p[2];
	for(int i=3;i<=n;i++){
		while(tot>1 && (p[i]-q[tot-1])*(q[tot]-q[tot-1])<=0) --tot;
		q[++tot]=p[i];
	}
	q[++tot]=q[1];
}
int main(){
	int t=1;
	while(1){
		n=read();tot=0;
		if(!n) return 0;
		for(int i=1;i<=n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
		graham();
		db ans=1e20;
		for(int i=1;i<tot;i++){
			db res=0.0;
			for(int j=1;j<tot;j++) res=max(res,calc(q[i+1],q[i],q[j]));
			ans=min(ans,res);
		}
		ans=ceil(ans*100)/100.0;
		printf("Case %d: %.2lf\n",t++,ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43346903/article/details/87933304