(三分)Turn the corner -- HDOJ

Turn the corner
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 722 Accepted Submission(s): 329

Problem Description
Mr. West bought a new car! So he is travelling around the city.

One day he comes to a vertical corner. The street he is currently in has a width x, the street he wants to turn to has a width y. The car has a length l and a width d.

Can Mr. West go across the corner?

Input
Every line has four real numbers, x, y, l and w.
Proceed to the end of file.

Output
If he can go across the corner, print “yes”. Print “no” otherwise.

Sample Input

10 6 13.5 4
10 6 14.5 4

Sample Output

yes
no

Source
2008 Asia Harbin Regional Contest Online

Recommend
gaojie

总结:
三分适用于在一个区间内,求凸函数或者凹函数 的最值,二分适用于单调的函数。

难点:
1、要想顺利通过,必须保证A点到Z轴的距离H 小于等于y
2、H的求解
这里写图片描述
3、H函数的单调性
函数 = (l*sin(t) - x) / tan(t) + d / sin(t);

    l = x = d = 3;;
        for(double i=0.1; i<=PI/2.0; i += 0.1)
        {
           cout << Cal(i)<<endl;
        }

运行上面的代码,可以看到值是先增大后减小,可以断定函数先增后减,找出最大值,再与y进行比较,就可以得出答案

#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#define PI acos(-1.0)
#define eps 0.000001

using namespace std;
double x,y,l,d;
double Cal(double t)
{
    return (l*sin(t) - x) / tan(t) + d / sin(t);
}
int main(void)
{
 //   freopen("in.txt","r",stdin);

    while(scanf("%lf %lf %lf %lf",&x,&y,&l,&d) != EOF )
    {
        double midl,midr,l,r,y1,y2;
        l = 0,r = 0.5*PI;
        while((fabs(r-l) > eps))
        {
            midl = l + (r-l)/3;
            midr = r - (r-l)/3;
            y1 = Cal(midl);
            y2 = Cal(midr);
            if(y1 > y2)
                r = midr;
            else
                l = midl;
        }

        if(Cal(r) <= y) cout << "yes";
        else cout << "no";
        cout <<endl;
    }
    /*
        l = x = d = 3;;
        for(double i=0.1; i<=PI/2.0; i += 0.1)
        {
           cout << Cal(i)<<endl;
        }*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值