AcWing 2785. 信号增幅仪

题意

https://www.acwing.com/problem/content/description/2787/

给你一个椭圆的 倾斜角 和 短半轴与长半轴的比值 问你将n个点全部覆盖的最小短半轴椭圆是多少

思路

根据椭圆的标准方程

考虑将坐标压缩 已知 P = a / b , x' = x / P,y' = y  满足 x'² + y'² = 1;

所以坐标先顺时针旋转后再压缩就变成了一个最小圆覆盖问题!

代码

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>

using namespace std;
const double eps = 1e-12;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int MaxN = 5e5 + 5;

int n;
struct Point{
	double x,y;
	Point(double x = 0,double y = 0) : x(x),y(y) {}
}p[MaxN];
typedef Point Vector;

struct Circle{
	Point C;
	double r;
};

int dcmp(double x){//equal ?= 0
	if(fabs(x) < eps) return 0;
	else return x < 0 ? -1:1;
}

//运算符重载
Vector operator + (Vector A, Vector B){ return Vector(A.x+B.x, A.y+B.y); }
Vector operator - (Vector A, Vector B){ return Vector(A.x-B.x, A.y-B.y); }
Vector operator * (Vector A, double p){ return Vector(A.x*p, A.y*p); }
Vector operator / (Vector A, double p){ return Vector(A.x/p, A.y/p); }
bool operator == (const Point &a, const Point &b){
	return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
}

double Dot(Vector A, Vector B){ return A.x*B.x + A.y*B.y; }
double Cross(Vector A, Vector B){ return A.x*B.y - A.y*B.x; }

double dis(Point A,Point B){
	return sqrt((A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y));
}

//两参数方程求交点,(点,向量,点,向量)
Point Getlinenode(Point P,Point v,Point Q,Point w){
	//线1(P1,P2) 线2(P3,P4) (P1,P1-P2,P3,P3-P4)
    Point u = P - Q;
    double t = Cross(w,u) / Cross(v,w);
    return P + v * t;
}

Vector get_Fa(Vector Y){
	Vector Fa;
	if(!dcmp(Y.x - 0)){//原向量垂直
		Fa.x = 1,Fa.y = 0;
	}
	else if(!dcmp(Y.y - 0)){//原向量水平
		Fa.x = 0,Fa.y = 1;
	}
	else{
		Fa.x = 1;
		Fa.y = -1.0 * Y.x / Y.y;
	}
	return Fa;
}

//点a顺时针旋转b度(弧度)
Point rotate(Point a,double b){
	Point Ro;
	Ro.x = a.x * cos(b) + a.y * sin(b);
	Ro.y = -a.x * sin(b) + a.y * cos(b);
	return Ro;
}

Circle get_circle(Point a,Point b,Point c){
	Vector ab = get_Fa(a - b);
	Vector ac = get_Fa(a - c);
	Point ab_mid = (a + b) / 2;
	Point ac_mid = (a + c) / 2;

	Point o = Getlinenode(ab_mid,ab,ac_mid,ac);
	Circle C;
	C.C = o;
	C.r = dis(o,a);
	return C;
}

//最小圆覆盖
Circle Mcc(){
	random_shuffle(p,p + n);
	Circle c({p[0],0});
	for(int i = 1;i < n; i++){
		if(dcmp(c.r - dis(p[i],c.C)) < 0){//i不在圆内
			c = {p[i],0};
			for(int j = 0;j < i; j++){
				if(dcmp(c.r - dis(p[j],c.C)) < 0){
					c = {(p[i] + p[j]) / 2,dis(p[i],p[j]) / 2};
					for(int k = 0;k < j ; k++){
						if(dcmp(c.r - dis(p[k],c.C)) < 0){
							c = get_circle(p[i],p[j],p[k]);
						}
					}
				}
			}
		}
	}
	return c;
}

int main()
{
	scanf("%d",&n);
	for(int i = 0;i < n; i++){
		scanf("%lf %lf",&p[i].x,&p[i].y);
	}
	double a,P;
	scanf("%lf %lf",&a,&P);
	a = a / 180.0 * pi;
	for(int i = 0;i < n; i++){
		p[i] = rotate(p[i],a);//把椭圆摆正
		p[i].x /= P;//坐标压缩
	}
	Circle ans = Mcc();
	printf("%.3f\n",ans.r);
	return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值