BOBSLEDDING
-
描述
Dr.Kong has entered a bobsled competition because he hopes his hefty weight will give his an advantage over the L meter course (2 <= L<= 1000). Dr.Kong will push off the starting line at 1 meter per second, but his speed can change while he rides along the course. Near the middle of every meter Bessie travels, he can change his speed either by using gravity to accelerate by one meter per second or by braking to stay at the same speed or decrease his speed by one meter per second.
Naturally, Dr.Kong must negotiate N (1 <= N <= 500) turns on the way down the hill. Turn i is located T_i meters from the course start (1 <= T_i <= L-1), and he must be enter the corner meter at a peed of at most S_i meters per second (1 <= S_i <= 1000). Dr.Kong can cross the finish line at any speed he likes.
Help Dr.Kong learn the fastest speed he 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]'):
0 1 2 3 4 5 6 7[3] 8 9 10 11[1] 12 13[8] 14
(Start) |------------------------------------------------------------------------| (Finish)
Below is a chart of Dr.Kong '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
His maximum speed was 5 near the beginning of meter 4.
-
输入
-
There are multi test cases,your program should be terminated by EOF
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 Dr.Kong can attain between the start and the finish line, inclusive. 样例输入
-
14 3 7 3 11 1 13 8
样例输出
-
5
解题思路:
贪心问题,首先将他们预处理一下,即先将要改变的位置从小到大排列,然后在确保后一个(改变的)可以通过的情况下,前一个(改变的)最大值,(注意一定要从最后一个往前改变),
最后一步模拟
代码如下:
# include<stdio.h>
# include<algorithm>
using namespace std;
struct node{
int x,v;
}a[1505];
bool cmp(node a,node b)
{
return a.x<b.x;
}
int main(){
int n,m;
while(~scanf("%d%d",&n,&m))
{ int l1=0;
for(int i=0;i<m;i++)
{
scanf("%d%d",&a[i].x,&a[i].v);
}
sort(a,a+m,cmp); //按位置排序
for(int i=m-1;i>=1;i--) //从后向前
{
if(a[i].v<a[i-1].v)
{
int l=a[i].x-a[i-1].x;
int ll=a[i-1].v-a[i].v;
if(ll>l)
{
a[i-1].v=a[i].v+l;
}
}
}
int sum=0,max=1,j=0;
for(int i=0;i<=n;i++)//模拟
{
sum++;
if(j<m&&sum>a[i].v&&sum-a[j].v>a[j].x-i){
if(max<sum-1) max=sum-1;
sum=a[j].v;
i=a[j].x;
j++;
}
if(i==a[j].x)
j++;
}
if(max<sum) max=sum;
printf("%d\n",max);
}
return 0;
}