-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob_3.php
67 lines (59 loc) · 1.67 KB
/
prob_3.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
// $in = fopen("prob_3.txt", "r");
// $out = fopen("prob_3.out", "w");
$in = STDIN;
$out = STDOUT;
$T = trim(fgets($in));
for ($x = 1; $x <= $T; $x++) {
$solution = '';
$activities = $c = $j = [];
$N = trim(fgets($in));
for ($i = 0; $i < $N; $i++) {
$activity = [];
list($activity['start'], $activity['end']) = explode(' ', trim(fgets($in)));
$activity['id'] = $i;
$activities[] = $activity;
}
$start = array_column($activities, 'start');
$end = array_column($activities, 'end');
array_multisort($start, SORT_ASC, $end, SORT_ASC, $activities);
for ($i = 0; $i < $N; $i++) {
$activity = &$activities[$i];
if (empty($c)) {
$c[] = $activity;
$activity['group'] = 'C';
} else {
for ($k = 0; $k < count($c); $k++) {
if (overlap($activity, $c[$k])) {
for ($m = 0; $m < count($j); $m++) {
if (overlap($activity, $j[$m])) {
$solution = 'IMPOSSIBLE';
goto end2;
}
}
$j[] = $activity;
$activity['group'] = 'J';
goto end;
}
}
$c[] = $activity;
$activity['group'] = 'C';
goto end;
}
end:
}
$ids = array_column($activities, 'id');
array_multisort($ids, SORT_ASC, $activities);
foreach ($activities as $act) {
$solution .= $act['group'];
}
end2:
fputs($out, "Case #$x: $solution\n");
}
fclose($in);
fclose($out);
function overlap($act1, $act2) {
return ($act1['start'] >= $act2['start'] && $act1['start'] < $act2['end'])
|| ($act1['end'] > $act2['start'] && $act1['end'] <= $act2['end'])
|| ($act1['start'] <= $act2['start'] && $act1['end'] >= $act2['end']);
}