[USACO09DEC]雪橇Bobsledding(贪心)

https://www.luogu.org/problem/P2968

题目描述

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 Ti meters from the course start (1 <= Ti <= L-1), and she must be enter the corner meter at a speed of at most Si meters per second (1 <= Si <= 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.
贝茜从山顶滑雪到山脚,山顶到山脚距离是L(2<L<10^9)米.贝茜在起点的速度是1米每秒,但是他的速度是可以改变的,在每一米的速度可以是前一米的速度加1、减1,或者等于前一米的速度.在滑行的过程中,贝茜会遇到N<=100000)个转弯处,第i个转弯处位于距离出发Ti米处,为了安全,贝茜到达第i个转弯处的速度不能超过Si(1<Si<10^9)米 每秒.当然贝茜到达终点时的速度没有最大限制.请你计算贝茜在滑雪过程中最大的速度可以是多少?
 
 
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.

输入描述:

* Line 1: Two space-separated integers: L and N
* Lines 2..N+1: Line i+1 describes turn i with two space-separated integers: Ti and Si

输出描述:

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

输入

14 3 
7 3 
11 1 
13 8 

输出

 5

100000的数据规模显然很难区间DP,于是我们考虑贪心。

这题的特性:速度变化量为±1或0。

对于每个拐角,经过它的速度的最大限制(既要小于题目给出的安全限制,又要确保后面的拐角能够顺利通过)。

知道这个之后,我们就可以从前往后模拟,计算出每两个拐角之间的速度最大值(不要忘了还有起点和终点),以及到达拐角时的速度。

计算最大速度值要用点脑子(自己试着推下,或参考代码)

 
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <queue>
 9 #include <set>
10 #include <map>
11 #include <math.h>
12 const int INF=0x3f3f3f3f;
13 typedef long long LL;
14 const int mod=1e9+7;
15 const double PI=acos(-1);
16 const int maxn=100010;
17 using namespace std;
18 //ios::sync_with_stdio(false);
19 //    cin.tie(NULL);
20 
21 int l,n,ans;
22 struct node
23 {
24     int dis;//限制点的坐标
25     int speed;//限制速度
26 }limit[maxn];
27 
28 bool cmp(node a,node b)
29 {
30     return a.dis<b.dis;
31 }
32 
33 int main()
34 {
35     scanf("%d %d",&l,&n);
36     for(int i=1;i<=n;i++)
37     {
38         scanf("%d %d",&limit[i].dis,&limit[i].speed);
39     }
40     sort(limit+1,limit+1+n,cmp);//不知道数据是不是按距离输入的,保险起见排个序 
41     
42     for(int i=n;i>=2;i--)//反着更新速度限制 
43     {
44         limit[i-1].speed=min(limit[i-1].speed,limit[i].speed+limit[i].dis-limit[i-1].dis);
45     }
46     ans=1;//答案 
47     int s=1;//速度值 
48     for(int i=1;i<=n;i++)
49     {
50         int d1=limit[i].speed-s; 
51         int d2=limit[i].dis-limit[i-1].dis;
52         if(d1<d2)
53         {
54             ans=max(ans,limit[i].speed+(d2-d1)/2);
55             s=limit[i].speed;
56         }    
57         else
58         {
59             ans=max(ans,s+d2);
60             s+=d2;
61         }
62     }
63     //注意最后一段是可以一直加速的,别忘了再比较一次
64     s+=l-limit[n].dis;
65     ans=max(ans,s); 
66     printf("%d
",ans);
67     return 0;
68 }
 
 
 
 
 
 
原文地址:https://www.cnblogs.com/jiamian/p/11376948.html