CodeJAM - Train Timetable - Resuelto en PHP
Para aquellos que duden de PHP, o que piensen que PHP eso solo para páginas webs, aqui les va esta:
#!/usr/bin/php
<?php
# Dedicado para los que piensan que PHP es solo para pàginas webs
# y para las personas tontas que comparan un framework cualquiera
# con un lenguaje de programación
#
/**/
function x_readline()
{
return trim(fgets(STDIN));
}
function hour2minutes($hour)
{
$tmp = explode(":",$hour);
return $tmp[0] * 60 + $tmp[1];
}
function get_number_train($depart, $arrival, $ttime)
{
$ret=$tmp=0;
sort($depart);
sort($arrival);
if ( count($arrival)==0)
$ret = count($depart);
else
{
foreach($depart as $pzTrain)
{
if ( $tmp < count($arrival) && $pzTrain >= ($arrival[$tmp] + $ttime))
$tmp++;
else
$ret++;
}
}
return $ret;
}
$total=0; # numero total a "inputs" a procesar
define('NO_WHERE',0); # todavia no comenzamos
define('IN_INPUT_PHASE_1',1); # ttime
define('IN_INPUT_PHASE_2',2); # cantidad de "viajes"
define('IN_READ_TIME',3);
$status = NO_WHERE;
$inp=0;
while ( ($line = x_readline()) != "")
{
#print "+".$line." ($status)\n";
switch($status)
{
case NO_WHERE: # primera linea
$total = (int) $line;
$status++;
break;
case IN_INPUT_PHASE_1:
#limpieza del estado anterior
$inp++;
#print "Input $inp\n";
$input_line=$ttime=$cnt=$pzTrainA=$pzTrainB=0;
$trips=$inA=$outA=$inB=$outB=array();
#
$ttime = (int) $line;
$status++;
break;
case IN_INPUT_PHASE_2:
$trips=explode(" ",$line);
$input_line = $trips[0] + $trips[1];
$status++;
break;
case IN_READ_TIME:
$time = array_map("hour2minutes",explode(" ",$line));
if ( $cnt < $trips[0]) {
$outA[] = $time[0];
$inB[] = $time[1];
} else {
$outB[] = $time[0];
$inA[] = $time[1];
}
if ( ++$cnt == $input_line) {
$pzTrainA = get_number_train($outA,$inA,$ttime);
$pzTrainB = get_number_train($outB,$inB,$ttime);
print "Case #".($inp).": $pzTrainA $pzTrainB\n";
$status = IN_INPUT_PHASE_1;
}
break;
}
}
?>
Nada mas y nada menos un problema de CodeJam resuelto en PHP… no miré si PHP se puede usar, de cualquier manera estoy realizado… demostrando que no solo la viborita (o la mala extension de una gran Lenguaje) pueden hacer cosas así…
Para los que no tiene acceso al CodeJam, aqui escribo el enunciado del problema:
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








php Rulz!!!!
Bien hecho mi amigo!!!
Grande Saddor!!!!