洛谷P2968 Bobsledding S

题目描述

Bessie has entered a bobsled competition because she hopes her hefty weight will give her an advantage over the L meter course (2 <= L <= 1,000,000,000).

Bessie will push off the starting line at 1 meter per second, but her speed can change while she rides along the course. Near the middle of every meter Bessie travels, she can change her speed either by using gravity to accelerate by one meter per second or by braking to stay at the same speed or decrease her speed by one meter per second.

Naturally, Bessie must negotiate N (1 <= N <= 100,000) turns on the way down the hill. Turn i is located T_i meters from the course start (1 <= T_i <= L-1), and she must be enter the corner meter at a speed of at most S_i meters per second (1 <= S_i <= 1,000,000,000). Bessie can cross the finish line at any speed she likes.

Help Bessie learn the fastest speed she can attain without exceeding the speed limits on the turns.

Consider this course with the meter markers as integers and the turn speed limits in brackets (e.g., '[3]'):


|   1   2   3   4   5   6   7[3]
|---+---+---+---+---+---+---+
|                            \
Start                         + 8    
                               \
                                + 9    
                                 \
                                  + 10       +++ 14 (finish)
                                   \         /
                              11[1] +---+---+
                                        12  13[8]

Below is a chart of Bessie's speeds at the beginning of each meter length of the course:

Max:                              3               1       8 
Mtrs: 0   1   2   3   4   5   6   7   8   9  10  11  12  13  14 Spd:  1   2   3   4   5   5   4   3   4   3   2   1   2   3   4 
```
Her maximum speed was 5 near the beginning of meter 4. 

贝茜从山顶滑雪到山脚,山顶到山脚距离是L(2<L<10^9)米.贝茜在起点的速度是1米每 秒,但是他的速度是可以改变的,在每一米的速度可以是前一米的速度加1、减1,或者等于前一米 的速度.在滑行的过程中,贝茜会遇到N<=100000)个转弯处,第i个转弯处位于距离出发 Ti米处,为了安全,贝茜到达第i个转弯处的速度不能超过Si(1<Si<10^9)米 每秒.当然贝茜到达终点时的速度没有最大限制.请你计算贝茜在滑雪过程中最大的速度可以是多 少?

输入格式

* Line 1: Two space-separated integers: L and N

* Lines 2..N+1: Line i+1 describes turn i with two space-separated integers: T_i and S_i

输出格式

* Line 1: A single integer, representing the maximum speed which Bessie can attain between the start and the finish line, inclusive.

输入输出样例

输入 #1复制

14 3 
7 3 
11 1 
13 8 

输出 #1复制

5 

上代码:

#include<cstdio>
#include<algorithm>
#include<cstdlib>
using namespace std;
int l,n,ans,m;//m表示到达某个拐角时的速度
typedef struct{
    int t,s,q;
}P;
bool cmp(P aa,P bb){
    return (aa.t<bb.t);
}
P p[100002];
int main()
{
    scanf("%d%d",&l,&n);
    for (int i=1;i<=n;i++)
    scanf("%d%d",&p[i].t,&p[i].s);
    sort(p+1,p+n+1,cmp);p[n].q=p[n].s;//切记要排序
    for (int i=n;i>=2;i--)
    p[i-1].q=min(p[i-1].s,p[i].q+p[i].t-p[i-1].t);//推出最大速度限制
    if (p[1].t+1<=p[1].q)ans=p[1].t+1;
    else ans=(p[1].q+p[1].t+1)/2;
    m=min(p[1].q,p[1].t+1);//起点与第一个拐角的信息
    for (int i=2;i<=n;i++)
    {
        if (m+p[i].t-p[i-1].t<=p[i].q) ans=max(ans,m+p[i].t-p[i-1].t);
        else ans=max(ans,(p[i].q+p[i].t-p[i-1].t+m)/2);
        m=min(p[i].q,m+p[i].t-p[i-1].t);//i和i-1个拐角的信息
    }
    printf("%d",max(ans,m+l-p[n].t));//不要忘了终点
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值