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;
}
} }

 

运行效果:

 

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

最新文章

  1. androidstudio报错
  2. Java线程新特征——Java并发库
  3. JavaScript中如何中断forEach循环
  4. ios 数组排序
  5. 使用clssneme改变图片或样式
  6. PHP实现物流查询(通过快递网API实现)
  7. spoj 39
  8. lightoj 1018 (状态压缩DP)
  9. 去ECSHOP版权,去官方后门
  10. CSS 常用命名
  11. Using .NET 4&#39;s Lazy&lt;T&gt; 实现单实例
  12. SGU 152.Making round
  13. HQApi命令行接口配置
  14. cookie原理
  15. Python3+Selenium2完整的自动化测试实现之旅(一):自动化测试环境搭建
  16. Python——Django-manage.py的内容
  17. Effective Java通用设计
  18. java_12多态
  19. WTS 2.1.18124.1 彻底抛弃了 15063(Win 10 创意者更新)
  20. dotnet core webapi 发布部署到docker的步骤

热门文章

  1. Django【进阶】数据库查询性能相关
  2. 关于:TypeConverter 无法从 System.String 转换
  3. JS判断客户端是否是iOS或者Android手机移动端
  4. 并发容器ConcurrentHashMap与synchronized联合使用达到线程安全
  5. SQl server 2008 附加数据库失败,错误:5120
  6. hdu 1399(水题)
  7. Qt笔记——多线程
  8. 我XXXX!!!够了!!!从github拉到dockerhub,再用daocloud加速下载
  9. 【JBPM4】任务节点-任务分配assignment-Handler
  10. AC日记——【模板】分块/带修改莫队(数颜色) 洛谷 P1903