Ferry Loading III
来源:互联网 发布:大学生手机号码数据库 编辑:程序博客网 时间:2024/05/23 01:58
Ferry Loading III
import java.io.*;
class Car
...{
String position;//左岸?右岸?
int t;//汽车到达的时间
int p;//记录读入输入文件时的顺序
public Car(int t,String s,int p)
...{
this.position=s;
this.t=t;
this.p=p;
}
}
class sequence
...{
int number;//记录存放顺序,以便在对应位置输出结果
int time;//到达对岸所花时间
public sequence(int number,int time)
...{
this.number=number;
this.time=time;
}
}
class Test4
...{
public static void main(String[] args) throws Exception
...{
BufferedReader bf=new BufferedReader(new FileReader("ferry.in"));
int num=Integer.parseInt(bf.readLine());
String str;
StringTokenizer tag;
for(int i=0;i<num;i++)
...{
str=bf.readLine();
tag=new StringTokenizer(str);
int n=Integer.parseInt(tag.nextToken());
int t=Integer.parseInt(tag.nextToken());
int m=Integer.parseInt(tag.nextToken());
Car car[]=new Car[m];
for(int j=0;j<m;j++)
...{
str=bf.readLine();
tag=new StringTokenizer(str);
car[j]=new Car(Integer.parseInt(tag.nextToken()),tag.nextToken(),j);
}
Car tmp;
for(int j=0;j<car.length-1;j++)
for(int k=j+1;k<car.length;k++)
if(car[j].t>car[k].t)
...{
tmp=car[j];
car[j]=car[k];
car[k]=tmp;
}
Vector v1=new Vector(); //存放每辆汽车到达的时间
Vector v2=new Vector(); //存放每辆汽车到达对岸的时间
Vector v3=new Vector(); //记录可过河的汽车数
int totaltime=car[0].t; //初始化为船第一次过河等待的时间
for(int j=0;j<car.length;j++)
v1.add(car[j]);
int k;
int total=0;
boolean loop1;
boolean loop2;
while(total!=m)//汽车没有全部到达对岸,total为已经到达对岸的汽车的数量
...{
do...{
k=0;//at left
boolean flag=true;
loop1=true;
for(int j=0;j<v1.size();j++)//扫描左岸可过河的汽车
if(((Car)v1.elementAt(j)).position.equals("left")&&((Car)v1.elementAt(j)).t<=totaltime)
...{
v3.add(j);
k++;
if(k==n)break;
}
for(int j=0;j<v3.size();j++)
...{
if(flag==true)...{totaltime+=t;flag=false;}//保证totaltime变量只加一次过河的时间t
v2.add(new sequence(((Car)v1.elementAt(((Integer)v3.elementAt(j)).intValue())).p,totaltime));
total++;
v1.removeElementAt(((Integer)v3.elementAt(j)).intValue());
v1.insertElementAt(new Car(Integer.MAX_VALUE,"left",0),((Integer)v3.elementAt(j)).intValue());
}
int len=v3.size();v3.removeAllElements();
if(len>0)...{loop1=false;}
else if(len==0&&right(totaltime,v1))
...{
totaltime+=t;
loop1=false;
}
else
...{
if(getP(totaltime=getmin(v1),v1).equals("right"))
...{totaltime+=t;loop1=false;}
}
}while(loop1==true);
do...{
k=0;//at right
boolean flag=true;
loop2=true;
for(int j=0;j<v1.size();j++)//扫描右岸可过河的汽车
if(((Car)v1.elementAt(j)).position.equals("right")&&((Car)v1.elementAt(j)).t<=totaltime)
...{
v3.add(j);
k++;
if(k==n)break;
}
for(int j=0;j<v3.size();j++)
...{
if(flag==true)...{totaltime+=t;flag=false;}//保证totaltime变量只加一次过河的时间t
v2.add(new sequence(((Car)v1.elementAt(((Integer)v3.elementAt(j)).intValue())).p,totaltime));
total++;
v1.removeElementAt(((Integer)v3.elementAt(j)).intValue());
v1.insertElementAt(new Car(Integer.MAX_VALUE,"left",0),((Integer)v3.elementAt(j)).intValue());
}
int len=v3.size();v3.removeAllElements();
if(len>0)...{loop2=false;}
else if(len==0&&left(totaltime,v1))
...{totaltime+=t;loop2=false;}
else
...{
if(getP(totaltime=getmin(v1),v1).equals("left"))
...{totaltime+=t;
loop2=false;}
}
}while(loop2==true);
}
sequence s[]=new sequence[v2.size()];
for(int j=0;j<v2.size();j++)
s[j]=(sequence)v2.elementAt(j);
Arrays.sort(s,new Comparator()...{
public int compare(Object a,Object b)
...{
sequence s1=(sequence)a;
sequence s2=(sequence)b;
return s1.number-s2.number;
}});
for(int j=0;j<s.length;j++)
System.out.println(s[j].time);
}
}
public static boolean left(int totaltime,Vector v1)//左岸是否有汽车能过河
...{
for(int j=0;j<v1.size();j++)//扫描左岸可过河的汽车
if(((Car)v1.elementAt(j)).position.equals("left")&&((Car)v1.elementAt(j)).t<=totaltime)
return true;
return false;
}
public static boolean right(int totaltime,Vector v1)//右岸是否有汽车能过河
...{
for(int j=0;j<v1.size();j++)//扫描右岸可过河的汽车
if(((Car)v1.elementAt(j)).position.equals("right")&&((Car)v1.elementAt(j)).t<=totaltime)
return true;
return false;
}
public static int getmin(Vector v1)//获取能够过河的汽车到达时间的最小值
...{
int tmp=((Car)v1.elementAt(0)).t;
for(int i=1;i<v1.size();i++)
if(tmp>((Car)v1.elementAt(i)).t)
tmp=((Car)v1.elementAt(i)).t;
return tmp;
}
public static String getP(int value,Vector v1)//左岸?右岸?
...{
for(int i=0;i<v1.size();i++)
if(value==((Car)v1.elementAt(i)).t)
return ((Car)v1.elementAt(i)).position;
return null;
}
}
试题四:
Input:ferry.in output:ferry.out
在桥变得普遍之前,一般是用渡船来渡汽车过河。河里的渡船,不象那些大渡船,是沿缆绳摆渡的。
有一艘可装n辆汽车的渡船来回各需t分钟。到达河岸的汽车都可由渡船渡
输入
输入的第一行是测试用例个数c。每个测试用例第一行是三个数:n,t,m,其中n,t,m的意义如上所述(n表示一趟可装的汽车数,t表示渡船从岸边到对岸的分钟数,m表示到达岸边的汽车数)。随后的m行,每行表示一辆汽车到达的时间(自该天开始的分钟数),以及汽车所在河岸(”left”或”right”)。对每个测试用例,每辆汽车输出一行,顺序与输入顺序相同,给出每辆汽车在对岸下船的时间。两个测试用例之间输出一空行。
可假设0 < n, t, m ≤ 10000。每个测试的到达时间严格递增。渡船开始在河的left岸。汽车上船与下船时间可认为是0.
Sample input
2
2 10 10
0 left
10 left
20 left
30 left
40 left
50 left
60 left
70 left
80 left
90 left
2 10 3
10 right
25 left
40 left
Output for sample
10
30
30
50
50
70
70
90
90
110
30
40
60
- Ferry Loading III
- uva 10901 - Ferry Loading III(simulation)
- UVA 10901 Ferry Loading III 队列模拟
- hdu 1146 UVa10901 Ferry Loading III
- UVa 10901: Ferry Loading III 装上车子过河去
- Ferry Loading II
- ZOJ 1918 Ferry Loading II
- 【贪心】poj2336 ferry loading II
- hdu 3741 Ferry Loading V
- Ferry Loading II 贪心算法
- uva 10440 Ferry Loading II
- Sicily 1366. Ferry Loading IV
- poj 2336 Ferry Loading II
- Ferry Loading IV(队列)
- uva 10440 Ferry Loading II
- UVa 10440 - Ferry Loading II
- POJ 2609 Ferry Loading 笔记
- POJ2336 Ferry Loading II 动态规划
- 伟人一锅粥
- J2EE程序中的SQL语句自动构造方法
- 软件测试的常识
- 位逻辑运算的巧妙应用
- ICEfaces Note(2)
- Ferry Loading III
- 金达仁:成功应用ERP的思路与方法分析
- Windows Server 2003 服务器中的 IIS 管理器中启用 ASP.NET
- 怎样注册sqldmo.dll
- 汉江怪物
- 刘德华中国巡回演唱会2007
- IT外包的无奈:转了一圈又回来
- 迷失的项目管理
- 个人当老师感受