BZOJ 1857 - 三分套三分

传送门

题目分析

三分:

对于本题,先三分线段a上的点,再在三分计算函数中三分线段b上的点,三分套三分即可求出最优解。

code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;

#define eps 1e-8
double ax, ay, bx, by, cx, cy, dx, dy;
double disa, disb, p, q, r;

inline int read(){
	int i = 0, f = 1;char ch = getchar();
	for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
	if(ch == '-') ch = getchar(), f = -1;
	for(; ch >= '0' && ch <= '9'; ch = getchar())
		i = (i << 3) + (i << 1) + (ch - '0');
	return i * f;
}

inline void wr(int x){
	if(x < 0) putchar('-'), x = -x;
	if(x > 9) wr(x / 10);
	putchar(x % 10 + '0');
}

inline double getlen(double x1, double y1, double x2, double y2){
	return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

inline double calc2(double da, double db){
	double xa, xb, ya, yb;
	if(fabs(ax - bx) < eps){
		xa = ax;
		ya = (ay < by) ? ay + da : ay - da;
	}
	else if(fabs(ay - by) < eps){
		xa = (ax < bx) ? ax + da : ax - da;
		ya = ay;
	}
	else{
		xa = ax + da * (bx - ax) / disa;
		ya = ay + da * (by - ay) / disa;
	}
	
	if(fabs(dx - cx) < eps){
		xb = cx;
		yb = (cy < dy) ? dy - db : dy + db;
	}
	else if(fabs(dy - cy) < eps){
		xb = (cx < dx) ? dx - db : dx + db;
		yb = cy;
	}
	else{
		xb = dx - db * (dx - cx) / disb;
		yb = dy - db * (dy - cy) / disb;
	}
	
	return da / p + getlen(xa, ya, xb, yb) / r + db / q;
}

inline double calc(double da){
	double l = 0, r = disb, mid1, mid2;
	for(int i = 1; i <= 60; i++){
		mid1 = l + (r - l) / 3, mid2 = r - (r - l) / 3;
		if(calc2(da, mid1) - calc2(da, mid2) > eps) l = mid1;
		else r = mid2;
	}
	
	return calc2(da, l);
}

int main(){
	scanf("%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf", &ax, &ay, &bx, &by, &cx, &cy, &dx, &dy, &p, &q, &r);

	disa = getlen(ax, ay, bx, by);
	disb = getlen(cx, cy, dx, dy);
	
	double l = 0, r = disa, mid1, mid2;
	for(int i = 1; i <= 60; i++){
		mid1 = l + (r - l) / 3, mid2 = r - (r - l) / 3;
		if(calc(mid1) - calc(mid2) > eps) l = mid1;
		else r = mid2;
	}
	printf("%0.2f", calc(l));
}
posted @ 2017-09-05 22:26  CzYoL  阅读(198)  评论(0编辑  收藏  举报