D. Follow Traffic Rules

D. Follow Traffic Rules
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output

Everybody knows that the capital of Berland is connected to Bercouver (the Olympic capital) by a direct road. To improve the road's traffic capacity, there was placed just one traffic sign, limiting the maximum speed. Traffic signs in Berland are a bit peculiar, because they limit the speed only at that point on the road where they are placed. Right after passing the sign it is allowed to drive at any speed.

It is known that the car of an average Berland citizen has the acceleration (deceleration) speed of a km/h2, and has maximum speed of v km/h. The road has the length of l km, and the speed sign, limiting the speed to w km/h, is placed d km (1 ≤ d < l) away from the capital of Berland. The car has a zero speed at the beginning of the journey. Find the minimum time that an average Berland citizen will need to get from the capital to Bercouver, if he drives at the optimal speed.

The car can enter Bercouver at any speed.

Input

The first line of the input file contains two integer numbers a and v (1 ≤ a, v ≤ 10000). The second line contains three integer numbers ld and w (2 ≤ l ≤ 100001 ≤ d < l1 ≤ w ≤ 10000).

Output

Print the answer with at least five digits after the decimal point.

Examples
input
1 1
2 1 3
output
2.500000000000
input
5 70
200 170 40
output
8.965874696353

题意:

有一条公路,长度为l,在距离d处的地方有一个检查速度的装置,这个装置要求通过这一点时的速度不能高于限定的速度w,有一辆车的最大行驶速度是v,加速度是a(加减的加速度一样),问这个车从公路的起点出发,不违章的情况下,最少需要多长时间


题解:

可以说是物理题+数学题,难点在于要把所有的过程考虑清楚,每一步怎么处理,细节太多,本来思路是清晰的,一见到wa,可能一慌就混乱了,一定要注意心态,虽然一上午只做了一道题,但是感觉这样也许对自己的心性也是一种锻炼吧

 

/*
http://blog.csdn.net/liuke19950717
*/
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
double slove(double a,double b,double c)//一元二次方程的正解
{
    return (-b+sqrt(b*b-4*a*c))/(2*a);
}
int main()
{
    double a,v,len,d,w,ans=0;
    scanf("%lf%lf%lf%lf%lf",&a,&v,&len,&d,&w);
    if(w>=v||w*w/(2*a)>d)//不会超速,可以一直加速跑
    {
        double x=(v*v)/(2*a);//加满速需要的距离
        if(x>=len)//一直加速下去
        {
            ans+=sqrt(2*a*len)/a;//加速时间
        }
        else
        {
            ans+=(len-x)/v+v/a;
        }
    }
    else//有超速的可能
    {
        len-=d;//单独讨论限速段
        double tx=w*w/(2*a);//加速到限定速度需要的距离
        double t=w/a;//加速到限定速度的时间
        d-=tx;//限速段其余的路程
        ans+=t;d/=2;
        tx=(v*v-w*w)/(2*a);
        if(tx>d)//没有加速到最高速
        {
            double tv=sqrt(2*a*d+w*w);
            ans+=2*(tv-w)/a;
        }
        else
        {
            ans+=2*(v-w)/a+2*(d-tx)/v;//加速到满后匀速
        }
        //过了限速段后
        tx=(v*v-w*w)/(2*a);
        if(tx>len)//没有加速到最高速
        {
            double tv=sqrt(2*a*len+w*w);
            ans+=(tv-w)/a;
        }
        else
        {
            ans+=(v-w)/a+(len-tx)/v;//加速到满后匀速
        }
    }
    printf("%.12lf\n",ans);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 你可以使用自定义的 Scrapy 中间件来处理 response.follow() 请求。 首先,在你的 Scrapy 项目中创建一个中间件文件,然后在这个文件中定义一个新的中间件类。在这个类中,你需要实现以下三个方法: ``` from scrapy import signals class MyMiddleware: def __init__(self): # 在这里初始化中间件 pass @classmethod def from_crawler(cls, crawler): # 创建中间件实例,并将其绑定到信号 middleware = cls() crawler.signals.connect(middleware.spider_opened, signals.spider_opened) return middleware def process_request(self, request, spider): # 在这里处理 response.follow() 请求 if request.callback == "parse": request = request.replace(callback=self.parse) return request def parse(self, response): # 在这里处理响应数据 pass ``` 然后,你需要在 Scrapy 的设置文件中启用你的中间件: ``` # settings.py DOWNLOADER_MIDDLEWARES = { "myproject.middlewares.MyMiddleware": 500, } ``` 最后,在你的爬虫文件中使用 response.follow() 方法即可。 ### 回答2: Scrapy是一个用于爬取网站数据的Python框架,可以通过设置中间件对请求和响应进行处理。中间件是Scrapy中的一个组件,可以对请求进行修改、过滤或重定向,同时也可以对响应进行处理。 要将response.follow方法加入到中间件里,需要按照以下步骤进行操作: 1. 创建一个新的Scrapy中间件类,继承自scrapy.downloadermiddlewares.DownloaderMiddleware类。 2. 在中间件类中重写process_response方法,该方法会在下载器下载完网页后被调用。 3. 在process_response方法中判断当前的响应是否需要进行重定向,如果需要重定向,则调用response.follow方法来跟进重定向的URL。 4. 返回处理后的响应对象。 下面是一个示例: ```python from scrapy import Request class MyMiddleware(object): def process_response(self, request, response, spider): if response.status in [301, 302]: redirected_url = response.headers.get('Location') if redirected_url: new_request = Request( url=redirected_url, headers=request.headers, cookies=request.cookies, meta=request.meta, dont_filter=True # 如果要进行重定向,需要设置dont_filter为True,避免重复访问 ) # 调用response.follow方法来跟进重定向的URL return spider.follow(new_request) return response ``` 在上述示例中,我们创建了一个名为MyMiddleware的中间件类,其中重写了process_response方法。方法中首先判断响应的状态码是否为301或302,如果是,则获取重定向的URL,并创建一个新的Request对象。然后,通过调用spider.follow方法来跟进重定向的URL。最后,返回处理后的响应对象。 需要注意的是,上述示例仅为演示如何将response.follow方法加入到中间件里,具体的实现方式可能会因具体的项目需求而有所变化。 ### 回答3: 要将`response.follow`方法添加到Scrapy的中间件中,你需要按照以下几个步骤进行操作: 1. 创建一个自定义的中间件类,继承自Scrapy的`DownloaderMiddleware`类,并重写`process_request`方法: ```python from scrapy import Request class MyMiddleware(object): def process_request(self, request, spider): if isinstance(request, Request): # 判断请求是否为Request类型 if 'follow' in request.meta: # 判断请求的meta中是否存在'follow'字段 url = request.url callback = request.callback priority = request.priority dont_filter = request.dont_filter return Request(url=url, callback=callback, priority=priority, dont_filter=dont_filter) return None ``` 2. 在Scrapy的设置文件`settings.py`中,将自定义的中间件添加到`DOWNLOADER_MIDDLEWARES`配置项中: ```python DOWNLOADER_MIDDLEWARES = { 'yourproject.middlewares.MyMiddleware': 543, # 数字越小,优先级越高 } ``` 3. 在爬虫中使用`response.follow`方法,并在请求的meta中添加`'follow': True`,以触发中间件的处理: ```python import scrapy class MySpider(scrapy.Spider): name = 'myspider' def start_requests(self): url = 'http://example.com/page1' yield scrapy.Request(url=url, callback=self.parse, meta={'follow': True}) def parse(self, response): # 使用response.follow方法,会调用中间件中的process_request方法 yield response.follow('/page2', callback=self.parse_page2) def parse_page2(self, response): # 解析爬取到的数据 pass ``` 通过以上步骤,你就可以将`response.follow`方法添加到中间件中,并在需要时使用`response.follow`方法进行请求的跟进。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值