三分套三分 --- HDU 3400 Line belt

 Line belt#

Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=3400
#


 

Mean: 

给出两条平行的线段AB, CD,然后一个人在线段AB的A点出发,走向D点,其中,人在线段AB上的速度为P, 在线段CD上的速度为Q,在其他地方的速度为R,求人从A点到D点的最短时间。

 

analyse:

经典的三分套三分。

首先在AB线段上三分,确定一个点,然后再在CD上三分,确定第二个点,计算出answer。也就是嵌套的三分搜索。

Time complexity: O(logn*logm)

 

Source code: 

复制代码
//  Memory   Time
//  1347K     0MS
//   by : crazyacking
//   2015-03-31-23.22
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<iostream>
#include<algorithm>
#define MAXN 1000010
#define LL long long
using namespace std;
struct point
{
        double x,y;
};
point A,B,C,D;
double p,q,r;
double length(point X,point Y)
{
    return sqrt((X.x-Y.x)*(X.x-Y.x)+(X.y-Y.y)*(X.y-Y.y));
}
double time(double a,double b)
{
    point X,Y;
    X.x=a*(B.x-A.x)+A.x;
    X.y=a*(B.y-A.y)+A.y;
    Y.x=b*(C.x-D.x)+D.x;
    Y.y=b*(C.y-D.y)+D.y;
    return length(A,X)/p+length(D,Y)/q+length(X,Y)/r;
}
double ThiDiv(double alen)
{
        double l=0.0,r=1.0,lm,rm;
        while(r-l>1e-10)
        {
                lm=(l*2.0+r)/3;
                rm=(l+r*2.0)/3;
                if(time(alen,lm)>=time(alen,rm))
                        l=lm;
                else r=rm;
        }
        return time(alen,lm)<time(alen,rm)?time(alen,lm):(time(alen,rm));
}
int main()
{
        ios_base::sync_with_stdio(false);
        cin.tie(0);
//      freopen("C:\\Users\\Devin\\Desktop\\cin.cpp","r",stdin);
//      freopen("C:\\Users\\Devin\\Desktop\\cout.cpp","w",stdout);
        int Cas;
        scanf("%d",&Cas);
        while(Cas--)
        {
                scanf("%lf %lf %lf %lf",&A.x,&A.y,&B.x,&B.y);
                scanf("%lf %lf %lf %lf",&C.x,&C.y,&D.x,&D.y);
                scanf("%lf %lf %lf",&p,&q,&r);
                double l=0.0,r=1.0,lm,rm;
                while(r-l>1e-10)
                {
                        lm=(l*2.0+r)/3;
                        rm=(l+r*2.0)/3;
                        if(ThiDiv(lm)>=ThiDiv(rm))
                                l=lm;
                        else r=rm;
                }
                printf("%.2f\n",min(ThiDiv(lm),ThiDiv(rm)));
        }
        return 0;
}
/*

*/
View Code
复制代码

 

 

 

posted @   北岛知寒  阅读(163)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
历史上的今天:
2014-04-01 最小生成树
点击右上角即可分享
微信分享提示
主题色彩