Problem:


Problem C: Stopped Watches

In the middle of Tyrrhenian Sea, there is a small volcanic island called Chronus. The island
is now uninhabited but it used to be a civilized island. Some historical records imply that the
island was annihilated by an eruption of a volcano about 800 years ago and that most of the
people in the island were killed by pyroclastic flows caused by the volcanic activity. In 2003, a
European team of archaeologists launched an excavation project in Chronus Island. Since then,
the project has provided many significant historic insights. In particular the discovery made
in the summer of 2008 astonished the world: the project team excavated several mechanical
watches worn by the victims of the disaster. This indicates that people in Chronus Island had
such a highly advanced manufacturing technology.

Shortly after the excavation of the watches, archaeologists in the team tried to identify what
time of the day the disaster happened, but it was not successful due to several difficulties. First,
the extraordinary heat of pyroclastic flows severely damaged the watches and took away the
letters and numbers printed on them. Second, every watch has a perfect round form and one
cannot tell where the top of the watch is. Lastly, though every watch has three hands, they
have a completely identical look and therefore one cannot tell which is the hour, the minute,
or the second (It is a mystery how the people in Chronus Island were distinguishing the three hands. Some archaeologists
guess that the hands might be painted with different colors, but this is only a hypothesis, as the paint was lost
by the heat.
). This means that we cannot decide the time indicated by a watch uniquely;
there can be a number of candidates. We have to consider different rotations of the watch.
Furthermore, since there are several possible interpretations of hands, we have also to consider
all the permutations of hands.

You are an information archaeologist invited to the project team and are asked to induce the
most plausible time interval within which the disaster happened, from the set of excavated
watches.

In what follows, we express a time modulo 12 hours. We write a time by the notation hh:mm:ss,
where hh, mm, and ss stand for the hour (hh = 00, 01, 02, . . . , 11), the minute (mm = 00,
01, 02, . . . , 59), and the second (ss = 00, 01, 02, . . . , 59), respectively. The time starts from
00:00:00 and counts up every second 00:00:00, 00:00:01, 00:00:02, . . ., but it reverts to 00:00:00
every 12 hours.

The watches in Chronus Island obey the following conventions of modern analog watches.

A watch has three hands, i.e. the hour hand, the minute hand, and the second hand,
though they look identical as mentioned above.

Every hand ticks 6 degrees clockwise in a discrete manner. That is, no hand stays between
ticks, and each hand returns to the same position every 60 ticks.

The second hand ticks every second.

The minute hand ticks every 60 seconds.

The hour hand ticks every 12 minutes.

At the time 00:00:00, all the three hands are located at the same position.

Because people in Chronus Island were reasonably keen to keep their watches correct and pyroclastic flows spread over the island quite rapidly, it can be assumed that all the watches were
stopped in a short interval of time. Therefore it is highly expected that the time the disaster
happened is in the shortest time interval within which all the excavated watches have at least
one candidate time.

You must calculate the shortest time interval and report it to the project team.

Input

The input consists of multiple datasets, each of which is formatted as follows.

      n

       s1 t1 u1

       s2 t2 u2

            .

            .

            .

       sn tn un

The first line contains a single integer n (2 ≤ n ≤ 10), representing the number of the watches.
The three numbers si , ti , ui in each line are integers such that 0 ≤ si ,ti , ui ≤ 59 and they specify
the positions of the three hands by the number of ticks relative to an arbitrarily chosen position.

Note that the positions of the hands of a watch can be expressed in many different ways. For
example, if a watch was stopped at the time 11:55:03, the positions of hands can be expressed
differently by rotating the watch arbitrarily (e.g. 59 55 3, 0 56 4, 1 57 5, etc.) and as well by
permuting the hour, minute, and second hands arbitrarily (e.g. 55 59 3, 55 3 59, 3 55 59, etc.).

The end of the input is indicated by a line containing a single zero.

Output

For each dataset, output the shortest time interval within which all the watches given in the
dataset have at least one candidate time. The output must be written in a single line in the
following format for each dataset.

       hh:mm:ss h'h':m'm':s's'

Each line contains a pair of times hh:mm:ss and, h'h':m'm':s's' indicating that the shortest
interval begins at hh:mm:ss and ends at h'h':m'm':s's' inclusive. The beginning time and the
ending time are separated by a single space and each of them should consist of hour, minute,
and second in two digits separated by colons. No extra characters should appear in the output.

In calculating the shortest interval, you can exploit the facts that every watch has at least one
candidate time and that the shortest time interval contains 00:00:00 only if the interval starts
from 00:00:00 (i.e. the shortest interval terminates before the time reverts to 00:00:00).

If there is more than one time interval that gives the shortest, output the one that first comes
after 00:00:00 inclusive.

Sample Input

3
8 8 18
32 32 32
57 2 57
5
49 3 49
7 30 44
27 21 21
33 56 56
21 46 4
3
45 52 28
36 26 36
20 55 50
10
33 8 39
50 57 43
35 21 12
21 17 11
16 21 58
45 40 53
45 30 53
39 1 8
55 48 30
7 48 15
0

Output for the Sample Input

00:00:00 00:00:10
06:14:56 06:32:09
07:27:37 07:32:02
05:17:40 05:21:03



Sample input:
3
8 8 18
32 32 32
57 2 57
5
49 3 49
7 30 44
27 21 21
33 56 56
21 46 4
3
45 52 28
36 26 36
20 55 50
10
33 8 39
50 57 43
35 21 12
21 17 11
16 21 58
45 40 53
45 30 53
39 1 8
55 48 30
7 48 15
0


Sample output:
00:00:00 00:00:10
06:14:56 06:32:09
07:27:37 07:32:02
05:17:40 05:21:03

Submission status: Accepted

Solution code:
from itertools import permutations
BASE = 12*3600
def convert(v):
    return "%02d:%02d:%02d" % (v // 3600, (v // 60) % 60, v % 60)
while 1:
    N = int(input())
    if N == 0:
        break
    R = set()
    L = []
    for i in range(N):
        ts = set()
        *E, = map(int, input().split())
        for a, b, c in permutations(E, r=3):
            for i in range(60):
                h = (a+i) % 60; m = (b+i)%60; s = (c+i) % 60
                if m // 12 == h % 5:
                    v = 3600*(h//5) + 60*m + s
                    ts.add(v)
                    R.add(v)
        L.append(sorted(ts))
    R = sorted(R)
    res = 13*3600; mi = ma = 0
    C = [0]*N
    for r in R:
        s = r
        for i in range(N):
            c = C[i]; ts = L[i]; l = len(ts)
            while c < l and ts[c] < r:
                c += 1
            C[i] = c
            if c == l:
                s = max(s, BASE + ts[0])
            else:
                s = max(s, ts[c])
        if s - r < res:
            res = s - r
            mi = r % BASE; ma = s % BASE
    print("%s %s" % (convert(mi), convert(ma)))

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**13
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    d = collections.defaultdict(list)
    def kf(e):
        na = []
        for i in range(3):
            na.append(sorted([(e[j]-e[i])%60 for j in range(3)]))
        return tuple(min(na))

    for h in range(24):
        for m in range(60):
            for s in range(60):
                a = h * 5 + m // 12
                k = kf([a,m,s])
                d[k].append((h*3600+m*60+s))

    def to_time(i):
        h = i // 3600
        m = i // 60 % 60
        s = i % 60
        return '{:02d}:{:02d}:{:02d}'.format(h,m,s)

    def f(n):
        a = [LI() for _ in range(n)]
        cs = [d[kf(a[i])] + [inf] for i in range(n)]
        mr = inf
        mi = -1
        for i in range(60*60*24):
            t = i
            for j in range(n):
                c = cs[j][bisect.bisect_left(cs[j], i)]
                if t < c:
                    t = c
            tr = t - i
            if mr > tr:
                mr = tr
                mi = i
        return '{} {}'.format(to_time(mi), to_time(mi+mr))

    while 1:
        n = I()
        if n == 0:
            break
        rr.append(f(n))

    return '\n'.join(map(str, rr))


print(main())