NC20276 [SCOI2010]传送带
作者:互联网
题目
- 原题地址:[SCOI2010]传送带
- 题目编号:NC20276
- 题目类型:三分
- 时间限制:C/C++ 1秒,其他语言2秒
- 空间限制:C/C++ 262144K,其他语言524288K
1.题目大意
- 平面两条线段
AB
、CD
,想从A
走到D
,在两条线段和平面上走分别有各自的速度,求最短时间
2.题目分析
- 在
AB
上从A
走到X
,再在平面中从X
走到线段CD
上的Y
点,再从Y
点走到D
- 学习一下三分的板子
3.题目代码
#include<bits/stdc++.h>
using namespace std;
const double eps=1e-4;//锁精度
double ax,ay,bx,by,cx,cy,dx,dy;
double P,Q,R;
double a1,a2,b1,b2;
inline void swap(double &x,double &y){
double c;
c=x,x=y,y=c;
}
inline double f(double x,double a,double b){//已知x以及两系数,求y
return a*x+b;
}
inline double far(double x,double y,double m,double n){//求两点之间的距离
return sqrt((x-m)*(x-m)+(y-n)*(y-n));
}
inline double fi(double x,double y,double a,double b){
double tim = far(x,y,a,b)/R;
tim += far(x,y,dx,dy)/Q;
return tim;
}
inline double suan(double x,double y){
double tim = far(x,y,ax,ay)/P;//A到(x,y)的时间
double l,r,ans,L,R;
if(cx==dx){
l = cy,r = dy;
if(l>r){
swap(l,r);
}
while(r-l>=eps){
double lx=l+(r-l)/3,rx=r-(r-l)/3;
L = fi(cx,lx,x,y), R = fi(cx,rx,x,y);
if(L<=R){
ans = L;
r = rx;
continue;
} else {
ans = R;
l = lx;
}
}
} else {
l = cx,r = dx;
if(l>r){
swap(l,r);
}
while(r-l>=eps){//三分由(x,y)到CD上的某点
double lx = l + (r-l)/3,rx = r - (r-l)/3;
L = fi(lx,f(lx,a2,b2),x,y), R = fi(rx,f(rx,a2,b2),x,y);
if(L<=R){
ans = L;
r = rx;
continue;
} else {
ans = R;
l = lx;
}
}
}
return tim + ans;
}
int main(){
cin >> ax >> ay >> bx >> by >> cx >> cy >> dx >> dy;
cin >> P >> Q >> R;
if(ax!=bx){
a1 = (ay-by) / (ax-bx);
b1 = (ay-a1*ax);
}
if(cx!=dx){
a2 = (cy-dy) / (cx-dx);
b2 = (cy-a2*cx);
}
double l,r,ans,L,R;
if(ax==bx){
l = ay,r = by;
if(l>r){
swap(l,r);
}
while(l<=r){
double lx = l+(r-l)/3,rx = r-(r-l)/3;
L = suan(ax,lx),R = suan(ax,rx);
if(L<=R){
ans = L;
r = rx - eps;
continue;
} else {
ans = R;
l = lx + eps;
}
}
} else {
l = ax,r = bx;
if(l>r){
swap(l,r);
}
while(r-l>=eps){//三分由A到AB中的一点(不走AB即为从A到A)
double lx=l+(r-l)/3,rx=r-(r-l)/3;
L=suan(lx,f(lx,a1,b1)),R=suan(rx,f(rx,a1,b1));
if(L<=R){
r = rx;
ans = L;
continue;
} else {
l = lx;
ans = R;
}
}
}
printf("%.2lf",ans);
return 0;
}
标签:传送带,double,rx,cx,ay,ax,NC20276,lx,SCOI2010 来源: https://www.cnblogs.com/zhangyi101/p/16612444.html