- 描述
-
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
- 來源
- 第四屆河南省程序設計大賽
- 上傳者
- 張雲聰
要對輸入的閾(yu)值進行排序!!!! 對7 11 13從小到大排序!!!
綠線是從左向右排查,紅線是在綠線基礎上從右向左排查,這樣能保證結果
給幾組測試數據
20 3 7 3 11 1 13 8
14 3 6 3 11 1 13 3
輸出 10和5
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
struct point{
int x,v;
};
int cmp(point const &a,point const &b){
return a.x<b.x;
}
int main(){
int n,m,i,j,max,k;
while(~scanf("%d%d",&n,&m)){
point p[510]; //閾值
point a[1010]; //所有點的座標和速度
memset(p,0,sizeof(p));
memset(a,0,sizeof(a));
p[0].x=0; p[0].v=1;
for(i=1;i<=m;i++){
scanf("%d%d",&p[i].x,&p[i].v);
}
sort(p,p+m+1,cmp);
a[0].v=1; i=1;
for(j=1;j<=m;j++){
while(i<p[j].x){
a[i].v=a[i-1].v+1;
i++;
}
if(i==p[j].x){
if(a[i-1].v>=p[j].v)
a[i].v=p[j].v;
else a[i].v=a[i-1].v+1;
i++;
}
} //從左到右過一遍
for(j=i;j<=n;j++){
a[j].v=a[j-1].v+1;
} //最後一個閾值後可能還有路程,其速度要判斷
max=a[n].v;
for(i=n;i>0;i--){
if(a[i-1].v>=a[i].v+1){
a[i-1].v=a[i].v+1;
if(max<a[i-1].v)
max=a[i-1].v;
}
} //從右到左過一遍,順便再記錄最大值
printf("%d\n",max);
}
return 0;
}