codeforces Cable Connection

我的思路很暴力

直接枚举斜率[-100000,0]之间,然后设置一个非常远的直线,对所有点扫一遍,确定一个离这条直线最近的点P。

用点P和斜率k来创建cable,并用cable的距离来relax答案。

现在问题在于,怎么来枚举k,如果直接枚举,设置步长的话,不是超时就是精度不足。

因此我们需要想个办法来枚举k。

我们先枚举0,10000,20000,...,100000找到一个最优答案,假设是20000

那么我们再枚举21000,22000.....,29000以及19000,18000,...11000这些数,找到一个最优的,这样一直找下去,总会找到k。

枚举的时间复杂度为O(10*迭代深度)

具体原理看我下一篇文章。

这道题目卡精度,注意了


代码:

#include <cstdio>
#include <cmath>
using namespace std;


const int maxn = 1e6+7;
const double INF = 1e18;
struct Point{
	int x,y;
}Ps[maxn]; 
int n;
int check(double k){
	double t = INF;
	int mark = 0;
	for(int i = 0;i < n;++i){
		if(abs(k*Ps[i].x-Ps[i].y+(1-k)*10000) < t){
			t = abs(k*Ps[i].x-Ps[i].y+(1-k)*10000);
			mark = i;
		}
	}
	return mark;
}
double getans(double k,int id){
	double a = -k*Ps[id].x + Ps[id].y;
	double b = -Ps[id].y/k+Ps[id].x;
	return a*a+b*b;
}
double f(double k){
	int id = check(-k);
	double ans = getans(-k,id);
	return ans;
}
double csf(double l,double r,int n = 6,double eps = 0.0000001){  
    double x;
    while(r - l > eps){
        double step = (r-l)/(n+1);
        double mx = INF;
        for(double i = l+step;i < r;i += step) {
        	double t = f(i);
        	if(mx > t){
                mx = t;
				x = i;
			}
		}
        l = x-step;
        r = x+step;
    }
    return l;
} 
int main(){
	while(scanf("%d",&n)!=EOF){
		for(int i = 0;i < n;++i){
			scanf("%d %d",&Ps[i].x,&Ps[i].y);
		}
		double k = csf(0,10000,4);
		printf("%.3lf\n",sqrt(f(k)));
	}
	return 0; 
} 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值