You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
180 lines
4.5 KiB
180 lines
4.5 KiB
we want a java code for the following coding challenge: a road is divided into segments numbered from 1 to n
|
|
‐ within each segment, at any moment there can be at
|
|
most one car
|
|
‐ a car can change segments at every full second
|
|
‐ a segment can be entered only when during the past
|
|
second there was no car in it
|
|
‐ if it is not possible for a car to proceed it waits
|
|
‐ all segment changes occur simultanously
|
|
‐ each car has to perform a trip which consists of entering
|
|
the road at a given start segment, subsequently moving
|
|
from one segment to the next, and leaving the road at a
|
|
given end segment
|
|
‐ the arrival time of a car is when it finishes its trip
|
|
‐ all trips within a test case will have different start
|
|
segments Calculate the arrival times of a number of cars, which perform
|
|
trips on one simple road.
|
|
- all trips start at time=1
|
|
Input:
|
|
Note: lines are separated by newline (\n)
|
|
number road segments (n)
|
|
number of cars (m)
|
|
startsegment,endsegment for car 1
|
|
…
|
|
startsegment,endsegment for car m
|
|
Example input
|
|
100
|
|
5
|
|
3,99
|
|
40,75
|
|
20,99
|
|
28,76
|
|
1,100
|
|
Example output
|
|
98,37,81,50,101
|
|
1 < n < 1000
|
|
1 < m < 1000
|
|
1 <= startsegment < endsegment <= n
|
|
Result:
|
|
arrival times of the cars, separated by comma, in the order of
|
|
the input
|
|
|
|
Lets assume we already have the following data structures:
|
|
|
|
package at.salento;
|
|
|
|
public class Car {
|
|
private int startSegment;
|
|
private int endSegment;
|
|
|
|
private int arrivalTime;
|
|
private int order;
|
|
|
|
private int earliestStartTime; // introduced in level 2
|
|
|
|
private int delay;
|
|
|
|
private boolean entered = false;
|
|
|
|
private boolean toRemove = false; // not used at the moment
|
|
|
|
public Car() {
|
|
this.startSegment = 0;
|
|
this.endSegment = 0;
|
|
this.arrivalTime = 0;
|
|
this.order = 0;
|
|
this.delay = 0;
|
|
}
|
|
|
|
public Car(int start, int end, int duraction) {
|
|
this.startSegment = start;
|
|
this.endSegment = end;
|
|
this.arrivalTime = duraction;
|
|
this.order = 0;
|
|
this.delay = 0;
|
|
}
|
|
|
|
public Car(int start, int end, int duration, int order) {
|
|
this.startSegment = start;
|
|
this.endSegment = end;
|
|
this.arrivalTime = duration;
|
|
this.order = order;
|
|
this.delay = 0;
|
|
}
|
|
|
|
public int getStartSegment() {
|
|
return startSegment;
|
|
}
|
|
|
|
public void setStartSegment(int startSegment) {
|
|
this.startSegment = startSegment;
|
|
}
|
|
|
|
public int getEndSegment() {
|
|
return endSegment;
|
|
}
|
|
|
|
public void setEndSegment(int endSegment) {
|
|
this.endSegment = endSegment;
|
|
}
|
|
|
|
public int getOrder() {
|
|
return order;
|
|
}
|
|
|
|
public void setOrder(int order) {
|
|
this.order = order;
|
|
}
|
|
|
|
public int getArrivalTime() {
|
|
return arrivalTime;
|
|
}
|
|
|
|
public void setArrivalTime(int arrivalTime) {
|
|
this.arrivalTime = arrivalTime;
|
|
}
|
|
|
|
public void increaseArrivalTime() {
|
|
this.arrivalTime++;
|
|
}
|
|
|
|
public int getDelay() {
|
|
return delay;
|
|
}
|
|
|
|
public void increaseDelay() {
|
|
this.delay++;
|
|
}
|
|
|
|
public void setDelay(int delay) {
|
|
this.delay = delay;
|
|
}
|
|
|
|
public int getTotalArrivalTime() {
|
|
return this.arrivalTime + this.delay;
|
|
}
|
|
|
|
public boolean isEntered() {
|
|
return entered;
|
|
}
|
|
|
|
public void setEntered(boolean entered) {
|
|
this.entered = entered;
|
|
}
|
|
|
|
public boolean isToRemove() {
|
|
return toRemove;
|
|
}
|
|
|
|
public void setToRemove(boolean toRemove) {
|
|
this.toRemove = toRemove;
|
|
}
|
|
|
|
public int getEarliestStartTime() {
|
|
return earliestStartTime;
|
|
}
|
|
|
|
public void setEarliestStartTime(int earliestStartTime) {
|
|
this.earliestStartTime = earliestStartTime;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
String viz = "";
|
|
for (int i = 0; i< startSegment; i++) viz += " ";
|
|
viz += "|";
|
|
for (int i = startSegment +1; i < endSegment; i++) viz += "-";
|
|
viz += "|";
|
|
// for (int i=)
|
|
return String.format(" Car(start: %02d", startSegment)
|
|
+ String.format(", end: %02d", endSegment)
|
|
+ String.format(", arr.t.: %02d", arrivalTime)
|
|
+ String.format(", delay: %02d", delay)
|
|
+ String.format(", est: %02d", earliestStartTime)
|
|
+ String.format(", tot.arr.t: %02d", getTotalArrivalTime())
|
|
+ String.format(", order: %02d", order)
|
|
+ ") " + "\n";
|
|
}
|
|
}
|
|
|
|
and we have filled an private ArrayList<Car> cars; in a class Level1ComputationCGT. we now need a method that performs the required calculation
|