NYOJ 309

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 3
11 1
13 8
樣例輸出
5

題目的意思是說有一段滑雪路長L,有n個拐點,每個拐點要求速度不超過s[i],且這個人從0開始,只能每秒加速1M或者減速1M求在符合要求的情況下,在這個途中

最大的速度能夠達到多少,典型的揹包題。

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
    int dp[1005];
    int l,n,a,b;
    while(scanf("%d%d",&l,&n)!=EOF)
    {
        memset(dp,0,sizeof(dp));   //每次循環必須重置啊啊啊啊啊啊啊啊啊啊
        for(int i=0; i<n; i++)
        {
            scanf("%d%d",&a,&b);
            dp[a]=b;             //記錄每個拐點的限速
        }
        int s=0;                 //剛開始一定要賦值0,如果此時s=1下面從i=1開始就會漏掉1的時候
        for(int i=0; i<=l; i++)
        {
            if(dp[i]==0)
                dp[i]=s+1;            //沒有拐點就一直加速
            else
            {
                if(dp[i]>dp[i-1])
                {
                    dp[i]=dp[i-1]+1;
                }
                else
                {
                    int p=i;              //如果是拐點就往前查找看到哪裏能夠符合要求通過
                    while(dp[p-1]-dp[p]>1)
                    {
                        dp[p-1]=dp[p]+1;
                        p--;
                    }
                }
            }
            s=dp[i];
        }
        int max=-3;        //最後求出最大值即可
        for(int i=0; i<=l; i++)
        {
            if(max<dp[i])
                max=dp[i];
        }
        printf("%d\n",max);
    }
    return 0;
}


發佈了67 篇原創文章 · 獲贊 100 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章