ICPCCamp2017 Day 4 A The Catcher in the Rye(二分+光的折射定律)

大体题意:

告诉你有三个矩形连在一起,要求你从第一个矩形左下角出发,到第三个矩形的右上角,在每个矩形中速度不一样,求最少时间?

思路:

最容易想到的是三分。

取第一个矩形的走的高度是x, 第二个玻璃走的高度是y,列一个函数发现是一个凹函数。

三分就好了。但是时间是0.25s  过不了。

不过有大神 有一个小技巧,就是把这三个矩形 缩小h倍。  最后算完 在乘回去。  (好猛= =)


其实正解是二分。 

把它想成光线, 肯定有一个唯一确定的光线路径。

利用光的折射定律来二分。

只需要二分枚举第一个路径的角度, 就可以算出剩下的两个来,加起来看是否是高度H即可。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define sqr(x) ((x)*(x))
using namespace std;
const double eps = 1e-10;
const double pi = acos(-1.0);
int T;
double h,a,b,c,va,vb,vc;
int mm = 16;
double oo = 1e5+10;
inline int dcmp(double a,double b){
    if (fabs(a-b) < eps) return 0;
    if (a < b) return -1;
    return 1;
}
double ha,hb,hc;
void go(double m){
    double sina = sin(m);
    double cosa = sqrt(1-sqr(sina));
    double tana = sina/cosa;
    ha = tana*a;
    double sinb = sina*vb/va;
    double cosb = sqrt(1-sqr(sinb));
    double tanb = sinb/cosb;
    hb = b*tanb;
    double sinc = sina*vc/va;
    double cosc = sqrt(1-sqr(sinc));
    double tanc = sinc/cosc;
    hc = c*tanc;
}
int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%lf %lf %lf %lf %lf %lf %lf",&h, &a, &b, &c, &va, &vb, &vc);
        double l = 0, r = pi/2;
        while(r-l > eps){
            double m = (l+r)/2;
            go(m);
            if (dcmp(ha+hb+hc,h) < 0) l=m;
            else r = m;
        }
        go((l+r)/2);
        printf("%.10f\n",sqrt(sqr(ha)+sqr(a))/va + sqrt(sqr(hb)+sqr(b))/vb + sqrt(sqr(hc)+sqr(c))/vc);
    }
    return 0;
}
/**
2
10 3 4 3 1 1 1
21 5 12 4 4 3 4

**/



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值