nyoj 309 乘雪橇(模拟)

本文介绍了一种算法解决雪橇竞赛中如何最大化速度同时确保在每个转弯处不超过速度限制的问题。通过更新速度数组来保证在转弯前适当减速,并最终找到整个赛程中的最高速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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
分析:

题意就是从起点滑到终点,中间有一些转弯,转弯时有速度限制,而且在滑行过程中没一米只能匀加速或者匀减速,所以当转弯时需要提前开始减速,才能顺利通过,所以当遇到转弯,速度过大时,就需要更新前面的数据,才能符合条件。

主要就是对数组做处理,当不符合条件是更新数据,等到所有数据确定以后再寻找最大值,如果是边更新边处理,得到的可能不是有效的最大值

#include<stdio.h>
#include<string.h>
int main()
{
    int l,n,i,dp[1010];
    while(~scanf("%d%d",&l,&n))
    {
        int a,b;
        memset(dp,0,sizeof(dp));
        for(i=0; i<n; i++)
        {
            scanf("%d%d",&a,&b);
            dp[a]=b;
        }
        int p=1,lim=1;
        for(i=0; i<=l; i++)
        {
            if(dp[i]==0)//不是拐弯的地方一直加速
                dp[i]=lim;
            else
            {
                if(dp[i]>dp[i-1])//拐弯地方
                    dp[i]=dp[i-1]+1;
                else//如果到达拐弯处速度过大,就要更新前面的
                {
                    p=i;//从第i个开始更新
                    while(dp[p-1]-dp[p]>1)
                    {
                        dp[p-1]=dp[p]+1;//一直更新到符合条件
                        p--;
                    }
                }
            }
            lim=dp[i]+1;//速度加1
        }
        int max=0;//寻找最大速度
        for(i=0; i<=l; i++)
            if(dp[i]>max)
                max=dp[i];
        printf("%d\n",max);
    }
    return 0;
}
        


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值