HDU5515-Game of Flying Circus

這裏的題解寫的非常好。
http://blog.csdn.net/snowy_smile/article/details/49535301

代碼:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
double t1;
double t2;
double t,v1,v2;
void solve()
{
    if(v1==v2)
    {
        printf("Yes\n");
        return;
    }
    else if(v1*v1*2>v2*v2)//能在3之前相遇
    {
        double l=0,r=300;
        for(int i=0;i<=100;i++)
        {
            double mid=(l+r)/2;
            double a=sqrt((300*300+mid*mid))/v1;
            double b=(300+mid)/v2;
            if(a>b)
            {
                l=mid;
            }
            else
                r=mid;
        }
        double ta=sqrt(l*l+300*300)/v1+l/v1+2*t1;
        double tb=t+3*t2;
        if(ta<=tb)//若A能先到達4
        {
            printf("Yes\n");
            return ;
        }
        else
        {
            printf("No\n");
            return ;
        }
    }
    else if(v1*3>v2)//能在4之前相遇
    {
        double l=0,r=300;
        for(int i=0;i<=100;i++)
        {
            double mid=(l+r)/2;
            double b=(600+mid)/v2;
            double a=sqrt(300*300+(300-mid)*(300-mid))/v1;
            if(a>b)
            {
                l=mid;
            }
            else
                r=mid;
        }
        double ta=sqrt(l*l+300*300)/v1+3*t1+sqrt((300-l)*(300-l)+300*300)/v1;
        double tb=t+4*t2;
        if(ta<=tb)//若A能先到達1
        {
            printf("Yes\n");
            return ;
        }
        else
        {
            printf("No\n");
            return ;
        }
    }
    else
    {
        printf("No\n");
        return ;
    }
}
int main (void)
{
    int num;
    cin>>num;
    int cas=1;
    while(num--)
    {
        scanf("%lf %lf %lf",&t,&v1,&v2);
        t1=300.0/v1;
        t2=300.0/v2;
        printf("Case #%d: ",cas++);
        solve();
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章