NYOJ:309 BOBSLEDDING

来源:互联网 发布:nginx 里pid是什么 编辑:程序博客网 时间:2024/05/24 02:58


BOBSLEDDING

时间限制: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 311 113 8
样例输出
5
空博士参加了一个雪橇竞赛,因为他希望他健壮的身材可以给他L米的优势,孔博士从起跑线以一米每秒的速度出发,但他的速度在比赛过程中是可以改变的,
在每米的中间,他可以改变他通过重力加速每秒1米或制动呆在相同的速度或减少他的速度通过1米每秒。空博士必须在山上越过n个弯道,第i个弯道在距起跑
线t_i米处,他必须过这个弯道的速度最多不超过s_i米每秒,空博士可以以任意速度通过终点。求整个过程的最大速度。
解题思路:在每一段都让他往前冲保持最高速前进,每到一个转弯处,若此时速度超过限速,就让这个位置等于限速,在从这个限速往
前推导出之前各个弯道的速度,如果没有超过限速,直接加速通过即可。如果到了最后一个限速弯道,不用再考虑速度的限制,一直加
速冲到终点。这样通过m个弯道所用的时间最少。

例如测试数据:
14 3                                            7 311 113 8
限速                                              3                             1                  8
    0    1     2     3     4     5      6     7      8     9     10    11      12      13     14——>
    1    2     3     4     5     6      7     8超过弯道限速,将该弯道处速度变为该处最高限速往前推之前各个弯道的速度
    1    2     3     4     5     5      4     3      4     5      6      7又超速,同上
    1    2     3     4     5     5      4     3      4     3      2      1       2        3        4

例如测试数据:
7  3
6  3
7  1
5   4
            
                                         4     3     1
    0     1      2      3      4     5     6     7  
    1      2      3     4      5     6
    1      2      3     4      5     4     5
                                   5      4     3
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stack>
#include<algorithm>
using namespace std;
int a[1006];
struct M
{
    int x;//弯道序号
    int v;//速度
}b[1006];
bool cmp(M a,M b)
{
    return a.x<b.x;
}
int main()
{
    int m,n,i,j;
    while(~scanf("%d%d",&m,&n))
    {
        a[0]=1;//数组a用来存放通过每个地方的速度
        b[0].x=0;               //第一个位置为0,
        b[0].v=1;               //速度为1
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&b[i].x,&b[i].v);   //限制速度的弯道序号,和限速
        }
        sort(b,b+n+1,cmp);         //按弯道序号进行排序
        //for(i=0;i<=n;i++)
            //printf("%d %d\n",b[i].x,b[i].v);
        for(i=0;i<n;i++)
        {
            int v1=a[b[i].x];
            for(j = b[i].x ; j <= b[i+1].x ; j++)//从第i个弯道到第i+1个弯道,先让他一直加速
            {
                a[j]=v1;//这个弯道处的速度为v1
                v1++;//每走一米速度加1
                //cout<<a[j]<<" ";
            }
            if(a[j-1]>b[i+1].v)//如果让他从i一直加速到i+1,发现到第i个弯道的时候,超过限速了
            {
                int v2=b[i+1].v;//让v2为i+1处的最大速度
                for(j = b[i+1].x ; ; j--)//在从这个地方往前推结果
                {
                    a[j]=v2;
                    v2++;//因为v2最初是由它从前面某个地方减速得来的,所以v2要++
                    if(a[j]>=a[j-1])//如果我这个地方和它之前相邻的一个地方速度一样,就可以结束了
                        break;
                }
            }
        }
        int v3=a[b[n].x];//过了最后一个限速弯道后,不必在考虑速度超限的问题,直接一直加速冲到终点即可
        for(j = b[n].x ; j <= m ; j++)
        {
            a[j]=v3;
            v3++;
        }
        int mmax=-1;
        for(i=0;i<=m;i++)//求最大速度
         {

    //cout<<a[i]<<" ";
            if(mmax<a[i])
                mmax=a[i];
         }
        printf("%d\n",mmax);
    }
    return 0;
}
0 0