sexta-feira, 18 de julho de 2008

Google Code Jam - Train Timetable

from Google Code Jam - july 2008....

Problem

A train line has two stations on it, A and B. Trains can take trips from A to B or from B to A multiple times during a day. When a train arrives at B from A (or arrives at A from B), it needs a certain amount of time before it is ready to take the return journey - this is the turnaround time. For example, if a train arrives at 12:00 and the turnaround time is 0 minutes, it can leave immediately, at 12:00.

A train timetable specifies departure and arrival time of all trips between A and B. The train company needs to know how many trains have to start the day at A and B in order to make the timetable work: whenever a train is supposed to leave A or B, there must actually be one there ready to go. There are passing sections on the track, so trains don't necessarily arrive in the same order that they leave. Trains may not travel on trips that do not appear on the schedule.

Input

The first line of input gives the number of cases, N. N test cases follow.

Each case contains a number of lines. The first line is the turnaround time, T, in minutes. The next line has two numbers on it, NA and NB. NA is the number of trips from A to B, and NB is the number of trips from B to A. Then there are NA lines giving the details of the trips from A to B.

Each line contains two fields, giving the HH:MM departure and arrival time for that trip. The departure time for each trip will be earlier than the arrival time. All arrivals and departures occur on the same day. The trips may appear in any order - they are not necessarily sorted by time. The hour and minute values are both two digits, zero-padded, and are on a 24-hour clock (00:00 through 23:59).

After these NA lines, there are NB lines giving the departure and arrival times for the trips from B to A.

Output

For each test case, output one line containing "Case #x: " followed by the number of trains that must start at A and the number of trains that must start at B.

Limits

1 ≤ N ≤ 100

Small dataset

0 ≤ NA, NB ≤ 20

0 ≤ T ≤ 5

Large dataset

0 ≤ NA, NB ≤ 100

0 ≤ T ≤ 60

Sample


Input

Output
2
5
3 2
09:00 12:00
10:00 13:00
11:00 12:30
12:02 15:00
09:00 10:30
2
2 0
09:00 09:01
12:00 12:02

Case #1: 2 2
Case #2: 2 0


SOLUTION

/**
sample file

2
5
3 2
09:00 12:00
10:00 13:00
11:00 12:30
12:02 15:00
09:00 10:30
2
2 0
09:00 09:01
12:00 12:02

*/
package codejam;

import java.io.File;
import java.io.FileInputStream;
import java.util.PriorityQueue;

/**
* @author Ricardo A. Harari - ricardo.harari@gmail.com
*
*/
public class TrainTimable {

PriorityQueue schedules = new PriorityQueue();

int turnaround = -1; // minutes

int n = 0;

private static final String FILE_NAME = "/tmp/sample.txt";

/**
* pass the full location of the sample file as argument
* if you dont pass anything it will search at /tmp/sample.txt
*
* @param args
*/
public static void main(String[] args) {
String fname = args.length == 0 ? FILE_NAME : args[0];
try {
TrainTimable o = new TrainTimable();
o.start(fname);
} catch (Exception e) {
System.out.println("Oppps.");
System.out.println("An exception has been throw:" + e.getMessage());
e.printStackTrace();
}
}

private void start(String fileName) throws Exception {
File f = new File(fileName); // sample file

int na = 0;
int nb = 0;

if (!f.exists() || !f.isFile()) throw new Exception("Sorry, " + fileName + " is not a valid file.");
FileInputStream fi = new FileInputStream(f);
int cint;
String tmp = new String();
int caseNum = 0;
int step = 0;
while ((cint = fi.read()) > -1) {
char c = (char) cint;
if (c == '\r' || c == '\n') {
tmp = tmp.trim();
if (tmp.length() > 0) {
if (step == 0) {
try {
n = Integer.parseInt(tmp);
step++;
} catch (NumberFormatException nfe) {
// do nothing. the 1st line should not be in correct format
}
} else if (step == 1) {
turnaround = Integer.parseInt(tmp);
caseNum++;
na = 0;
nb = 0;
schedules.clear();
step++;
} else if (step == 2) {
int pos = tmp.lastIndexOf(' ');
if (pos > -1) {
na = Integer.parseInt(tmp.substring(0, pos).trim());
nb = Integer.parseInt(tmp.substring(pos).trim());
}
step++;
} else if (na > 0) {
addToSchedule(tmp, true);
na--;
} else if (nb > 0) {
addToSchedule(tmp, false);
nb--;
}
if (step == 3 && na == 0 && nb == 0) {
process(caseNum);
turnaround = 0;
step = 1;
}
tmp = "";
}
} else {
tmp += c;
}
}
if (step == 3) {
process(caseNum);
}
fi.close();
}

/**
* @param caseNum
*/
private void process(int caseNum) {
int trainA = 0;
int trainB = 0;
int availableTrainA = 0;
int availableTrainB = 0;

while (schedules.size() > 0) {
Schedule sched = schedules.poll();
if (!sched.arrive) {
boolean isFromA = sched.source.equals("A");
if (isFromA) {
if (availableTrainA > 0) {
availableTrainA--;
} else {
trainA++;
}
} else {
if (availableTrainB > 0) {
availableTrainB--;
} else {
trainB++;
}
}
} else {
boolean isToB = sched.source.equals("B");
if (isToB) {
availableTrainB++;
} else {
availableTrainA++;
}
}
}
print(caseNum, trainA, trainB);
}

private long hourToLong(String time) {
int hour = Integer.parseInt(time.substring(0, 2));
int minute = Integer.parseInt(time.substring(3));
return hour*60 + minute;
}

/**
* @param tmp
* @param b
*/
private void addToSchedule(String tmp, boolean fromA) {
int pos = tmp.lastIndexOf(' ');
if (pos > -1) {
long start = hourToLong(tmp.substring(0, pos).trim());
long end = hourToLong(tmp.substring(pos).trim()) + turnaround;
schedules.add(new Schedule(start * 100000 + end, fromA ? "A" : "B", false));
schedules.add(new Schedule(end * 100000, fromA ? "B" : "A", true));
}
}

/**
* @param caso
* @param i
*/
private void print(int caso, int na, int nb) {
System.out.println("Case #" + caso + ": " + na + " " + nb);
}

/**
*
* @author Ricardo Alberto Harari - ricardo.harari@gmail.com
*
*/
class Schedule implements Comparable {

public Schedule(long _nextSchedule, String _source, boolean _arrive) {
nextSchedule = _nextSchedule;
source = _source;
arrive = _arrive;
}
long nextSchedule;
String source;
boolean arrive = false;

/* (non-Javadoc)
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
public int compareTo(Schedule o) {
if (nextSchedule - o.nextSchedule < 0) return -1;
if (nextSchedule - o.nextSchedule > 0) return 1;
return 0;
}
}

}

Nenhum comentário: