Usaco Ski race bobsledding, Dec (DP)

Source: Internet
Author: User

Description

Ski race bobsled

Bessie took part in a steep alpine ski race with a total length of L. When she starts, her initial speed is 1, and Bessie can speed up or decelerate, every 1 meters, she can increase the speed by 1, reduce 1 or remain unchanged. During the course of skiing, Bessie encounters N turn points, and the turning point numbered I has Ti meters from the starting point. To be safe, Bessie cannot reach the I-turn point more than Si. There is no limit to the speed of crossing the end. What is the maximum speed that Bessie can achieve during the entire race?

Input Format

First line: Two integer L and n,2≤l≤10^9; 1≤n≤10^5

The second line to the Nth + 1 line: The line i + 1 has two integers T and S, 1≤t < L; 1≤s≤10^9

Output Format

Single integer: Represents the maximum speed that Bessie can achieve during the race

Sample Input
14 37 311) 113 8
Sample Output
5
Hint

The first place to reach the highest speed is 4 meters from the beginning.

Source
bobsledding, Dec
A DP that requires preprocessing
It is clear that the speed max for the first corner of the f[i is indicated by the state of each turning point].
Because it can be arbitrarily added or kept speed so easy to reach I point speed less than f[i] state is bound to reach; so just record Max.
And we can't just think about whether we can move to the current state, because the minimum speed behind the turning point may
affect the current state;
An example
20 3
3 3
13 20
15 7
Only consider
Current and previous state words at the second turning point speed can reach 13;
And the correct answer is 11, because if F[2]=13 then cannot be transferred to the state of f[3];
Then we just need to preprocess s[i]=min (S[i],s[i+1]+t[i+1]-t[i]) from the back.
Then it satisfies the no-effect nature;
Perform DP f[i]=min (F[i-1]+t[i]-t[i-1],s[i]);
Then Daleitai Maxn=max (MAXN, (f[i]+f[i-1]+t[i]-t[i-1])/2);
The last note is to sort by T first
#include <cstdio> #include <iostream> #include <algorithm>using namespace Std;int n,j,k,l,m,maxx,i; int f[100010];struct st{int s,t;} Mu[100010];bool CMP (const  ST X,  Const St y) {if (x.t<y.t) return True;return false;} int main () {//freopen ("xx.in", "R", stdin), scanf ("%d%d", &l,&n), for (I=1;i<=n;++i) scanf ("%d%d", &mu[i] . t,&mu[i].s); sort (mu+1,mu+1+n,cmp); F[0]=1;mu[n+1].s=1e9;mu[n+1].t=l;for (i=n;i>=1;--i) mu[i].s=min (Mu[i]. S,MU[I+1].S+MU[I+1].T-MU[I].T); for (i=1;i<=n+1;++i) {f[i]=min (mu[i].s,f[i-1]+mu[i].t-mu[i-1].t); Maxx=max (mu[ I].T-MU[I-1].T+F[I]+F[I-1])/2,maxx);} printf ("%d", Maxx);}

  

Usaco Ski race bobsledding, Dec (DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.