标签:
时间限制:1000 ms | 内存限制:65535 KB
难度:3
描述
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
来源
每个测速点都是限制,我们就求所有限速点都对该点限制的最大速度
4、.取出队列,并向两边扩展
2、可以采用优先队列将限制速度小的优先扩展
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
const int inf=0x3f3f3f3f;
int n,len;
int v[100000];
struct node
{
int w,f;
} a,b;
int solve();
queue<node>qu;
int main()
{
while(~scanf("%d %d",&len,&n))
{ memset(v,inf,sizeof(v));//初始化
while(!qu.empty())
qu.pop();
a.w=0,a.f=1;
qu.push(a);v[a.w]=1;
for(int i=1; i<=n; i++)
{
scanf("%d %d",&a.w,&a.f);
qu.push(a);//入队
v[a.w]=a.f;//更新值
}
printf("%d\n",solve());
}
return 0;
}
int solve()
{
while(!qu.empty())
{
a=qu.front();
qu.pop();
for(int i=-1; i<2; i+=2)//左右遍历
{
int f=a.f+1;
int w=a.w+i;
if(w<0||w>len)continue;
if(v[w]<=f)continue;//小于当前值才更新
v[w]=f;
b.w=w,b.f=f;
qu.push(b);
}
}
int ans=0;
for(int i=0; i<=len; i++)//寻找最大
if(v[i]>ans)
ans=v[i];
return ans;
}
标签:
原文地址:http://blog.csdn.net/a894383755/article/details/51334147