High way
 
时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

In the city, there is a one-way straight highway starts from the northern end, traverses the whole city southward, and finishes at the southern end. Due to some financial difficulties, there is only one lane on this highway, which means that overtaking other cars in front is impossible.

The highway is quite busy. Lots of different cars are running on it every day. For each car, it is running on the highway at the point which X km far from the northern end in Minute 0, and will leave at the point which Y km far from the northern end. Each car also has a speed limit L km/min. During the whole trip on the highway, each car should run as fast as possible without exceeding its speed limit or overtaking other cars in front. In addition, no two or more cars will be at the same point at Minute 0. Cars can be regarded as points and the distance between two cars can be infinitely small.

Given the information of N cars, please calculate the leaving time for each car.

输入

The first line contains one integer N. 2 <= N <= 1000.

Then follows N line. Each line contains three integers X, Y and L, showing the information of this car. 0 <= X < Y <= 1000000, 0 < L <= 10.

输出

Output N real numbers, one per line -- the leaving time (in minute) of each car, preserving the input order of cars. Your answers should be rounded to 2 digits after the decimal point and the output must contain exactly 2 digits after the decimal point.

样例输入
3
3 5 1
1 4 2
0 8 3
样例输出
2.00
1.50
3.00 代码:
package carRun;

import java.util.Scanner;

public class carTime {

	public static void main(String argv[]){

		Scanner br=new Scanner(System.in);
car[] c;
c=new car[100];
String[] k;
k = new String[100];
int num=0;
do {
k[num] = br.nextLine();
if (k[num].equals("")) {
break;
}
num++;
} while(true); for(int i=0;i<num;i++){ String[] sf=k[i].split(" ");
c[i]=new car(Float.parseFloat(sf[0]),Float.parseFloat(sf[1]),Float.parseFloat(sf[2])); //车初始化
c[i].foreNumber=i+1;
c[i].NforeNumber=i-1; } for(int i=0;i<num;i++){ // System.out.println(c[i].start+" "+c[i].end+" "+c[i].catchspeed+" "+c[i].foreNumber+" "+c[i].NforeNumber); } float[] minTime;
minTime=new float[1000];
for(int i=0;i<1000;i++){
minTime[i]=1000; //每段的最大时间 (初始化状态)
} int max; max=num-1; int min; min=0;int end=0; for(int j=0;j>=0;j++){ int key; int mode;
key=0; mode=0; // find the minTime
for(int i=min;i<max;){
if(c[i].catchspeed>c[c[i].foreNumber].catchspeed)
c[i].catchTime=(c[c[i].foreNumber].start-c[i].start)/(c[i].catchspeed-c[c[i].foreNumber].catchspeed);
c[i].downTime=(c[i].end-c[i].start)/c[i].catchspeed; if(c[i].catchTime!=0&&c[i].catchTime<=minTime[j]) //0 present catch; 1 present down.
{ minTime[j]=c[i].catchTime; key=i; mode=0;} if(c[i].downTime!=0&&c[i].downTime<=minTime[j])
{ minTime[j]=c[i].downTime;key=i; mode=1;}
i=c[i].foreNumber;
} c[max].downTime=(c[max].end-c[max].start)/c[max].catchspeed;
if(c[max].downTime!=0&&c[max].downTime<=minTime[j])
{ minTime[j]=c[max].downTime;key=max; mode=1;} if(mode==1)
c[key].downTag=j;
System.out.println("minTima["+j+"]: "+minTime[j]+" key: "+key+" mode: "+mode+" "+c[key].downTag);
// make the change of cars.
for(int i=max;i>=min; ){ c[i].start=c[i].start+c[i].catchspeed*minTime[j];
if(c[i].start==c[i].end)
c[i].cardown=1;
i=c[i].NforeNumber;
} //tab the head car number.
for(int i=num-1;i>=0;i-- ){
if(c[i].cardown==0)
{ max=i;
break;}
}
//System.out.println("max:"+max);
//tab the tail car number
for(int i=0;i<=max;i++ ){
if(c[i].cardown==0)
{ min=i;
break;}
}
// System.out.println("min:"+min); //change the foreNumber
for(int i=min;i<max;){ for(int h=i+1;h<=max;h++ ){
if(c[h].cardown==0)
{ c[i].foreNumber=h;
c[h].NforeNumber=i;
// i=h;
break;
}
}
i=c[i].foreNumber; } //change the speed for(int i=c[max].NforeNumber;i>=0;){ // System.out.println(i+c[i].start+c[c[i].foreNumber].start+c[i].catchspeed+c[c[i].foreNumber].catchspeed+c[i].catchspeed+c[c[i].foreNumber].catchspeed); if(c[i].start==c[c[i].foreNumber].start&&c[i].catchspeed>=c[c[i].foreNumber].catchspeed)
c[i].catchspeed=c[c[i].foreNumber].catchspeed;
if(c[i].start!=c[c[i].foreNumber].start)
c[i].catchspeed=c[i].speed;
i=c[i].NforeNumber;
} if(min==max)
{ c[min].catchspeed=c[min].speed;
end++;
}
//test report
/* for(int i=0;i<num;i++){ System.out.println(c[i].start+" "+c[i].end+" "+c[i].catchspeed+" "+c[i].foreNumber+" "+c[i].NforeNumber); }*/ //当只剩最后一辆车时,再循环一次,所有车便下了高速路。
if(end==2)
break; } //Count the time.
for(int i=0; i<num;i++){
int n=c[i].downTag;
c[i].Sum=0;
for(int y=0;y<=n;y++){
c[i].Sum=c[i].Sum+minTime[y];
}
System.out.println("The "+i+"号车的下车时间: "+c[i].Sum);
} } static class car{ float start; //记录起始位置
float end; //记录目的地位置
float speed; //记录初始速度
float catchspeed; //记录每个时间段的真实速度
float downTime; //记录每个状态下距离下高速路的时间
float catchTime; //记录每个状态下距离追上前车的时间
int foreNumber; //前面车的车号
int NforeNumber; //后面车的车号
int cardown; //记录车是否下路
int downTag; //记录车在第几个时间段后下路
float Sum; //记录车下路要用的总时间
public car(float a,float b, float c){
this.start=a;
this.end=b;
this.speed=c;
this.catchspeed=c;
this.downTime=0;
this.catchTime=0;
this.foreNumber=0;
this.cardown=0;
this.NforeNumber=0;
this.downTag=0;
this.Sum=0;
}
} }

 

运行效果:

 

输入按照起始位置输入,省去了对原数据的排序。

微软笔试Highway问题解析的更多相关文章

  1. 《PHP程序员面试笔试真题解析》——新书上线

    你好,是我--琉忆.很高兴可以跟你分享我的新书. 很高兴,在出版了PHP程序员面试笔试宝典后迎来了我的第二本书出版--<PHP程序员面试笔试真题解析>. 如果你是一个热爱PHP的程序员,刚 ...

  2. 微软project文件mpp解析

    最近在做一个项目管理的项目,主要是将用户在project文件中写的一些东西,读出来,并将其写入到数据库中. 也是借鉴了好多大佬的思想和代码,感觉自己需要整理一遍,所以,接下来就是表演的时候了. 第一步 ...

  3. 笔试真题解析 ALBB-2015 系统project师研发笔试题

    4)在小端序的机器中,假设 union X {     int x;     char y[4]; }; 假设 X a; a.x=0x11223344;//16进制 则:() y[0]=11 y[1] ...

  4. 笔试真题解析 ALBB-2015 算法project师实习生机试

    1.用十进制计算30!(30的阶乘),将结果转换成3进制进行表示的话,该进制下的结果末尾会有____个0. [解析] 计算N.下3进制结果末尾有多少个0,事实上就是计算3进制中的3被进位多少次,仅仅要 ...

  5. java笔试之参数解析(正则匹配)

    在命令行输入如下命令: xcopy /s c:\ d:\, 各个参数如下: 参数1:命令字xcopy 参数2:字符串/s 参数3:字符串c:\ 参数4: 字符串d:\ 请编写一个参数解析程序,实现将命 ...

  6. 《PHP面试笔试真题库》——PHP面试的好帮手

    你好,是我琉忆. 一个文艺的PHP开发工程师. 很荣幸能够在这里带来我的第一本新书--<PHP程序员面试笔试真题库>. 一.创作过程 <PHP 程序员面试笔试真题库>是我的第三 ...

  7. 赠送4本《 PHP 程序员面试笔试宝典》

    < PHP 程序员面试笔试宝典>历时一年,由机械工业出版社出版,在 2018 年 11 月问世.全书共八个章节,涉及 面试笔试经验技巧.PHP 基础知识.PHP 进阶知识,PHP 面向对象 ...

  8. 别语言之争了,最牛逼的语言不是.NET,也不是JAVA!

    谁都不用说,博客园明显的偏.NET,C#的讨论一出现,推荐讨论热火朝天,而发点JAVA的东西,应者寥寥.一旦有出现两大派系的竞争,那绝对是头条.每天都看,早就麻木了. 研二的我浸淫.NET已经三四年, ...

  9. RazorEngine 3.3 在Mono 3.2上正常运行

    RazorEngine 是一个简化的模板引擎基于微软新的Razor 解析引擎, Razor是在 ASP.NET MVC3 和 Web Pages中引入的.RazorEngine 提供了一个外包装和额外 ...

随机推荐

  1. win7下安装Linux实现双系统全攻略

    http://jingyan.baidu.com/article/c275f6bacc3326e33c756743.html

  2. 初学者学习Javascript很吃力怎么办?到底该如何学习Js?

      Js给初学者的印象总是那么的“杂而乱”,相信很多初学者都在找轻松学习Js的途径.在这里给大家总结一些学习Js的经验,希望能给后来的学习者探索出一条“轻松学习Js之路”. Js给人那种感觉的原因多半 ...

  3. web优化的方法

    缓存(减小对服务器.数据库的压力) 生成静态页面(区别于缓存,数据量太大用“缓存”不利) URL重写(SEO,搜索引擎的优化) ajax的优化(SEO) <meta content=“” nam ...

  4. Linux操作系统中内存buffer和cache的区别--从free命令说起(转)

    原文链接:http://os.51cto.com/art/200709/56603.htm 我们一开始,先从Free命令说起. Free free 命令相对于top 提供了更简洁的查看系统内存使用情况 ...

  5. awk处理之案例五:awk匹配字段2包含字段1的文本

    编译环境 本系列文章所提供的算法均在以下环境下编译通过. [脚本编译环境]Federa 8,linux 2.6.35.6-45.fc14.i686 [处理器] Intel(R) Core(TM)2 Q ...

  6. Factorial Trailing Zeroes——数学类

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  7. [vim]使用中问题

    bug1: vim文档中文注释为乱码 step1: vim /var/lib/locales/supported.d/local 在其中添加下面的中文字符集 zh_CN.GBK GBK zh_CN.G ...

  8. OFBIZ 10.04 开发环境搭建(ofbiz+mysql+eclipse)

    1.下载安装 JDK1.6,并设置环境变量 在“我的电脑”上点右键—>“属性”—>“高级”—> “环境变量(N)”.    新建系统变量JAVA_HOME:C:Program Fil ...

  9. Jquery当div超出页面时就固定到顶部否则跟随页面滚动

    $.fn.smartFloat = function () { var position = function (element) { var top = element.position().top ...

  10. react与mox-react的shouldComponentUpdate 理解

    react性能优化中,提到的就是通过 React.PureComponent 替换 React.Component 组件进行编程. 两个组件之间的不同主要就是PureComponent做了should ...