TCHS SRM 31 Problem 500
来源:互联网 发布:c语言中主函数 编辑:程序博客网 时间:2024/06/05 20:26
题目:
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:HighwayJamMethod:minTimeParameters:int[], int, intReturns:doubleMethod 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.5In 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.0Here, 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-63)
{1, 0, 10}
4
1000
Returns: 115.2The 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;}}
这个题目真的是太恶心了!一定注意一个条件,不能走过了! 开始想法就是这个思路,不过比这个实现起来复杂多了。。。。基于比较的递归策略,其实
完全不用这么费劲!
答案是设置一个全局变量,然后依次遍历所有。。。现在想起来,这个问题太弱智了。。。
- TCHS SRM 31 Problem 500
- TopCoder - "TCHS SRM 1" 答案 及 代码
- TopCoder SRM 633 Div2 Problem 500 - Jumping
- TopCoder SRM 634 Div2 Problem 500 - ShoppingSurvey
- Topcoder SRM 663 Div2 Problem 500
- Topcoder: Problem Statement SRM 39 DIV1 500 Point
- TopCoder SRM 644 Div2 Problem 500 - LostCharacter (贪心)
- TopCoder SRM 645 Div2 Problem 500 - ConnectingCars (枚举)
- TopCoder SRM 646 Div2 Problem 500 - TheGridDivTwo (BFS + 优先队列)
- TopCoder SRM 647 Div2 Problem 500 - TravellingSalesmanEasy (思维)
- TopCoder SRM 649 Div2 Problem 500 - CartInSupermarketEasy (区间DP)
- TopCoder SRM 650 Div2 Problem 500 - TaroFillingAStringDiv2 (DP)
- TopCoder SRM 651 Div2 Problem 500 - FoxAndSouvenirTheNext (DP)
- TopCoder SRM 653 Div2 Problem 500 - RockPaperScissorsMagicEasy (DP)
- TopCoder SRM 654 Div2 Problem 500 - OneEntrance (思维)
- TopCoder SRM 655 Div2 Problem 500 - FoldingPaper2 (思维)
- TopCoder SRM 657 Div2 Problem 500 - ProblemSetsEasy (二分 + 模拟)
- TopCoder SRM 659 Div2 Problem 500 - PublicTransit (思维)
- Hibernate 的原理与配置
- Android应用程序模型
- 调用com
- js动态生成table【转】js动态生成table【转】
- Beautiful Code {16. The Linux Kernel Driver Model: The Benefits of Working Together}
- TCHS SRM 31 Problem 500
- 不要存侥幸
- Hibernate包作用详解
- 全选/删除集合
- 没有生成forms authentication cookie
- Hello World!之php调用COM+组件(转)
- NetworkInterface实例 打印出与本地主机关联的所有IP地址
- 我们有多少对西方文明的偏见?——关于五千年与二百年问题的通信
- 提高SQL效率的方案