TCHS SRM 31 Problem 500

题目:

Problem Statement

    

It's only 9 AM, and already your day has been horrible. You overslept your alarm, your oatmeal got cold as you listened to the traffic report, and the water in your shower was frigid. To make matters worse, as you got onto the highway you saw a horrible traffic jam. To try to make your day better, you have to figure out the fastest way to get to your exit.

You have been given lanes, a int[] listing the speed (in feet per second) of cars traveling in each lane on the highway. You begin in lane 0, and your exit (located dist feet away) is also in lane 0. You are allowed to move into an adjacent lane only if you have been in your current lane for at least laneChange seconds; otherwise, you won't have sufficient time to make sure there's no accident! This also includes exiting; if you change lanes during your trip, you must return in lane 0 for at least laneChange seconds in order to safely exit the highway. If you do not change lanes during your trip, then you may exit the highway as soon as you reach your exit.

With this information, return the least amount of time required for you to reach your exit. See the examples for further clarification.

 

Definition

    
Class:HighwayJam
Method:minTime
Parameters:int[], int, int
Returns:double
Method signature:double minTime(int[] lanes, int laneChange, int dist)
(be sure your method is public)
    
 
 

Notes

-Your return value must have an absolute or relative error less than 1e-9.
 

Constraints

-lanes will contain between 1 and 50 elements, inclusive.
-Each element of lanes will be between 0 and 528000, inclusive.
-Element 0 of lanes will not be 0.
-laneChange will be between 0 and 60, inclusive.
-dist will be between 0 and 1000000000 (109), inclusive.
 

Examples

0) 
    
{1,2}
2
15
Returns: 9.5
In this case, you can stay in lane 0, which will take you 15/1 = 15 seconds. Alternatively, you can shift into lane 1 after 2 seconds, drive for 5.5 seconds, and then switch back to lane 0 for 2 more seconds. This covers 2(1) + 5.5(2) + 2(1) = 15 feet in only 9.5 seconds.
1) 
    
{1, 2}
2
6
Returns: 6.0
Here, you do not have enough time to safely switch to the other lane and come back, so you must stay in lane 0 the whole time.
2) 
    
{528000}
10
1
Returns: 1.893939393939394E-6
 
3) 
    
{1, 0, 10}
4
1000
Returns: 115.2
The middle lane is not moving at all, but you need to pass through it to get to the fastest lane.
4) 
    
{12, 0, 18, 0, 0, 0, 0,
 944, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 528000}
15
1500
Returns: 123.33333333333334

 

public class HighwayJam
{

public double minTime(int[] lanes, int laneChange, int dist)
{
    double ret = 1.0*dist/lanes[0];
    double dleft = dist;
    double ctime = 0;
   
    for(int i=1; i<lanes.length; i++)
    {
	     dleft -= 2*laneChange*lanes[i-1];
	     ctime += 2*laneChange;
	     if(dleft < laneChange*lanes[i]) break;	// if no time to switch back, we're done
	     ret = Math.min(ret, ctime + dleft/lanes[i]);
    }
    return ret;
}

}
这个题目真的是太恶心了!一定注意一个条件,不能走过了! 开始想法就是这个思路,不过比这个实现起来复杂多了。。。。基于比较的递归策略,其实
完全不用这么费劲!
答案是设置一个全局变量,然后依次遍历所有。。。现在想起来,这个问题太弱智了。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值